translate Maple code to C# code - Maple Help

Online Help

All Products    Maple    MapleSim

Home : Support : Online Help : Programming : Input and Output : Translation : CodeGeneration/CSharp

CodeGeneration[CSharp] - translate Maple code to C# code

Calling Sequence

CSharp(x, cgopts)




expression, list, rtable, procedure, or module



(optional) one or more CodeGeneration options



The CSharp(x, cgopts) calling sequence translates Maple code to C# code.


- If the parameter x is an algebraic expression, then a C# statement assigning the expression to a variable is generated.


- If x is a list, Maple Array, or rtable of algebraic expressions, then a sequence of C# statements assigning the elements to a C# array is produced.  Only the initialized elements of the rtable or Maple Array are translated.


- If x is a list of equations nm=expr where nm is a name and expr is an algebraic expression, then this is understood to be a sequence of assignment statements.  In this case, the equivalent sequence of C# assignment statements is generated.


- If x is a procedure, then a C# class is generated containing a function equivalent to the procedure, along with any necessary import statements.


- If x is a module, then a C# class is generated, as described on the CSharpDetails help page.


The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions.


For more information about how the CodeGeneration package translates Maple code to other languages, see Translation Details. For more information about translation to C# in particular, see CSharpDetails.


For a description of the options used in the following examples, see CodeGenerationOptions.


Translate a simple expression and assign to the name w in the target code.


w = -2 * x * z + y * z + x;

Translate a list and assign to an array with name w in the target code.


w[0,0] = x;
w[0,1] = 2 * y;
w[1,0] = 5;
w[1,1] = z;

Translate a computation sequence.  Optimize the input first.



s = 0.10e1 + x;
t1 = Math.Log(s);
t2 = Math.Exp(-x);
t = t1 * t2;
r = x * t + t2;

Declare that x is a float and y is an integer. Return the result in a string.


s:=cg = x + (System.Double) y + 0.1e1;


Translate a procedure.  Assume that all untyped variables have type integer.

f := proc(x, y, z) return x*y-y*z+x*z; end proc:


public class CodeGenerationClass {
  public static System.Int32 f (System.Int32 x, System.Int32 y, System.Int32 z)
    return x * y - y * z + x * z;

Translate a procedure containing an implicit return.  A new variable is created to hold the return value.

f := proc(n)
  local x, i;
  x := 0.0;
  for i to n do
    x := x + i;
  end do;
end proc:


public class CodeGenerationClass {
  public static System.Double f (System.Int32 n)
    System.Double x;
    System.Int32 i;
    System.Double cgret;
    x = 0.0e0;
    for (i = 1; i <= n; i++)
      x = x + (System.Double) i;
      cgret = x;
    return cgret;

Translate a procedure accepting an Array as a parameter.  Note that the indices are renumbered so that the CSharp array starts at index 0.

f := proc(x::Array(numeric, 5..7))
  return x[5]+x[6]+x[7];
end proc:


public class CodeGenerationClass {
  public static System.Double f (System.Double[] x)
    return x[0] + x[1] + x[2];

Translate a module.

m := module() export p; local q;
    p := proc(x,y) if y>0 then trunc(x); else ceil(x); end if; end proc:
    q := proc(x) sin(x)^2; end proc:
end module:


using System;

public class m {
  public static System.Int32 p (System.Double x, System.Int32 y)
    if (0 < y)
      return (System.Int32)(x);
      return (System.Int32)Math.Ceiling(x);
  private static System.Double q (System.Double x)
    return Math.Pow(Math.Sin(x), 0.2e1);

Translate a linear combination of hyperbolic trigonometric functions.


cg0 = 0.2e1 * (Math.Exp(x) + Math.Exp((-0.1e1) * x)) / 0.2e1 - 0.7e1 * (Math.Exp(0.2e1 * x) - 0.1e1) / (Math.Exp(0.2e1 * x) + 0.1e1);

Translate a procedure with no return value containing a printf statement.

f := proc(a::integer, p::integer)
  printf("The integer remainder of %d divided by %d is: %d\n", a, p, irem(a, p));
end proc:


using System;

public class CodeGenerationClass {
  public static void f (System.Int32 a, System.Int32 p)
    Console.WriteLine("The integer remainder of " + a + " divided by " + p + " is: " + a % p);

See Also

CodeGeneration, CodeGenerationOptions, CSharpDetails, TranslationDetails

Download Help Document

Was this information helpful?

Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam