public override Polynom <T, R> Prod(Polynom <T, R> a, Polynom <T, R> b) { var _prod = a * b; if (_prod.Degree >= this.FMode.Degree) { var _rem = new List <Polynom <T, R> >(); _prod.Div(this.FMode, _rem); _prod = _rem.Last(); } return(_prod); }
public static Polynom <T, R> GetGcd(Polynom <T, R> a, Polynom <T, R> b) { var _alocal = new Polynom <T, R>(a); var _zero = new Polynom <T, R>(_alocal.FRing, -1); var _one = new Polynom <T, R>(_alocal.FRing, 0); var gcd = _zero; if (_alocal.Degree < 0) { gcd = b; } else if (b.Degree < 0) { gcd = _alocal; } else { var r1 = b; var r2 = new List <Polynom <T, R> >(); var _quot = _alocal.Div(b, r2); gcd = b; var q = _zero; var temp = new List <Polynom <T, R> >(); var temp1 = _zero; while (r2.Last().Degree >= 0) { gcd = r2.Last(); q = r1.Div(r2.Last(), temp); //r1 = r2.Last(); r1 = gcd; r2 = temp; } } var _major = gcd.FCoeffs[gcd.Degree]; if (!_alocal.FRing.Equals(_major, _alocal.FRing.One)) { var _major_1 = _alocal.FRing.Reverse(_major); gcd.Mult(_major_1); } return(gcd); }
public Polynom <Matrix <T, R>, MRing <T, R> > GetMatrixPolynom(int aMatrixSize) { var _mring = new MRing <T, R>(this.FRing, aMatrixSize); var _mcoeffs = new Matrix <T, R> [this.FCoeffs.Length]; for (var i = 0; i < this.FCoeffs.Length; ++i) { _mcoeffs[i] = _mring.One; _mcoeffs[i].Mult(this.FCoeffs[i]); } var _m_xi = new Polynom <Matrix <T, R>, MRing <T, R> >(_mring, _mcoeffs); return(_m_xi); }
public static Polynom <T, R> GetGcd <T, R>(Polynom <T, R> a, Polynom <T, R> b) where R : RingBase <T> { var temp = new List <Polynom <T, R> >(); var r1 = a; var r2 = b; while (r2.Degree >= 0) { r1.Div(r2, temp); r1 = r2; r2 = temp.Last(); } return(r1); }
public string MakeDivTexString( Polynom <T, R> aDenumerator, Polynom <T, R> aQuot, List <Polynom <T, R> > aRemainder, string aVarName) { var _sb = new StringBuilder(); _sb.AppendLine(@"\extrarowheight=2pt"); _sb.AppendLine(@"\arraycolsep=0.05em"); _sb.AppendFormat(@"\begin{{array}}{{"); for (int i = 0; i <= this.Degree; ++i) { _sb.Append("r"); } _sb.Append(@"@{\,}r|l}"); _sb.AppendLine(); _sb.Append(this.TexString(aVarName, true)); _sb.Append(@"&&\,"); _sb.Append(aDenumerator.TexString(aVarName, false)); _sb.Append(@"\\"); _sb.AppendLine(); _sb.AppendFormat("\\cline{{{0}-{0}}}", this.Degree + 3); var _secondString = String.Format("{0}&&\\,{1}\\\\", aRemainder[0].TexString(aVarName, true).TrimEnd('\\'), aQuot.TexString(aVarName, false)); _sb.AppendLine(_secondString); for (var i = 1; i < aRemainder.Count; ++i) { if (i % 2 == 1) { int s = this.Degree - aRemainder[i - 1].Degree + 1; _sb.AppendLine(String.Format("\\cline{{{0}-{1}}}", s, s + aDenumerator.Degree)); } int _shift = this.Degree - aRemainder[i].Degree + 1; if (aRemainder[i].Degree < 0) { _shift--; } var _pref = new String('&', _shift - 1); _sb.AppendLine(String.Format("{0}{1}\\\\", _pref, aRemainder[i].TexString(aVarName, true))); } _sb.AppendLine(@"\end{array}"); return(_sb.ToString()); }
public override Polynom <T, R> InnerReverse(Polynom <T, R> a) { if (a.Degree != 0) { throw new DivideByZeroException( String.Format("The polynom {0} isn't invertible on the ring {1}.", a, this)); } if (a.Equals(One)) { return(One); } var _reverse = this.FRing.Reverse(a.GetValue(this.FRing.Zero)); return(new Polynom <T, R>(this.FRing, new T[] { _reverse })); }
public Polynom <T, R> RightDiv(Polynom <T, R> aDenumerator, List <Polynom <T, R> > aRemainder) { return(this.Div(aDenumerator, aRemainder, true)); }
public Polynom <T, R> LeftDiv(Polynom <T, R> aDenumerator, List <Polynom <T, R> > aRemainder) { return(this.Div(aDenumerator, aRemainder, false)); }
private Polynom <T, R> Div(Polynom <T, R> aDenumerator, List <Polynom <T, R> > aRemainder, bool aIsRight) { if (aRemainder == null) { aRemainder = new List <Polynom <T, R> >(); } var quot = new Polynom <T, R>(this.FRing, -1); if (this.Degree < aDenumerator.Degree) { aRemainder.Add(this); return(quot); } int deg_quot = this.Degree - aDenumerator.Degree; var _rem = new Polynom <T, R>(this); var _minusOne = this.FRing.Opposite(this.FRing.One); var c = this.FRing.Reverse(aDenumerator.FCoeffs[aDenumerator.Degree]); while (_rem.Degree >= aDenumerator.Degree) { int p = _rem.Degree - aDenumerator.Degree; var d = new T[p + 1]; if (aIsRight) { d[p] = this.FRing.Prod(_rem.FCoeffs[_rem.Degree], c); } else { d[p] = this.FRing.Prod(c, _rem.FCoeffs[_rem.Degree]); } var dp = new Polynom <T, R>(this.FRing, d); quot += dp; var dd = new Polynom <T, R>(this.FRing, -1); if (aIsRight) { dd = dp * aDenumerator; } else { dd = aDenumerator * dp; } aRemainder.Add(new Polynom <T, R>(dd)); dd.Mult(_minusOne); _rem += dd; aRemainder.Add(_rem); } if (quot.Degree < deg_quot) { var _shift = new Polynom <T, R>(this.FRing, deg_quot - quot.Degree); quot = quot * _shift; } return(quot); }
public static Polynom <T, R> operator -(Polynom <T, R> Ax, Polynom <T, R> Bx) { var _bx = new Polynom <T, R>(Bx); return(Ax + (-_bx)); }
public PxFx(R aRing, Polynom <T, R> aMode) { this.FRing = aRing; this.FMode = aMode; }
public override Polynom <T, R> Prod(Polynom <T, R> a, Polynom <T, R> b) { return(a * b); }
public override Polynom <T, R> Opposite(Polynom <T, R> a) { return(-a); }
public override string GetTexString(Polynom <T, R> a) { return(String.Format("[{0}]", a.TexString())); }
public override bool Equals(Polynom <T, R> a, Polynom <T, R> b) { return(a == b); }
public override bool IsNaN(Polynom <T, R> a) { return(false); }
public static Polynom <T, R> GetGcdEx(Polynom <T, R> a, Polynom <T, R> b, out Polynom <T, R> u, out Polynom <T, R> v) { var _alocal = new Polynom <T, R>(a); var _zero = new Polynom <T, R>(_alocal.FRing, -1); var _one = new Polynom <T, R>(_alocal.FRing, 0); var gcd = _zero; if (_alocal.Degree < 0) { u = _zero; v = (b.Degree < 0 ? _zero : _one); gcd = b; } else if (b.Degree < 0) { v = _zero; u = (_alocal.Degree < 0 ? _zero : _one); gcd = _alocal; } else { var r1 = b; var r2 = new List <Polynom <T, R> >(); var _quot = _alocal.Div(b, r2); gcd = b; var u1 = _one; var u2 = _zero; var v1 = _one; var v2 = _one; var q = _zero; if (r2.Last().Degree < 0) { u1 = _zero; } else { v1 = -_quot; } var temp = new List <Polynom <T, R> >(); var temp1 = _zero; while (r2.Last().Degree >= 0) { gcd = r2.Last(); q = r1.Div(r2.Last(), temp); //r1 = r2.Last(); r1 = gcd; r2 = temp; temp1 = u1; u1 = u2 - u1 * q; u2 = temp1; temp1 = v1; v1 = v2 - v1 * q; v2 = temp1; } u = u2; v = v2; } var _major = gcd.FCoeffs[gcd.Degree]; if (!_alocal.FRing.Equals(_major, _alocal.FRing.One)) { var _major_1 = _alocal.FRing.Reverse(_major); gcd.Mult(_major_1); } var _sum = u * a + v * b; _major = _sum.FCoeffs[_sum.Degree]; if (!_alocal.FRing.Equals(_major, _alocal.FRing.One)) { var _major_1 = _alocal.FRing.Reverse(_major); u.Mult(_major_1); v.Mult(_major_1); } return(gcd); }
public override Polynom <T, R> Sum(Polynom <T, R> a, Polynom <T, R> b) { return(a + b); }
private List <Polynom <T, R> > Factorisation(Polynom <T, R> aPolynom) { if (!(aPolynom.FRing is IEnumerable <T>)) { throw new ArgumentException("Polinom's ring mast be enumerable."); } var p = (int)aPolynom.FRing.Size; var _res = new List <Polynom <T, R> >(); if (aPolynom.Degree < 2) { return(_res); } var _pring = new Rx <T, R>(aPolynom.FRing); var _derivative = aPolynom.Derivative; var _gcd = Polynom <T, R> .GetGcd(aPolynom, _derivative); if (_gcd.Degree > 0) { if (_derivative != _pring.Zero) { _res.Add(_gcd); _res.Add(aPolynom.Div(_gcd, null)); } else { p = (int)aPolynom.FRing.SimpleSubfieldSize; var c = aPolynom.Degree / p; var _pol = new Polynom <T, R>(aPolynom.FRing, c); for (var i = 0; i <= c; i++) { _pol.FCoeffs[i] = aPolynom.FRing.Pow( aPolynom.FCoeffs[p * i], this.FRing.Size / p); } for (var i = 0; i < p; i++) { _res.Add(new Polynom <T, R>(_pol)); } } return(_res); } var _aij_vals = new T[aPolynom.Degree, aPolynom.Degree]; for (var i = 1; i < aPolynom.Degree; i++) { var _pol = new Polynom <T, R>(aPolynom.FRing, i * p); _pol.FCoeffs[i] = aPolynom.FRing.Opposite(aPolynom.FRing.One); var _rem = new List <Polynom <T, R> >(); _pol.Div(aPolynom, _rem); var _ai = _rem.Last(); for (var j = 0; j <= _ai.Degree; j++) { _aij_vals[j, i - 1] = _ai.FCoeffs[j]; } } var _aij = new Matrix <T, R>(aPolynom.FRing, _aij_vals); var _matrix = Matrix <T, R> .SolveLs(_aij); var _solvation = new T[_matrix.RowCount + 1]; _solvation[0] = aPolynom.FRing.Zero; bool IsResEmpty = true; var _matrixValues = _matrix.Values; for (var i = 0; i < _matrix.RowCount; i++) { if (_solvation[i + 1] == null) { _solvation[i + 1] = aPolynom.FRing.Zero; } for (var j = 0; j < _matrix.ColumnCount; j++) { _solvation[i + 1] = aPolynom.FRing.Sum(_solvation[i + 1], _matrixValues[i, j]); } IsResEmpty = _solvation[i + 1].Equals(aPolynom.FRing.Zero); } if (!IsResEmpty) { var _pp = new Polynom <T, R>(aPolynom.FRing, _solvation); var _gcd1 = _pring.One; var _enumerable = (IEnumerable <T>) this.FRing; foreach (var _item in _enumerable) { var _ppp = new Polynom <T, R>(_pp); _ppp.FCoeffs[0] = _ppp.FRing.Sum(_item, _ppp.FCoeffs[0]); _gcd1 = Polynom <T, R> .GetGcd(aPolynom, _ppp); if (!_gcd1.Equals(_pring.One)) { _res.Add(_gcd1); var _rem = new List <Polynom <T, R> >(); var _quot = aPolynom.Div(_gcd1, _rem); _res.Add(_quot); break; } } } return(_res); }
public override Polynom <T, R> LeftReverse(Polynom <T, R> a) { return(this.Reverse(a)); }