/* * Bernoulli Compiler * Copyright (c) Cornell University * Department of Computer Science * * Kamen Yotov (kamen@yotov.org) * * $Source: C:/CVS/kyotov/kyotov/Research/BC/ILP/Math/Element.cs,v $ * $Revision: 1.3 $ * $Date: 2003/03/24 22:26:42 $ */ using System; namespace ILP { namespace Math { public class Element { private int v; public Element (int v) { this.v = v; } public static implicit operator int (Element i) { return i.v; } public static implicit operator Element (int i) { return new Element(i); } public static Element operator + (Element i1, Element i2) { checked { return i1.v + i2.v; } } public static Element operator * (Element i1, Element i2) { checked { return i1.v * i2.v; } } public static Element operator - (Element i) { checked { return -i.v; } } public static Element operator - (Element i1, Element i2) { checked { return i1 + (-i2); } } public static Element operator / (Element i1, Element i2) { checked { return i1.v / i2.v; } } public static bool operator == (Element i1, Element i2) { return i1.v == i2.v; } public static bool operator != (Element i1, Element i2) { return !(i1 == i2); } public static bool operator < (Element i1, Element i2) { return i1.v < i2.v; } public static bool operator > (Element i1, Element i2) { return i2.v < i1.v; } public static bool operator <= (Element i1, Element i2) { return !(i1 > i2); } public static bool operator >= (Element i1, Element i2) { return !(i1 < i2); } public override bool Equals (object o) { return o is Element && this == (Element)o; } public override int GetHashCode () { return base.GetHashCode(); } public override string ToString () { return v.ToString(); } public int Sign { get { return System.Math.Sign(this); } } public Element Abs { get { return new Element(System.Math.Abs(v)); } } protected static void gcd (Element u, Element d, Element nu, Element nd, out Element ou, out Element od) { if (nd == 0) { ou = u; od = d; } else gcd(nu, nd, u - d / nd * nu, d % nd, out ou, out od); } public static Element gcd (Element a, Element b, out Element u, out Element v) { Element d; int sa = a.Sign; int sb = b.Sign; a = a.Abs; b = b.Abs; gcd(1, a, 0, b, out u, out d); if (d == a * u) v = 0; else v = (d - a * u) / b; if (a != 0) { u += v / a * b; v -= v / a * a; } u *= sa; v *= sb; return d; } public static Element gcd (Element x, Element y) { Element u; Element v; return gcd(x, y, out u, out v); } public static Element lcm (Element x, Element y) { return x / gcd(x, y) * y; } } } }