public static void Main(string[] args) { IntIntToDoubleFunction ptr = NthPower; int x = 10, y = 3; Console.WriteLine($"{x}^{y} = {ptr(x, y)}"); // whoa. ptr = NthRoot; Console.WriteLine($"{y}th root of {x} = {ptr(x, y)}"); // delegates are first class citizens: they can be assigned to variables, // passed as parameters, returned from functions. double res = ApplyFunction( new IntIntToDoubleFunction(Product), // this is the OLD C# syntax x, y); // Newer versions of C# create the "new IntIntToDoubleFunction" implicitly: res = ApplyFunction(Product, x, y); }
//------------------------------------------------------------------------- /// <summary> /// Obtains an instance with entries filled using a function. /// <para> /// The function is passed the row and column index, returning the value. /// /// </para> /// </summary> /// <param name="rows"> the number of rows </param> /// <param name="columns"> the number of columns </param> /// <param name="valueFunction"> the function used to populate the value </param> /// <returns> a matrix initialized using the function </returns> public static DoubleMatrix of(int rows, int columns, IntIntToDoubleFunction valueFunction) { if (rows == 0 || columns == 0) { return(EMPTY); } //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: double[][] array = new double[rows][columns]; double[][] array = RectangularArrays.ReturnRectangularDoubleArray(rows, columns); for (int i = 0; i < array.Length; i++) { double[] inner = array[i]; for (int j = 0; j < inner.Length; j++) { inner[j] = valueFunction(i, j); } } return(new DoubleMatrix(array, rows, columns)); }
public static double ApplyFunction(IntIntToDoubleFunction fn, int x, int y) { return(fn(x, y)); }