public T Inverse() { // Define our initial variables and give them their initial assignments. BigInteger[] newt = new BigInteger[Degree + 1]; newt[0] = 1; BigInteger[] t = new BigInteger[Degree + 1]; BigInteger[] newr = new BigInteger[Coefficients.Count + 1]; Coefficients.CopyTo(newr, 0); BigInteger[] r = new BigInteger[ModulusCoefficients.Count + 1]; ModulusCoefficients.CopyTo(r, 0); r[r.Length - 1] = 1; // Loop while there are elements which are non-zero. while (GetDegree(newr) != 0) { BigInteger[] quotient = DividePolynomialRounded(r, newr); Array.Resize(ref quotient, Degree + 1); BigInteger[] tempt = (BigInteger[])t.Clone(); BigInteger[] tempr = (BigInteger[])r.Clone(); for (int i = 0; i < Degree + 1; i++) { for (int j = 0; j < Degree + 1 - i; j++) { tempt[i + j] -= newt[i] * quotient[j]; tempr[i + j] -= newr[i] * quotient[j]; } } // Perform modulo on tempt for (int i = 0; i < tempt.Length; i++) { tempt[i] = tempt[i].Mod(Bn128Curve.P); } // Perform modulo on tempr for (int i = 0; i < tempr.Length; i++) { tempr[i] = tempr[i].Mod(Bn128Curve.P); } // Swap state for the next iteration. (newt, newr, t, r) = (tempt, tempr, newt, newr); } // Resize the array to the degree size accordingly, divide and return. Array.Resize(ref newt, Degree); return(New(newt).Divide(newr[0])); }
public void Minimize() { int n_p = Coefficients.Length; int m_dat = n_p; // data and pameter arrays: double[] p = new double[Coefficients.Length]; Coefficients.CopyTo(p, 0); // auxiliary settings: lmmin.lm_control_type control = new lmmin.lm_control_type(); lmmin.lm_initialize_control(control); control.epsilon = 0.001; lm_data_type data = new lm_data_type(); data.f = my_fit_function; // perform the fit: lmmin.lm_minimize(m_dat, n_p, p, lm_evaluate, lm_print, data, control); // print results: Console.Write("status: {0} after {1} evaluations\n", lmmin.lm_shortmsg[control.info], control.nfev); Console.Write("Coefficients = new double[] {"); for (int i = 0; i < Coefficients.Length; i++) { if (i == 0 || i == 5) { Console.WriteLine(); Console.Write(" /* {0} */", i); } Console.Write(" {0:G10},", Coefficients[i]); } Console.WriteLine(); Console.WriteLine("};"); }