コード例 #1
0
ファイル: MatrixDigit.cs プロジェクト: Piirtaa/Arith
 public MatrixDigit(string symbol, NumeralSet numberSystem)
 {
     if (string.IsNullOrEmpty(symbol))
         throw new ArgumentNullException("symbol");
     this._symbol = symbol;
     this._matrix = numberSystem.Matrix;
 }
コード例 #2
0
ファイル: ArithmeticMatrix.cs プロジェクト: Piirtaa/Arith
        public ArithmeticMatrix(NumeralSet numeralSet)
        {
            if (numeralSet == null)
                throw new ArgumentNullException("numeralSet");
            this._numeralSet = numeralSet;

            var keys = this._numeralSet.SymbolSet.As<LinkedList<string>>().Values;
            this._addMap = new SquareLookup<Tuple<string, bool>>(keys);
            this._subtractMap = new SquareLookup<Tuple<string, bool>>(keys);
            this._compareMap = new SquareLookup<bool?>(keys);

            foreach (var key1 in keys)
            {
                foreach (var key2 in keys)
                {
                    var dig1 = numeralSet.GetSymbolicDigit(key1);
                    var rollover = dig1.Add(key2);
                    this._addMap.Add(key1, key2, new Tuple<string, bool>(dig1.Symbol, rollover));

                    dig1 = numeralSet.GetSymbolicDigit(key1);
                    rollover = dig1.Subtract(key2);
                    this._subtractMap.Add(key1, key2, new Tuple<string, bool>(dig1.Symbol, rollover));

                    dig1 = numeralSet.GetSymbolicDigit(key1);
                    var compare = dig1.Compare(key2);
                    this._compareMap.Add(key1, key2, compare);
                }
            }
        }
コード例 #3
0
        public static void Test()
        {
            //init the set
            NumeralSet set = new NumeralSet(".", "-");
            for (int i = 0; i < 10; i++)
            {
                set.AddSymbolToSet(i.ToString());
            }

            var numA = Numeric.New(set, "1234567890.246");
            numA.ZoneIterateWithIndex((node, idx) =>
            {
                Debug.WriteLine("on number {0} digit {1} idx {2}", numA.SymbolsText,
                    node.NodeValue.Symbol, idx.SymbolsText);

                var mag = node.GetDigitMagnitude();
                Debug.WriteLine("digit order of mag " + mag.SymbolsText);

                var pos = node.GetDigitPosition();
                Debug.WriteLine("digit pos " + pos.SymbolsText);
            }, (node, idx) =>
            {
                Debug.WriteLine("on number {0} digit {1} idx {2}", numA.SymbolsText,
            node.NodeValue.Symbol, idx.SymbolsText);

                var mag = node.GetDigitMagnitude();
                Debug.WriteLine("digit order of mag " + mag.SymbolsText);

                var pos = node.GetDigitPosition();
                Debug.WriteLine("digit pos " + pos.SymbolsText);
            });

            int topLimit = 100;
            for (int x = 0; x < topLimit; x++)
            {
                for (int y = 0; y < topLimit; y++)
                {
                    var num1 = Numeric.New(set, x.ToString()).HasAddition();
                    var num2 = Numeric.New(set, y.ToString()).HasAddition();
                    int res = x + y;
                    num1.Add(num2);

                    Debug.Assert(num1.SymbolsText == res.ToString());
                    Debug.WriteLine("{0} + {1} = {2}", x, y, res);

                    num1.Subtract(num2);
                    int res2 = x;
                    Debug.Assert(num1.SymbolsText == res2.ToString());

                    num2.Subtract(num1);
                    int res3 = y - x;
                    Debug.Assert(num2.SymbolsText == res3.ToString());
                    Debug.WriteLine("{0} - {1} = {2}", y, x, res3);

                }
            }
        }
コード例 #4
0
        public static void Test()
        {
            //init the set
            NumeralSet set = new NumeralSet(".", "-");
            for (int i = 0; i < 10; i++)
            {
                set.AddSymbolToSet(i.ToString());
            }

            int topLimit = 100;
            for (int x = 1; x < topLimit; x++)
            {
                for (int y = 1; y < topLimit; y++)
                {
                    var num1 = Numeric.New(set, x.ToString()).HasMultiplication();

                    int res = x * y;
                    num1.Multiply(y.ToString());

                    Debug.Assert(num1.SymbolsText == res.ToString());
                }
            }
        }
コード例 #5
0
ファイル: SymbolicDigit.cs プロジェクト: Piirtaa/Arith
        public static void Test()
        {
            //init the set
            NumeralSet set = new NumeralSet(".", "-");
            for (int i = 0; i < 10; i++)
            {
                set.AddSymbolToSet(i.ToString());
            }

            var digit = set.GetSymbolicDigit("0");
            Debug.Assert(digit.Symbol == "0");
            var s = digit.AddOne();
            Debug.Assert(!s);
            Debug.Assert(digit.Symbol == "1");
            digit.AddOne();
            Debug.Assert(digit.Symbol == "2");
            s = digit.Add("9");
            Debug.Assert(digit.Symbol == "1");
            Debug.Assert(s);
            digit.Subtract("1");
            Debug.Assert(digit.Symbol == "0");
            s = digit.SubtractOne();
            Debug.Assert(digit.Symbol == "9");
            Debug.Assert(s);

            for (int i = 0; i < 9; i++)
            {
                var i2 = i + 1;
                var digit1 = set.GetSymbolicDigit(i.ToString());
                var comp = digit1.Compare(i2.ToString());
                Debug.Assert(comp.Value == false);

                var digit2 = set.GetSymbolicDigit(i2.ToString());
                digit1.AddOne();
                Debug.Assert(digit1.Symbol == i2.ToString());
            }
        }
コード例 #6
0
ファイル: NumeralSet.cs プロジェクト: Piirtaa/Arith
        public static void Test()
        {
            //init the set
            NumeralSet set = new NumeralSet(".", "-");
            for (int i = 0; i < 10; i++)
            {
                set.AddSymbolToSet(i.ToString());
            }

            Debug.Assert(set.DecimalSymbol == ".");
            Debug.Assert(set.NegativeSymbol == "-");
            Debug.Assert(set.ZeroSymbol == "0");
            Debug.Assert(set.OneSymbol == "1");

            var parse1 = set.ParseSymbols("1234567890", true, true);
            Debug.Assert(parse1.Length == 10);
            Debug.Assert(parse1[0] == "1");
            Debug.Assert(parse1[1] == "2");
            Debug.Assert(parse1[2] == "3");
            Debug.Assert(parse1[3] == "4");
            Debug.Assert(parse1[4] == "5");
            Debug.Assert(parse1[5] == "6");
            Debug.Assert(parse1[6] == "7");
            Debug.Assert(parse1[7] == "8");
            Debug.Assert(parse1[8] == "9");
            Debug.Assert(parse1[9] == "0");

            var parse2 = set.ParseSymbols("-1234567890.123", true, true);
            Debug.Assert(parse2[0] == "-");
            Debug.Assert(parse2[1] == "1");
            Debug.Assert(parse2[2] == "2");
            Debug.Assert(parse2[3] == "3");
            Debug.Assert(parse2[4] == "4");
            Debug.Assert(parse2[5] == "5");
            Debug.Assert(parse2[6] == "6");
            Debug.Assert(parse2[7] == "7");
            Debug.Assert(parse2[8] == "8");
            Debug.Assert(parse2[9] == "9");
            Debug.Assert(parse2[10] == "0");
            Debug.Assert(parse2[11] == ".");
            Debug.Assert(parse2[12] == "1");
            Debug.Assert(parse2[13] == "2");
            Debug.Assert(parse2[14] == "3");

            var parse3 = set.ParseSymbols("-1234567890.123", false, true);
            Debug.Assert(parse3[14] == "-");
            Debug.Assert(parse3[13] == "1");
            Debug.Assert(parse3[12] == "2");
            Debug.Assert(parse3[11] == "3");
            Debug.Assert(parse3[10] == "4");
            Debug.Assert(parse3[9] == "5");
            Debug.Assert(parse3[8] == "6");
            Debug.Assert(parse3[7] == "7");
            Debug.Assert(parse3[6] == "8");
            Debug.Assert(parse3[5] == "9");
            Debug.Assert(parse3[4] == "0");
            Debug.Assert(parse3[3] == ".");
            Debug.Assert(parse3[2] == "1");
            Debug.Assert(parse3[1] == "2");
            Debug.Assert(parse3[0] == "3");

            try
            {
                var parse4 = set.ParseSymbols("x-1234567890.123", false, false);
            }
            catch { }

            var digit = set.GetSymbolicDigit("0");
            Debug.Assert(digit.Symbol.Equals(set.ZeroSymbol));
            var s = digit.AddOne();
            Debug.Assert(!s);
            Debug.Assert(digit.Symbol == "1");
            digit.AddOne();
            Debug.Assert(digit.Symbol == "2");
            s = digit.Add("9");
            Debug.Assert(digit.Symbol == "1");
            Debug.Assert(s);
            digit.Subtract("1");
            Debug.Assert(digit.Symbol == "0");
            s = digit.Subtract("1");
            Debug.Assert(digit.Symbol == "9");
            Debug.Assert(s);
        }
コード例 #7
0
ファイル: NumeralSet.cs プロジェクト: Piirtaa/Arith
        public bool IsCompatible(NumeralSet set)
        {
            if (set == null)
                return false;

            if (!this.DecimalSymbol.Equals(set.DecimalSymbol))
                return false;

            if (!this.NegativeSymbol.Equals(set.NegativeSymbol))
                return false;

            var thisNode = this.SymbolSet.FirstNode;
            var setNode = set.SymbolSet.FirstNode;
            while (thisNode != null && setNode != null)
            {
                if (!thisNode.NodeValue.Equals(setNode.NodeValue))
                    return false;

                thisNode = thisNode.NextNode;
                setNode = setNode.NextNode;

                if (thisNode != null && setNode == null)
                    return false;

                if (thisNode == null && setNode != null)
                    return false;
            }

            return true;
        }
コード例 #8
0
        public static void Test()
        {
            //init the set
            NumeralSet set = new NumeralSet(".", "-");
            for (int i = 0; i < 10; i++)
            {
                set.AddSymbolToSet(i.ToString());
            }

            var precision = Numeric.New(set, "5").HasAddition();
            var num = Numeric.New(set, "123456789").HasPrecision(precision).HasShift();

            var counter = precision.Clone() as AddingNumericDecoration;
            counter.PerformThisManyTimes(x =>
            {
                var shifty = num.As<IHasShift>(false);
                shifty.ShiftLeft();
            });
            Debug.Assert(num.SymbolsText == "1234.56789");
            num.As<IHasShift>(false).ShiftLeft();

            //the precision is maintained to 5 digits
            Debug.Assert(num.SymbolsText == "123.45678");
        }
コード例 #9
0
        public static void Test()
        {
            //init the set
            NumeralSet set = new NumeralSet(".", "-");
            for (int i = 0; i < 10; i++)
            {
                set.AddSymbolToSet(i.ToString());
            }

            Numeric num = Numeric.New(set, "123456789");
            var shiftNum = num.HasShift();

            shiftNum.ShiftToZero();
            Debug.Assert(num.SymbolsText == "123456789");

            shiftNum.ShiftRight();
            Debug.Assert(num.SymbolsText == "1234567890");

            shiftNum.ShiftRight();
            Debug.Assert(num.SymbolsText == "12345678900");

            shiftNum.ShiftLeft();
            Debug.Assert(num.SymbolsText == "1234567890");

            shiftNum.ShiftLeft();
            Debug.Assert(num.SymbolsText == "123456789");

            for (int i = 0; i < 20; i++)
            {
                var oldNum = shiftNum.Clone();
                shiftNum.ShiftLeft();
                Debug.Assert(oldNum.IsGreaterThan(shiftNum));
            }
        }