The HyperMath interpreter can be accessed via batch mode from Templex scripts. There are special constructs in the Templex language for the interface. Data can be passed between the two thru these constructs.
The general approach for exchanging data between HyperMath and Templex is:
• | Export Templex data to HyperMath via memory. |
• | Issue commands from Templex to process the data in HyperMath. |
• | Extract the results from HyperMath into Templex via memory. |
The data types supported are:
• | Scalar |
• | Vector/Array |
• | Matrix |
Complex numbers are not supported in Templex. As a result, complex data must be extracted from HyperMath by its real and imaginary parts separately.
The Templex language constructs for interfacing with HyperMath are listed below.
Evaluates an expression in HyperMath.
HMathEval(expression) |
|
expression |
A string containing a valid HyperMath expression. |
Exports the Templex variables to HyperMath via memory. The data are not copied to conserve memory. Equivalent variables are created in HyperMath.
HMathExportVars(var1, var2, …) |
|
var1, var2… |
The name of the Templex variables for export, each represented by a string and separated by a comma. |
Extracts the imaginary parts of the HyperMath variable contents into Templex via memory. The variable must be in either HyperMath base or global scope. If the variable content is purely real, it returns zero. Once it is extracted, the variable ceases to exist in HyperMath. Additional extraction causes an error. If both real and imaginary parts are needed from a HyperMath variable, the two parts need to be stored in separate variables in HyperMath and extracted individually.
out = HMathGetVarImaginary(var) |
|
var |
A string containing the name of the HyperMath variable. The variable can be real or complex. |
out |
The Templex variable to which the contents are copied. |
Export two vectors to HyperMath in Templex and return the average of the two. {v1={1,2,3}} {v2={10,20,30}} {HMathExportVars("v1","v2")} {HMathEval("avg = (v1+v2)/2")} {out = HMathGetVarReal("avg")} {out} The first two lines create two vectors. The third line exports the vectors to HyperMath memory and creates two HyperMath variables by the same name. The fourth line performs the averaging in HyperMath by specifying the appropriate HyperMath expression. Note that the result is stored in a b variable named avg in HyperMath. This ensures that it is extractable from Templex as required. The next line extracts the contents of avg into the Templex variable out. The result is displayed in the last line. |
This example performs similarly as the previous example, but it assumes the math is implemented in a HyperMath function stored in a script file (demo.hml). The calls to HyperMath are made from a Templex function that takes two vectors as input and returns the average. Below are the contents of the HyperMath script file demo.hml: function GetAvg(v1, v2) avg = (v1 + v2)/2; return avg; end This is the Templex function: function AvgCurves(c1,c2) { {HmathEval("env::include(\"C:/hmath/demo.hml\")")} {HMathExportVars("c1","c2")} {HMathEval("result = GetAvg(c1,c2)")} {out = HMathGetVarReal("result")} return out } First, the Templex function loads the HyperMath function so that its contents (the function, in this case) are available in HyperMath memory. It assumes the file is located in the folder c:/hmath. Note the backward slashes (\) in front of the inner quotes ("). Next, it exports the two vectors to HyperMath memory. Then, it executes the HyperMath function by passing the two vectors and storing the result in the variable result in HyperMath. Finally, it extracts that variable into Templex memory and returns it. |
Extracts the real parts of the HyperMath variable contents into Templex via memory. The variable must be in either HyperMath Base scope. Once it is extracted, the variable ceases to exist in HyperMath. Additional extraction causes an error.
out = HMathGetVarReal(var) |
|
var |
A string containing the name of the HyperMath variable. The variable can be real or complex. |
out |
The Templex variable to which the contents are copied. |