Ejemplo n.º 1
0
        public ModMatrix invert()
        {
            ModMatrix mi = new ModMatrix(Dimension, Modulus);

            BigInteger di = BigIntegerHelper.ModInverse(det(), Modulus);

            for (int y = 0; y < Dimension; y++)
            {
                for (int x = 0; x < Dimension; x++)
                {
                    int sign = 1 - 2 * ((x + y) % 2);       // sign = (-1) ^ (x+y)
                    mi[x, y] = (((sign * di * minor(y, x)) % Modulus) + Modulus) % Modulus;
                }
            }

            return(mi);
        }
Ejemplo n.º 2
0
        private static Stack <TOKEN> Scan(string expr)
        {
            TOKEN t          = new TOKEN();
            int   startIndex = 0;

            if (expr == "")
            {
                return(new Stack <TOKEN>());
            }
            switch (expr[0])
            {
            case ' ':
                return(Scan(expr.Substring(1)));

            case '(':
                t.ttype    = TOKEN.Ttype.BRACKETOPEN;
                startIndex = 1;
                break;

            case ')':
                t.ttype    = TOKEN.Ttype.BRACKETCLOSE;
                startIndex = 1;
                break;

            case '+':
                t.ttype    = TOKEN.Ttype.PLUS;
                startIndex = 1;
                break;

            case '-':
                t.ttype    = TOKEN.Ttype.MINUS;
                startIndex = 1;
                break;

            case '*':
                if (expr.Length > 1 && expr[1] == '*')
                {
                    t.ttype    = TOKEN.Ttype.POW;
                    startIndex = 2;
                }
                else
                {
                    t.ttype    = TOKEN.Ttype.MULTIPLY;
                    startIndex = 1;
                }
                break;

            case '/':
                t.ttype    = TOKEN.Ttype.DIVIDE;
                startIndex = 1;
                break;

            case '^':
                t.ttype    = TOKEN.Ttype.POW;
                startIndex = 1;
                break;

            case '!':
                t.ttype    = TOKEN.Ttype.EXCLAMATION;
                startIndex = 1;
                break;

            case '%':
                t.ttype    = TOKEN.Ttype.PERCENT;
                startIndex = 1;
                break;

            case ',':
                t.ttype    = TOKEN.Ttype.COMMA;
                startIndex = 1;
                break;

            default:

                Match m;
                bool  found = false;
                foreach (var f in prefixFunctions)
                {
                    m = Regex.Match(expr, "^" + f.Value.pattern, RegexOptions.IgnoreCase);
                    if (m.Success)
                    {
                        t.ttype    = TOKEN.Ttype.FUNCTION;
                        t.function = f.Key;
                        startIndex = m.Length;
                        found      = true;
                        break;
                    }
                }
                if (found)
                {
                    break;
                }

                if (Regex.IsMatch(expr, "^mod", RegexOptions.IgnoreCase))
                {
                    t.ttype    = TOKEN.Ttype.MOD;
                    startIndex = 3;
                    break;
                }

                // try to parse as hexadecimal, decimal, octal or binary number

                m = Regex.Match(expr, @"^([0-9a-z]+)\(([0-9]+)\)", RegexOptions.IgnoreCase);
                if (m.Success)
                {
                    int basis = int.Parse(m.Groups[2].Value);
                    t.integer  = Parse(m.Groups[1].Value, basis);
                    t.ttype    = TOKEN.Ttype.INTEGER;
                    startIndex = m.Groups[0].Value.Length;
                    break;
                }

                m = Regex.Match(expr, @"^[#hx]([0-9a-f]+)", RegexOptions.IgnoreCase);
                if (m.Success)
                {
                    //t.integer = BigInteger.Parse(m.Groups[1].Value, NumberStyles.AllowHexSpecifier);
                    t.integer  = BigIntegerHelper.Parse(m.Groups[1].Value, 16);
                    t.ttype    = TOKEN.Ttype.INTEGER;
                    startIndex = m.Groups[0].Value.Length;
                    break;
                }

                m = Regex.Match(expr, @"^([0-9]+)");
                if (m.Success)
                {
                    t.integer  = BigInteger.Parse(m.Groups[1].Value);
                    t.ttype    = TOKEN.Ttype.INTEGER;
                    startIndex = m.Groups[0].Value.Length;
                    break;
                }

                m = Regex.Match(expr, @"^o([0-7]+)", RegexOptions.IgnoreCase);
                if (m.Success)
                {
                    t.integer  = BigIntegerHelper.Parse(m.Groups[1].Value, 8);
                    t.ttype    = TOKEN.Ttype.INTEGER;
                    startIndex = m.Groups[0].Value.Length;
                    break;
                }

                m = Regex.Match(expr, @"^b([01]+)", RegexOptions.IgnoreCase);
                if (m.Success)
                {
                    t.integer  = BigIntegerHelper.Parse(m.Groups[1].Value, 2);
                    t.ttype    = TOKEN.Ttype.INTEGER;
                    startIndex = m.Groups[0].Value.Length;
                    break;
                }

                if (expr[0] == '#')
                {
                    t.ttype    = TOKEN.Ttype.HASH;
                    startIndex = 1;
                    break;
                }

                throw new ParseException("parsing failed at character " + expr[0]);
            }
            Stack <TOKEN> st = Scan(expr.Substring(startIndex));

            st.Push(t);
            return(st);
        }