HMath-2020: Working with HyperMath – Functions and Matrix Operators |
![]() |
![]() |
![]() |
![]() |
|
HMath-2020: Working with HyperMath – Functions and Matrix Operators |
![]() |
![]() |
![]() |
![]() |
In this tutorial, functions and matrix operators in the HyperMath scripting language will be examined. Also, local scoping rules will be explored.
Step 1: Launch HyperMath.
1. | From the Start menu, select All Programs > Altair HyperWorks > HyperMath. |
This launches HyperMath in the HyperMath GUI. Notice that by default, a file named Untitled1.hml exists in the Editor window. By default, the Authoring Mode is displayed as well.
Step 2: Using the Editor window, define a function which adds two matrices.
1. | In the Editor window, under the Untitled1.hml tab, enter the following line: |
function addition (m1,m2)
This begins a function named addition which has two variables passed to it. These variables are referenced in the function using m1 and m2.
2. | In the function addition, the variables will each be scaled by a value. The variable m1 will be scaled by 10 and the variable m2 will be scaled by 20. Add the following two lines to the function to do the scaling: |
m1=m1*10
m2=m2*10
Instead of creating new variables to assign the scaled values to, the same variable names are used.
3. | Next, a new variable will be declared which is local to the scope of the function. This variable will contain the sum of the two matrices defined earlier. Once the variable is created, the print() command will be used to report the value of the variable to the HMath window. |
sum=m1+m2
print("Inside the function sum is:", sum)
4. | To complete the function, the end command is used after the last command in the function. |
end
The entire function should look like the following:
5. | Save the file by going to File > Save As. Browse to the Desktop and save the file as Tutorial_2020.hml. |
The tab in the Editor window now says Tutorial_2020.hml.
6. | Go to the Project Browser. Right-click on the Files folder and select Add Existing File. Use the file browser to select Tutorial_2020.hml, saved in step 5. |
Tutorial_2020.hml is added to the Project Browser. Next to the tutorial name, there is a plus sign. Click the plus sign to expand the list. The function addition is listed under Tutorial_2020.hml.
Step 3: Define two matrices.
1. | Now that the function has been defined, the variables which will be passed to the function need to be defined. In this example, two matrices which have the same size will be passed. Both matrices are Base (i.e., defined outside functions) variables as defined below: |
mat1=[1,2,3;4,5,6]
mat2=[4,5,6;7,8,9] |
2. | Next, another Base variable called sum will be defined. This variable is defined to illustrate the difference between a local variable defined in a function and a Base variable. |
sum=10
This variable is different than the variable defined in the function addition. The variable sum inside the function addition is only pertinent within that function. Outside the function addition, this variable sum is set to the value of 10.
Step 4: Pass variables mat1 and mat2 to the function addition.
1. | To call the function addition and pass the variables mat1 and mat2, use the name of the function followed by the variables to be passed in parentheses: |
addition(mat1, mat2)
Even though we are passing the variables mat1 and mat2 to the function addition, when the function is executed, these variables will take on the names of m1 and m2 (as defined in the function).
2. | Next, use the print() command to print the value of the local variable sum: |
print("Outside the function sum is:", sum)
The complete script should look like the following:
3. | Finally, evaluate the file by clicking on the Run File icon, ![]() |
The print() command was used both inside and outside the function addition. The first print() command which is executed is the one inside of the function addition. This results in the 2X3 matrix which is printed to the HMath window.
The second print() command prints the variable sum which is defined outside of the function addition. The value 10 is printed to the HMath window as this is the value assigned to this variable. This illustrates the difference between local variables defined inside a function versus outside of a function.
4. | In the HMath window, type the following: |
print(mat1)
This results in the mat1 matrix being printed to the HMath window. This variable can be called now because it was defined as a Base variable in our script. The same is true with mat2.
Step 5: Defining with global variables.
1. | Global variables are declared explicitly by the keyword global and are accessible from anywhere. We will make the variable sum a global variable by declaring it as global both inside and outside the function. |
Note: | When defining a global element in HyperMath, you must include a semicolon (;) at the end of the expression (see images below as an example). |
2. | Insert the following in both locations as highlighted below: |
global sum;
3. | Finally, evaluate the file by clicking on the Run File icon, ![]() |
Step 6: Save the script Tutorial_2020.hml.
1. | From the File menu, click Save File to save the current file Tutorial_2020.hml. |
HMath-1000: Editing, Executing, Saving, and Plotting in HyperMath
HMath-1010: Working with HyperMath Authoring Mode
HMath-1020: Working with HyperMath Debugging Mode
HMath-2000: Working with HyperMath – Arithmetic and Relational Expressions and Control Structures
HMath-2010: Working with HyperMath – Logical and Relational Expressions and Control Structures
HMath-2030: Working with HyperMath – Plot Commands
HMath-3000: Working with HyperMath – String Library
HMath-3010: Working with HyperMath – Input/Output Library
HMath-3020: Working with HyperMath – Input/Output Library Continued
HMath-3030: Working with HyperMath – Batch Mode
HMath-4000: Using HyperMath Functions for Curve Fitting
HMath-4001: Using HyperMath for Material Characterization
HMath-4010: Solving Ordinary Differential Equations
HMath-4020: Solving Differential Algebraic Equations
HMath-4030: Optimization Algorithms in HyperMath
HMath-5000: Using HyperMath in HyperView Results Math
HMath-5001: Post Processing Results from FEA
HMath-5002: Registering a Function in HyperGraph 2D
HMath-5003: HyperMesh-HyperMath Cross Execution of a Tcl Script
HMath-5004: HyperMesh-HyperMath Cross-debugging of a Tcl Script