public ArgumentsValue Function(MatrixValue X, MatrixValue Y) { if (X.Length != Y.Length) throw new YAMPDifferentLengthsException(X.Length, Y.Length); var x1 = new ScalarValue(); var y1 = new ScalarValue(); var xy = new ScalarValue(); var x2 = new ScalarValue(); var slope = new ScalarValue(); var offset = new ScalarValue(); for (var i = 1; i <= X.Length; i++) { x1 = x1 + X[i]; y1 = y1 + Y[i]; xy = xy + X[i] * Y[i]; x2 = x2 + X[i] * X[i]; } var J = ((Double)X.Length * x2) - (x1 * x1); if (J.Re != 0.0) { slope = (((Double)X.Length * xy) - (x1 * y1)) / J.Re; offset = ((y1 * x2) - (x1 * xy)) / J.Re; } return new ArgumentsValue(slope, offset); }
public MatrixValue Function(MatrixValue M) { if (M.DimensionX != 3 && M.DimensionY != 3) throw new YAMPMatrixDimensionException(3, M.DimensionX, M.DimensionY, M.DimensionX); var isTransposed = M.DimensionY != 3; if (isTransposed) { M = M.Transpose(); } var m = new MatrixValue(3, M.DimensionX); for (var i = 1; i <= M.DimensionX; i++) { var r = M[1, i].Re; var theta = M[2, i].Re; var phi = M[3, i].Re; var rt = r * Math.Sin(theta); m[1, i] = new ScalarValue(rt * Math.Cos(phi)); m[2, i] = new ScalarValue(rt * Math.Sin(phi)); m[3, i] = new ScalarValue(r * Math.Cos(theta)); } return isTransposed ? m.Transpose() : m; }
public MatrixValue Function(MatrixValue M) { if (M.DimensionX != 3 && M.DimensionY != 3) throw new YAMPMatrixDimensionException(3, M.DimensionX, M.DimensionY, M.DimensionX); var isTransposed = M.DimensionY != 3; if (isTransposed) { M = M.Transpose(); } var m = new MatrixValue(3, M.DimensionX); for (var i = 1; i <= M.DimensionX; i++) { var x = M[1, i].Re; var y = M[2, i].Re; var z = M[3, i].Re; var r = Math.Sqrt(x * x + y * y + z * z); var phi = Math.Atan2(y, x); var theta = Math.Acos(z / r); m[1, i] = new ScalarValue(r); m[2, i] = new ScalarValue(theta); m[3, i] = new ScalarValue(phi); } return isTransposed ? m.Transpose() : m; }
// R is the radix, N the total length, and u contains the Nth complex roots of unity public Transformlet(int R, int N, ScalarValue[] u) { this.R = R; this.N = N; this.u = u; this.dx = N / R; }
public MatrixValue Function(ScalarValue x, ScalarValue y, ScalarValue z) { var r = Math.Sqrt(x.Re * x.Re + y.Re * y.Re + z.Re * z.Re); var phi = Math.Atan2(y.Re, x.Re); var theta = Math.Acos(z.Re / r); return new MatrixValue(new[] { new ScalarValue(r), new ScalarValue(theta), new ScalarValue(phi) }, 3, 1); }
public MatrixValue Function(ScalarValue j1, ScalarValue j2) { if (isNotHalf(j1)) throw new ArgumentException("0, +-0.5, +-1, +-1.5, ...", j1.Re.ToString()); if (isNotHalf(j2)) throw new ArgumentException("0, +-0.5, +-1, +-1.5, ...", j2.Re.ToString()); var l = 1; var M = new MatrixValue(); for (var m1 = -j1.Re; m1 <= j1.Re; m1 += 1.0) { for (var m2 = -j2.Re; m2 <= j2.Re; m2 += 1.0) { var m = m1 + m2; var ja = j1.Re + j2.Re; for (var j = Math.Abs(m); j <= ja; j += 1.0) { var v = CGCoefficients(j1.Re, j2.Re, j, m1, m2); M[l, 1] = new ScalarValue(m); M[l, 2] = new ScalarValue(m1); M[l, 3] = new ScalarValue(m2); M[l, 4] = new ScalarValue(j); M[l, 5] = new ScalarValue(v); l++; } } } return M; }
static MatrixValue Generate(Int32 n, Int32 m) { var distribution = new DiscreteUniformDistribution(Rng); var l = n * m; var M = new MatrixValue(n, m); var numbers = new List<Int32>(); distribution.Alpha = 0; for (var i = 1; i <= l; i++) { numbers.Add(i); } for (var j = 1; j <= n; j++) { for (var i = 1; i <= m; i++) { distribution.Beta = numbers.Count - 1; var index = distribution.Next(); index = Math.Max(Math.Min(0, index), numbers.Count - 1); M[j, i] = new ScalarValue(numbers[index]); numbers.RemoveAt(index); } } return M; }
Plot2DValue Plot(IFunction f, Double minx, Double maxx, Double precision) { var cp = new Plot2DValue(); var N = (Int32)((maxx - minx) / precision) + 1; var M = new MatrixValue(N, 2); var x = new ScalarValue(minx); for (var i = 0; i < N; i++) { var row = i + 1; var y = f.Perform(Context, x); M[row, 1] = x.Clone(); if (y is ScalarValue) { M[row, 2] = (ScalarValue)y; } else if (y is MatrixValue) { var Y = (MatrixValue)y; for (var j = 1; j <= Y.Length; j++) { M[row, j + 1] = Y[j]; } } x.Re += precision; } cp.AddPoints(M); return cp; }
public MatrixValue Function(MatrixValue M) { if (M.DimensionX != 2 && M.DimensionY != 2) throw new YAMPMatrixDimensionException(2, M.DimensionX, M.DimensionY, M.DimensionX); var isTransposed = M.DimensionY != 2; if (isTransposed) { M = M.Transpose(); } var m = new MatrixValue(2, M.DimensionX); for (var i = 1; i <= M.DimensionX; i++) { var x = M[1, i].Re; var y = M[2, i].Re; var phi = Math.Atan2(y, x); var r = x == 0.0 ? y : (y == 0.0 ? x : x / Math.Cos(phi)); m[1, i] = new ScalarValue(r * Math.Cos(phi)); m[2, i] = new ScalarValue(r * Math.Sin(phi)); } return isTransposed ? m.Transpose() : m; }
/// <summary> /// Returns the complex dot product (a, b). /// </summary> /// <param name="aStore">The first vector a.</param> /// <param name="aOffset">Offset in the vector a.</param> /// <param name="aStride">The offset between two elements of the vector a.</param> /// <param name="bStore">The second vector b.</param> /// <param name="bOffset">Offset in the vector a.</param> /// <param name="bStride">The offset between two elements of the vector a.</param> /// <param name="count">The number of elements to consider.</param> /// <returns>The result of the complex dot product.</returns> public static ScalarValue cDot(ScalarValue[] aStore, int aOffset, int aStride, ScalarValue[] bStore, int bOffset, int bStride, int count) { double re = 0.0; double im = 0.0; int n = 0; int a = aOffset; int b = bOffset; while (n < count) { var re1 = aStore[a].Re; var im1 = aStore[a].Im; var re2 = bStore[b].Re; var im2 = bStore[b].Im; re += (re1 * re2 - im1 * im2); im += (re1 * im2 + im1 * re2); n++; a += aStride; b += bStride; } return new ScalarValue(re, im); }
public BarPlotValue Function(MatrixValue Y, ScalarValue nbins) { var nn = nbins.GetIntegerOrThrowException("nbins", Name); var bp = new BarPlotValue(); if (Y.IsVector) { bp.AddPoints(YMath.Histogram(Y, nn)); } else { var M = new MatrixValue(); for (var i = 1; i <= Y.DimensionX; i++) { var N = YMath.Histogram(Y.GetColumnVector(i), nn); for (var j = 1; j <= N.Length; j++) { M[j, i] = N[j]; } } bp.AddPoints(M); } return bp; }
public MatrixValue Function(ScalarValue x0, ScalarValue xn, ScalarValue y0, ScalarValue yn) { var m = new Newton(); var xsteps = (int)Math.Abs(Math.Ceiling((xn.Re - x0.Re) / 0.1)); var ysteps = (int)Math.Abs(Math.Ceiling((yn.Re - y0.Re) / 0.1)); return m.CalculateMatrix(x0.Re, xn.Re, y0.Re, yn.Re, xsteps, ysteps); }
public virtual void FftKernel(ScalarValue[] x, ScalarValue[] y, int y0, int dy, int sign) { int yi = y0; y[yi] = ScalarValue.Zero; for (int j = 0; j < R; j++) y[yi] += x[j]; // now do the higher index entries for (int i = 1; i < R; i++) { yi += dy; y[yi] = x[0]; int ui = 0; if (sign < 0) ui = N; int du = dx * i; if (sign < 0) du = -du; for (int j = 1; j < R; j++) { ui += du; if (ui >= N) ui -= N; if (ui < 0) ui += N; y[yi] += x[j] * u[ui]; } } }
public MatrixValue Function(ScalarValue x0, ScalarValue xn, ScalarValue y0, ScalarValue yn, ScalarValue xsteps, ScalarValue ysteps) { var xs = xsteps.GetIntegerOrThrowException("xsteps", Name); var ys = ysteps.GetIntegerOrThrowException("ysteps", Name); var m = new Mandelbrot(); return m.CalculateMatrix(x0.Re, xn.Re, y0.Re, yn.Re, xs, ys); }
public virtual void FftPass(ScalarValue[] x, ScalarValue[] y, int Ns, int sign) { var v = new ScalarValue[R]; int dx = N / R; for (int j = 0; j < dx; j++) { int xi = j; int ui = 0; if (sign < 0) ui = N; int du = (dx / Ns) * (j % Ns); if (sign < 0) du = -du; v[0] = x[xi]; for (int r = 1; r < R; r++) { xi += dx; ui += du; v[r] = x[xi] * u[ui]; } int y0 = Expand(j, Ns, R); FftKernel(v, y, y0, Ns, sign); } }
public static ScalarValue Ylm(int l, int m, double theta, double phi) { var expphi = new ScalarValue(0.0, m * phi).Exp(); var factor = m < 0 ? Math.Pow(-1, -m) : 1.0; var legend = Plm(l, Math.Abs(m), Math.Cos(theta)); return factor * expphi * legend; }
public ArgumentsValue Function(ScalarValue n) { var nn = n.GetIntegerOrThrowException("n", Name); int dim = nn + 1; if (dim < 2) throw new YAMPArgumentRangeException("n", 1.0); var X = new MatrixValue(dim, dim); // x = sin(phi) * cos(theta) var Y = new MatrixValue(dim, dim); // y = sin(phi) * sin(theta) var Z = new MatrixValue(dim, dim); // z = cos(phi) var stheta = Table(0.0, 2.0 * Math.PI, dim, Math.Sin); var ctheta = Table(0.0, 2.0 * Math.PI, dim, Math.Cos); var sphi = Table(0.0, Math.PI, dim, Math.Sin); var cphi = Table(0.0, Math.PI, dim, Math.Cos); for (var j = 0; j < dim; j++) { var col = j + 1; for (var i = 0; i < dim; i++) { var row = i + 1; X[row, col] = new ScalarValue(sphi[j] * ctheta[i]); Y[row, col] = new ScalarValue(sphi[j] * stheta[i]); Z[row, col] = new ScalarValue(cphi[j]); } } return new ArgumentsValue(X, Y, Z); }
public MatrixValue Function(MatrixValue x, MatrixValue y, ScalarValue n) { if (x.Length != y.Length) throw new YAMPDifferentLengthsException(x.Length, y.Length); var nn = n.GetIntegerOrThrowException("n", Name); var m = nn + 1; if (m < 2) throw new YAMPArgumentRangeException("n", 0.0); var M = new MatrixValue(x.Length, m); var b = new MatrixValue(x.Length, 1); for (var j = 1; j <= M.Rows; j++) { var el = ScalarValue.One; var z = x[j]; for (var i = 1; i <= M.Columns; i++) { M[j, i] = el; el *= z; } b[j, 1] = y[j]; } var qr = QRDecomposition.Create(M); return qr.Solve(b); }
public MatrixValue Function(MatrixValue original, ScalarValue x) { var spline = new SplineInterpolation(original); var M = new MatrixValue(1, 2); M[1, 1].Re = x.Re; M[1, 2].Re = spline.ComputeValue(x.Re); return M; }
protected override ScalarValue GetValue(ScalarValue value) { if (value.Re == 0.0 && value.Im == 0.0) return ScalarValue.One; var arg = value * Math.PI; return arg.Sin() / arg; }
public MatrixValue Function(ScalarValue r, ScalarValue phi, ScalarValue theta) { var rt = r.Re * Math.Sin(theta.Re); var x = new ScalarValue(rt * Math.Cos(phi.Re)); var y = new ScalarValue(rt * Math.Sin(phi.Re)); var z = new ScalarValue(r.Re * Math.Cos(theta.Re)); return new MatrixValue(new[] { x, y, z }, 3, 1); }
public ScalarValue Function(ScalarValue n, ScalarValue z) { var nn = n.GetIntegerOrThrowException("n", Name); if (nn < 0) throw new Exception("Hermite polynomial of order n < 0 does not make sense."); return HermitePolynomial(nn, z); }
public ScalarValue Function(ScalarValue n, ScalarValue alpha, ScalarValue beta, ScalarValue z) { var nn = n.GetIntegerOrThrowException("n", Name); if (nn < 0) throw new Exception("Jacobi polynomial of order n < 0 does not make sense."); return JacobiPolynomial(nn, alpha, beta, z); }
public ScalarValue Function(ScalarValue n, ScalarValue x) { var nn = n.GetIntegerOrThrowException("n", Name); if (nn < 0) throw new Exception("Chebyshev polynomial of order n < 0 does not make sense."); var f = GetPolynom(nn); return new ScalarValue(f(x.Re)); }
static void FftKernel(ScalarValue x0, ScalarValue x1, out ScalarValue y0, out ScalarValue y1) { double a0 = x0.Re; double b0 = x0.Im; double a1 = x1.Re; double b1 = x1.Im; y0 = new ScalarValue(a0 + a1, b0 + b1); y1 = new ScalarValue(a0 - a1, b0 - b1); }
public ScalarValue Function(ScalarValue l, ScalarValue m, ScalarValue theta, ScalarValue phi) { var nn = l.GetIntegerOrThrowException("l", Name); if (nn < 0) throw new Exception("Spherical harmonics of order l < 0 does not make sense."); var nm = m.GetIntegerOrThrowException("m", Name); return Ylm(nn, nm, theta.Re, phi.Re); }
ScalarValue GetValue(int n, ScalarValue z) { if (n == 1) return z.Arctan(); else if (n == 0) return z / (1.0 + z * z); var iz = z * ScalarValue.I; return IMAGONEHALF * (PolyLogFunction.Polylog(n, iz) - PolyLogFunction.Polylog(n, -iz)); }
public MatrixValue Function(ScalarValue s, MatrixValue Z) { var n = s.GetIntegerOrThrowException("s", Name); var M = new MatrixValue(Z.DimensionY, Z.DimensionX); for (var j = 1; j <= Z.DimensionY; j++) for (var i = 1; i <= Z.DimensionX; i++) M[j, i] = GetValue(n, Z[j, i]); return M; }
/// <summary> /// Returns the result of the product x = a * x, with a complex a and a complex vector x. /// </summary> /// <param name="alpha">Some arbitrary complex scalar.</param> /// <param name="store">The complex vector x.</param> /// <param name="offset">The offset in the vector x.</param> /// <param name="stride">The offset between two elements in x.</param> /// <param name="count">The number of elements to consider (from x).</param> public static void cScal(ScalarValue alpha, ScalarValue[] store, int offset, int stride, int count) { int n = 0; int i = offset; while (n < count) { store[i] *= alpha; n++; i += stride; } }
public MatrixValue Function(ScalarValue from, ScalarValue to, ScalarValue count) { var c = count.GetIntegerOrThrowException("count", Name); if (c < 2) { throw new ArgumentException("linspace"); } var step = (to.Re - from.Re) / (c - 1); return new RangeValue(from.Re, to.Re, step); }
public ScalarValue Function(ScalarValue z) { var m = new Mandelbrot(); return(new ScalarValue(m.Run(z.Re, z.Im))); }
public ContourPlotValue Function(MatrixValue X, MatrixValue Y, MatrixValue Z, ScalarValue n) { var nn = n.GetIntegerOrThrowException("n", Name); var plot = new ContourPlotValue(); plot.AddPoints(X, Y, Z); plot.SetLevels(nn); return(plot); }
protected override ScalarValue GetValue(ScalarValue value) { return(Zeta.RiemannZeta(value)); }
/// <summary> /// Creates a new scalar value from the given one. /// </summary> /// <param name="value">Copies the contents of the given value.</param> public ScalarValue(ScalarValue value) : this(value._real, value._imag) { }
protected override ScalarValue GetValue(ScalarValue z) { var zi = 1.0 / z; return((zi + (zi + 1.0).Sqrt() * (zi - 1.0).Sqrt()).Ln()); }
public ComplexPlotValue Function(FunctionValue f, ScalarValue min, ScalarValue max) { return(Plot(f, min.Re, max.Re, min.Im, max.Im)); }
public MatrixValue Function(ScalarValue start, ScalarValue end, ScalarValue count) { return(Function(start, end, count, new ScalarValue(10))); }
public FunctionValue Function(MatrixValue Y, ScalarValue nbins, ScalarValue nParameters) { var nn = nbins.GetIntegerOrThrowException("nbins", Name); var nP = nParameters.GetIntegerOrThrowException("nParameters", Name); var N = Y.Length; var min_idx = Y.Min(); var min = Y[min_idx.Row, min_idx.Column]; var max_idx = Y.Max(); var max = Y[max_idx.Row, max_idx.Column]; var median = YMath.Median(Y); var variance = ScalarValue.Zero; var mean = Y.Sum() / Y.Length; for (var i = 1; i <= Y.Length; i++) { variance += (Y[i] - mean).Square(); } variance /= Y.Length; var delta = (max - min) / nn; var x = new MatrixValue(nn, 1); for (var i = 0; i < nn; i++) { x[i + 1] = min + delta * i; } var histogram = new HistogramFunction(); var fx = histogram.Function(Y, x); var linearfit = new LinfitFunction(Context); var dist = linearfit.Function(x, fx, new FunctionValue((context, argument) => { var _x = (argument as ScalarValue - median / 2) / (variance / 4); var _exp_x_2 = (-_x * _x).Exp(); var result = new MatrixValue(1, nP - 1); for (var i = 0; i < nP - 1; i++) { result[i + 1] = _exp_x_2 * _x.Pow(new ScalarValue(i)); } return(result); }, true)); var norm = Y.Length * (max - min) / nbins; var normed_dist = new FunctionValue((context, argument) => { var temp = dist.Perform(context, argument); if (temp is ScalarValue) { return(((ScalarValue)temp) / norm); } else if (temp is MatrixValue) { return(((MatrixValue)temp) / norm); } throw new YAMPOperationInvalidException(); }, true); return(normed_dist); }
public ScalarValue Function(ScalarValue order, ScalarValue argument) { var n = order.GetIntegerOrThrowException("order", Name); return(GetValue(n, argument.Re)); }
protected override ScalarValue GetValue(ScalarValue value) { return(new ScalarValue(Bessel.j1(value.Re))); }
protected override ScalarValue GetValue(ScalarValue value) { return(1.0 / value.Sin()); }
public MatrixValue Function(ScalarValue x0, ScalarValue xn, ScalarValue y0, ScalarValue yn, ScalarValue xsteps, ScalarValue ysteps) { var xs = xsteps.GetIntegerOrThrowException("xsteps", Name); var ys = ysteps.GetIntegerOrThrowException("ysteps", Name); var m = new Mandelbrot(); return(m.CalculateMatrix(x0.Re, xn.Re, y0.Re, yn.Re, xs, ys)); }
public MatrixValue Function(ScalarValue start, ScalarValue end, ScalarValue count, ScalarValue basis) { var c = count.GetIntegerOrThrowException("count", Name); if (c < 2) { throw new ArgumentException("logspace"); } var s = (end.Re - start.Re) / (c - 1); var r = new RangeValue(start.Re, end.Re, s); return(MatrixValue.PowSM(basis, r)); }
protected override ScalarValue GetValue(ScalarValue value) { return(2.0 / (value.Exp() - (-value).Exp())); }
public ComplexPlotValue Function(FunctionValue f, ScalarValue minX, ScalarValue maxX, ScalarValue minY, ScalarValue maxY) { return(Plot(f, minX.Re, maxX.Re, minY.Re, maxY.Re)); }
protected override ScalarValue GetValue(ScalarValue value) { return(new ScalarValue(value.Arg())); }
public MatrixValue Function(ScalarValue rows, ScalarValue cols, ScalarValue theta, ScalarValue k) { var n = rows.GetIntegerOrThrowException("rows", Name); var l = cols.GetIntegerOrThrowException("cols", Name); var m = new MatrixValue(n, l); for (var i = 1; i <= l; i++) { for (var j = 1; j <= n; j++) { m[j, i] = new ScalarValue(Gamma(theta.Re, k.Re)); } } return(m); }
protected override ScalarValue GetValue(ScalarValue value) { return(value.Conjugate()); }
public override ScalarValue Compare(ScalarValue left, ScalarValue right) { return(new ScalarValue(left == right)); }
public MatrixValue Function(ScalarValue rows, ScalarValue cols, ScalarValue mu, ScalarValue b) { var n = rows.GetIntegerOrThrowException("rows", Name); var l = cols.GetIntegerOrThrowException("cols", Name); var m = new MatrixValue(n, l); for (var i = 1; i <= l; i++) { for (var j = 1; j <= n; j++) { m[j, i] = new ScalarValue(Laplace(mu.Re, b.Re)); } } return(m); }
/// <summary> /// Implementation of the operation. /// </summary> /// <param name="left">The left scalar.</param> /// <param name="right">The right scalar.</param> /// <returns>The result of the operation.</returns> public abstract ScalarValue Operation(ScalarValue left, ScalarValue right);
public MatrixValue Function(ScalarValue dim) { var k = (int)dim.Re; return(MatrixValue.Ones(k, k)); }
public MatrixValue Function(ScalarValue n) { var d = n.GetIntegerOrThrowException("n", Name); return(Generate(d, d)); }
static ScalarValue GetValue(ScalarValue value, Double newBase) { return(value.Log(newBase)); }
public MatrixValue Function(ScalarValue rows, ScalarValue cols) { return(Function(rows, cols, new ScalarValue(1.0))); }
protected override ScalarValue GetValue(ScalarValue value) { return(new ScalarValue(double.IsInfinity(value.Re) || double.IsInfinity(value.Im))); }
public ScalarValue Function(ScalarValue a, ScalarValue b) { return(a / b); }
protected override ScalarValue GetValue(ScalarValue value) { return(value.Log10()); }
public ScalarValue Function(ScalarValue x, ScalarValue y) { var m = new Mandelbrot(); return(new ScalarValue(m.Run(x.Re, y.Re))); }
public MatrixValue Function(ScalarValue dim) { return(Function(dim, dim)); }