CodeGeneration - Maple Programming Help

Online Help

All Products    Maple    MapleSim


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

CodeGeneration

  

JavaScript

  

translate Maple code to JavaScript code

 

Calling Sequence

Parameters

Description

Examples

Compatibility

Calling Sequence

JavaScript(x, cgopts)

Parameters

x

-

expression, list, rtable, procedure, or module

cgopts

-

(optional) one or more CodeGeneration options

Description

• 

The JavaScript(x, cgopts) calling sequence translates Maple code to JavaScript code.

  

- If the parameter x is an algebraic expression, then a JavaScript 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 JavaScript statements assigning the elements to a JavaScript 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, this is understood as a sequence of assignment statements.  In this case, the equivalent sequence of JavaScript assignment statements is generated.

  

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

  

- If x is a module, then a JavaScript class is generated, as described on the JavaScriptDetails 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 JavaScript in particular, see JavaScriptDetails.

Examples

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

withCodeGeneration:

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

JavaScriptx+yz2xz,resultname=w

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

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

JavaScriptx,2y,5,z,resultname=w

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

Translate a computation sequence.  Optimize the input first.

css=1.0+x,t=lnsexpx,r=expx+xt:

JavaScriptcs,optimize

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

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

sJavaScriptx+y+1,declare=x::float,y::'integer',output=string

scg = x + y + 1;

(1)

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:

JavaScriptf,defaulttype=integer

function f(x, y, z) {
  return(y * x - 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:

JavaScriptf

function f(n) {
  var x;
  var i;
  var cgret;
  x = 0.0e0;
  for (i in 1...n)
  {
    x = x + i;
    cgret = x;
  }
  return(cgret);
}

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

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

JavaScriptf

function f(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:

JavaScriptm,resultname=t0

var m = {
  "p": function(x, y) {
      if (0 < y)
      {
        return(trunc(x));
      }
      else
      {
        return(Math.ceil(x));
      }
  },
  "q": function(x) {
      return(Math.pow(Math.sin(x), 2));
  }
}

Translate a linear combination of hyperbolic trigonometric functions.

JavaScript2coshx7tanhx

cg0 = 2 * (Math.exp(x) + Math.exp((-0.1e1) * x)) / 0.2e1 - 7 * (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:

JavaScriptf

function f(a, p) {
  printf("The integer remainder of %d divided by %d is: %d\n", a, p, a % p);
}

Compatibility

• 

The CodeGeneration[JavaScript] command was introduced in Maple 2015.

• 

For more information on Maple 2015 changes, see Updates in Maple 2015.

See Also

CodeGeneration

CodeGenerationOptions

JavaScriptDetails

trademark

TranslationDetails