Sei sulla pagina 1di 23

How to get started

1. HyperMath

HyperMath is an interpreted scripting language with a set of libraries designed for


numerical coding. It allows rapid numerical code development with minimal effort.
HyperMath can be accessed from GUI mode (ScriptView) as well as in batch mode.
HyperMath supports a matrix based data structure and includes many standard
constructs found in mainstream scripting languages. HyperMath is an easy to use
and intuitive language for authoring custom numerical code. It provides users access
to a set of math libraries and utilities for use in the code. Using ScriptView with
HyperMath allows users to do two-dimensional data visualization as well as
interactive debugging which allows for rapid code development. HyperMath also has
built-in CAE readers to give users access to a large set of data formats. Integration
with HyperWorks allows authored scripts to be directly executed from HyperWorks
Desktop and HyperStudy.

2. Work Environment (The ScriptView GUI)

In HyperMath, there are two different modes available: the Authoring, opened by
default (shown below) and the Debugging modes.

The various areas of the ScriptView GUI will be described below.

1
Toolbar Area:

The Toolbar Area is a collection of the available Toolbars in ScriptView. The File
Toolbar provides tools for creating, opening and saving script files.

There is also the Undo/Redo Toolbar which allows an action in the editor to be
reverted or a reverted action to be rejected.

The Bookmark Toolbar allows users to mark lines in the script file as bookmarks and
then navigate through those locations. This allows users to easily mark and access
different locations in their script file.

The Outline Toolbar provides different options for expanding and collapsing the
outline sections. These sections are located where the control structures (such as
while, if, and for loops) are located. There is also an option to turn the outlining
feature off.

While in the Authoring Mode, there is an Evaluate Toolbar which has a button to
evaluate the file (as shown in the image above). While in the Debugging Mode, this
changes to the Debugging Toolbar which controls the debugging session. These
options will be discussed in further detail later in the section on the Debugging Mode.

Editor:

2
The ScriptView Editor allows users to create or open script files for authoring and
execution. The Editor supports syntax highlighting, smart indentation, collapsible
sections, bookmarking, and displays the line numbers to the left.

Project Browser:

The Project Browser is where the current session contents are displayed in a
hierarchal tree. The Project Browser displays all open files, and plots within the
project. A project is at the top of the hierarchy. A session has a project by default. A
project may have one or more Scripts underneath it. Each Script groups a set of
script files and plots. Each Script has a default folder named Files where all files that
are in use in the session are listed. It also lists all undeleted plots in the folder Plots.
A plot can be removed by right-clicking on its name in the tree and selecting Delete.

Command Window:

The Command Window allows the execution of HyperMath language commands


and the TCL Window the TCL language commands. It also displays print command
outputs generated when scripts are executed from the editor.

3
File Browser:

The File Browser allows browsing folder contents, adding new files, and deleting
existing files. In addition, files can be opened in the editor by right-clicking on the
filename and selecting Open, double clicking on it, or dragging the file icon on the
editor. The displayed list can also be filtered by file types and/or matching patterns.
These options are accessible by clicking the Filter icon at the top right corner of the
browser window.

Library Manager:

4
The Library Manager lists the built-in math library in a categorized view. Click on the
Show/Hide Properties icon, , to display detailed information on the currently
selected library item.

Command History Window:

The Command History Window displays all the commands entered at the
Command Window by date since the last full erase. This allows for quick execution
of the commands by double-clicking on the desired command or by dragging and
dropping the command into the Command Window or Editor Window. Various
options are available by right-clicking on the items in this window.

Variable Browser:

The Variable Browser displays all the variables in the current session. This allows
the user to quickly identify the variable type (string, number, matrix etc), the scope
(main function or user function variable) and the value of the variables. The variables
can be copied, renamed or deleted.

There are additional items that are available in the Authoring Mode which are not
displayed by default. These are the following:

5
Property Editor:

The Property Editor displays information about the currently selected item in the
Project Browser. The information can be viewed categorically or alphabetically by
selecting the corresponding tab.

Bookmarks:

The Bookmarks tool lists all the bookmarks in the project. Each bookmark is listed
with its host file and the line number. Individual bookmarks can be disabled by
deactivating the box next to it. Bookmarks can also be created and deleted from this
window. Clicking a bookmark positions the cursor at that location in the editor. Use
the arrow keys in the Bookmarks tool to cycle through the bookmarks.

File Menu:

The File Menu provides access to tools such as saving, opening, and closing script
files and projects. It also allows for the file to be printed as well as provides a print
preview option. There is also a listing of the recent files and the recent projects to
allow for quick access to these files.

Edit Menu:

The Edit menu enables you to copy, past, overlay, and erase the contents of the
dockable windows. There is also access to a Preference dialog box where you can
set general preferences as well as preferences for the plot window and the editor
window.

6
Debug Menu:

While in the Authoring Mode, the Debug Menu allows users to start or stop the
debugging proecess.

View Menu:

The View Menu contains options for setting the Authoring or Debugging Mode and
turning on/off the Toolbars including the File, Undo/Redo, Evaluate, Bookmarks,
Outlining, and Debug toolbars. There are also options for placing the application in
full screen mode and options for wrapping the lines in the Editor window. Also, it
allows users to control the display of the Project Browser, Property Editor, Command
Window, Tcl Window, File Browser, Library Manager, Bookmarks, and Command
History Window.

Help Menu:

The Help Menu allows users to access the ScriptView help, the HyperMath help, and
help for other HyperWorks applications. There is also access to the release notes for
the current version as well as the version and copyright information for HyperWorks.

3. Writing a script (Authoring mode)

New script files can be created in a couple of different ways using ScriptView. Using
the File Toolbar, the File menu, and by right clicking on the File folder in the Project
Browser a new script file can be created.

The File Toolbar is used to open a HyperMath script file by simply clicking on New
File in the File Toolbar. This opens a new HyperMath script file called Untitled
(followed by a number), for example Untitled2.hml.

7
Using the menu options, you can also open a new file by going to File > New . This
provides the user with an option of creating a HyperMath file (.hml), a Glue file (.glu),
a Tcl File (.tcl), or a Text file (.txt). After selecting the file type and clicking OK, the
blank file opens in the Editor window with the appropriate extension.

A new file of any supported type can be created directly from the Project Browser by
right-clicking on the Files folder of a Script and selecting Add New File from the
context menu. The file is added to the Script from which the Files folder was
selected.

Opening Existing Files

Just as with creating new files, opening existing files can be done using the File
Toolbar, the File menu, and the Project Browser. Using the File Toolbar, simply
click on Open File and a dialog box will open which allows the user to browse and to
open a file.

8
Using the File Menu, select Open and this will also provide a dialog box to open a
file. The Project Browser can be used by right-clicking on the Files folder of a Script
and selecting Add Existing File from the context menu. The file is added to the
Script from which the Files folder was selected.

Saving Files

Saving files is done through either the File Toolbar or the File Menu. To save a file
through the File Toolbar, simply click on the Save File icon. You can also save each
file which has been modified by using the Save All Modified Files icon (both shown
below).

Using the File menu, the user has the options of Save, Save as, or Save All. Save
will save the current file with the name provided. Save as will provide a dialog for the
user to select where to save the file and to also specify a filename. Save All will save
all the modified files in the session with their current names.

4. HyperMath Language

4.1. Data Types

The following data types are supported in HyperMath.


 Numbers
 Strings
 Complex Data
 Boolean
 Vector and Matrix
 Tables
 Nil Values

9
Numbers
All numbers in HyperMath are double precision. HyperMath allows arithmetic using
operators to add, subtract, multiply, and divide numbers. The following example uses
the print() function to print the results of the calculations.
>> print(4+2)
6
>> print(8*8)
64
Numbers in HyperMath can also be entered as exponent types, as shown in the
following example.
>> print(1.234e5)
123400

You can also assign values to variables using the “equal’s to” operator, =. Also,
variable names are case sensitive. Below is an example where the variable x is
created and then the value is printed.
>> x = 7
>> print(x)
7
You can also use the value of a variable for calculations. In the following example,
the variable x is used in a calculation and the function print() is used to report the
result of the calculation.
>> print(x*2)
14

Strings
In addition to numbers, HyperMath has strings, or text, as variable types. Below is an
example of how to assign strings to variables.
>> who=”HyperMath user”
>> print(who)
HyperMath user
Notice how the string is enclosed in quotations.
Also, two strings can be joined together using the + operator between the two strings.
>> print(“hello”)
hello
>> print(“hello ” + who)

10
hello HyperMath user

The .. operator can also be used to join strings, as shown below.


>> print(“hello ” .. who)
hello HyperMath user

C-like escape sequences can also be used. These include \n for a new line and \t
for a tab. The example below illustrates a situation where both of these escape
sequences are used.
>> print(“hello\nNew line\tTab”)
hello
New line Tab
Complex Data Types
The letter “i” is used to indicate the imaginary part of the complex number.
Arithmetic operations are also supported on complex data types, and complex and
non-complex data types can be mixed as shown below.
>> x=1+2i; print(x)
1+2i
>> y=1+2i; print(x+y)
2+4i
>> print(x+9)
10+2i

Boolean Data Type


Boolean data types have a value of either true or false. If a value is not true, it has to
be false. If a value is not false, it has to be true. The not operator can be used with
Boolean data types to reverse the value.
>> x = true
>> print(x)
true
>> print(not x)
false
>> print(not false)
true

11
Boolean values are used to represent the results of logic tests, such as is something
equal (==) or not equal (~=) to something else. For the example below, tests are
being run to see if two numbers are equal or not equal.
>> print(1==0)
false
>> print(1==1)
true
>> print(1~=0)
true
One final point on Boolean data types, the numbers 1 and 0 equate to true and false,
respectively. Other numbers are not equated to either true or false.
>> print(1 == true)
true
>> print(0 == false)
true
>> print(2 == true)
false

Vector/Matrix
Either a 1-dimensional or 2-dimensional matrix type exists. The 1-dimensional type is
referred to as a vector, while the 2-dimensional type is a matrix. Only numbers are
supported as entries and many of the standard matrix operations, such as inversion,
can be performed on this data type.
To define a matrix, a pair of square brackets, [ ], are used and each element is
separated by a comma. Below, a 1-dimensional vector is defined using the square
brackets and commas. The output from the print() command is the variable type
([Matrix]), matrix size (1x3), and then the variable values (1 2 3).
>> z=[1,2,3]
>> print(z)
[Matrix] 1x3
1 2 3

The above example defines a 1x3 matrix, but what if a 3x1 matrix is desired? To
create a 3x1 matrix, the entries are separated by a semicolon (;) instead of commas.
>> z=[1;2;3] ; print(z)
[Matrix] 3x1
1

12
2
3

Similarly, a 2-dimensional matrix is created using both commas and semicolons. The
commas separate the column entries, and semicolons separate the row entries. For
example, a 2x3 matrix is defined below.
>> z=[1,2,3;4,5,6] ; print(z)
[Matrix] 2x3
1 2 3
4 5 6

Accessing the elements in a matrix is done by using parenthesis (optionally a pair of


square brackets, [ ] [ ], can also be used). The variable name is entered first,
followed by the index of the row, and then followed by the index of the column. The
example below illustrates how to access the element in the second row, first column.
>> z(2,1)
4

In addition to numbers, matrices can have elements which are complex numbers.
Below is an example.
>> z=[1+2i, 4; 5, 3+3i] ; print(z)
[Matrix] 2x2
1+2i 4
5 3+3i

Tables (similar to structures)


Tables are an example of an aggregate data type supported by HyperMath. An
aggregate data type is used to store collections such as lists, sets, and arrays. These
data types can store associative arrays that contain other objects, such as numbers,
strings, or even other aggregates.
A table is defined using a pair of curly brackets, { }. The following illustrates how an
empty table is created. The result of the print() command is the variable type
(table) followed by a unique identifier. This is simply the table’s address in memory.
>> x = {}
>> print(x)
table: 0558E9E8

13
Tables can be created with mixed variable types, such as numbers and strings. The
example below creates a table with both numbers and strings. A comma separates
the table entries. The indexing of tables is zero based (meaning the first element is
indexed by zero). To access individual table entries, square brackets, [ ], are used
with the index value.
>> x = {123, “hello”}
>> print(x[0])
123
>> print(x[1])
hello

Although tables can store numbers, it is not recommended to use tables to do


numerical calculations. Instead, vector and matrix data types should be used to
perform mathematical operations.

Nil Values
If a variable has the value of nil, this means that the variable has no value and no
longer exists. A variable can be deleted by setting the value of the variable to nil.

4.2. Relational Expressions

Relational Expressions use relational operators which return the Boolean values of
true or false. The following relational operators are supported by HyperMath:
 == equal to
 ~= not equal to
 < less than
 > greater than
 <= less than or equal to
 >= greater than or equal to
Both numbers and string are supported with these operators.

4.3. Control Structures

A control structure is a decision making tool. It allows decision to be made such as


“run this fragment of code if X is true” or “do this a few times” or “do this until
something is no longer true”.

Control structures generally consist of more than one line. The Command Window
only supports commands which consist of a single line. As a result, the examples
shown below are all entered into the Editor Window and then the file is evaluated to

14
view the output. Note that examples that are meant for the Editor Window do not
have the command line prompt (>>) before the commands.

While loop

This statement has the form:

while exp do
block
end

where exp is a conditional expression and block is the code which is executed while
the conditional expression is true. A while loop can be exited using the break
command. In the following example, a simple loop is created.

global i = 3
while i>0 do
print(i)
i = i-1
end
3
2
1

Repeat command

This command has the following form:

repeat
block
until exp

where exp is the conditional expression. Notice that in the repeat command, the
conditional expression comes after the block. The following is a simple example of
the repeat command.

global i=3
repeat
print(i)
i=i-1
until i==0

3
2
1

15
The following is an example with the repeat command which also uses the break
command. Remember, the break command can exit a repeat loop.

i=1
repeat
print(i)
i=i+1
if i>3 then break
end
until i==10

1
2
3

For loop

In HyperMath, the syntax for a for loop is the following

for variable = from_exp, to_exp [, step_exp] do


block
end

where from_exp is the value the variable is set to before entering the for block.
The block continues to be entered until the variable value is greater than to_exp.
Each time the block is exited, the variable increases by the value step_exp.
step_exp is an optional parameter. If step_exp is not specified, the variable is
incremented by the value 1. Below are two examples; one where the variable
step_exp is not specified and one where it is.

for i=1,3 do print(i) end


1
2
3
for i=3, 1, -1 do print(i) end
3
2
1
The variable i in the above examples, is only local to the scope of the for loop. The
variable has a nil value once the loop is completed.
HyperMath also supports a syntax for the for loop which is similar to the for loop in
the C-programming type language. This syntax is:
for (initialization; conditional; step)
{
bock
}

16
Any of the expression's initialization, conditional, or step fields may be empty. The
initialization code executes once at the very beginning, setting a variable to a given
value. The conditional field is tested before each iteration (if it evaluates to false at the
beginning, the statement never executes). At the end of each loop, step executes
and updates the value of the variable.

If/Else statement
This statement has the following syntax.
if exp then
block
elseif exp then
block
else
block
end

This statement allows for a question to be asked. If the exp is true, then the block is
executed. If it isn’t true, it then moves to the next elseif block. If this statement
isn’t true, it finally moves to the else block and executes what is in that block. The
elseif and else parameters are optional parameters. This means the if block can
be used on its own, and the if and else blocks can be used alone.

HyperMath also supports syntax for the if statement which is similar to the C-
programming language. The syntax for this additional format is shown below.
if(exp)
{
block
}
elseif(exp)
{
block
}
else
{
block
}

17
4.4. Functions

Functions take arguments, and return values. The basic syntax for defining a function
is:
function function_name (args)
body
end

Once a function is created, it is considered to be a command. As such, it may be


called using its name, followed by a value for each of its arguments.

The following example shows a simple function called “foo”. This function receives a
single argument, n, and returns twice its value

function foo(n)
return n*2
end
print(foo(7))
14

When passing multiple arguments to a function, they should be separated by commas


and they are passed in the order defined in the function definition. If an argument is
not passed, it receives a default value of nil. If too many arguments are passed,
they are simply ignored. Also, any argument type, such as numbers and strings, can
be passed.

There is also the option of having a variable number of arguments available for a
function. This is done by using “…” as the argument for the function. Inside the
function, each argument is passed to the arg parameter in the order they appear.
For example, the first element being passed can be accessed using the following
code.

function foo(…)
print(arg[0])
end
foo(3,77)
3

Also, the number of arguments passed can be obtained by using arg.n. The
following example illustrates how to determine the number of arguments passed.
function foo(…)
print(“Number of arguments:”, arg.n)
end
foo(3,77)
Number of arguments: 2

18
HyperMath can also return more than one value from the function. This is done by
using the return command with a comma separating the list of values.

function foo(angle)
return Cos(angle), Sin(angle)
end
print(foo(1))
0.54030230586814 0.8414709848079

Multiple returned values can be assigned to variables in a simple manner as shown


below. The global variable a is assigned the first returned value and the global
variable b is assigned the second returned value. To illustrate that the variables were
assigned properly, the print() command is used to show the output.

function foo(angle)
return Cos(angle), Sin(angle)
end
print(foo(1))
a,b=foo(1)
print(“a=”, a)
print(“b=”, b)
0.54030230586814 0.8414709848079
a= 0.54030230586814
b= 0.8414709848079

Scoping Rules for Variables

By default, variables are defined as local to the scope in which they are declared.
Variables can also be defined explicitly in the global or local scope. This is done
using the keywords global or local, respectively. When a variable is defined in
the global scope, it is visible throughout the entire scope. When the variable is
defined as local, it is only visible within a given scope, which is the body it is defined
in. If the definition of the variable is in the main body, this is equivalent to defining a
global variable; the variable will be available throughout the script. Below is an
example using the variable a. a is defined locally in the main body and then the
variable is updated.

a = 0
a = 10
print(a)
10

Here is another example where the variable a is implicitly defined in the main body of
the script. A second instance of the variable a is defined locally inside of the function
foo. This creates a second instance of the same named variable and it is local to the

19
function, meaning it is unavailable outside of the function. Therefore, when the
print(a) command is used outside the function, the variable in the main body is
printed.

a = 0
function foo()
local a = 10
print(a)
end
foo()
10

>> print(a)
0

Notice how the explicitly defined local variable a is not available outside the function.
Also, it did not conflict with the variable a defined outside the function. Therefore, it is
recommended that variables intended for use entirely inside of a function be defined
explicitly as a local variable within that function.

It is possible to reference a global variable inside a function. In the example below,


the variable a is defined globally outside the function foo, but the variable is called
inside the function.

a=10
function foo()
print(a)
b=a+12
print(b)
end
foo()
10
22

There are also scopes within control structures. Control structures such as for,
while, repeat, and if-then-else have their own scopes.

20
4.5. Vector and matrices operations

Addition – Matrix addition is done by using the addition operator, +. Addition can be
done between matrices of the same dimensions as well as between a scalar and a
matrix. An example of both is shown below.

>> x=[1,2,3;4,5,6]; y=[10,20,30;40,50,60]


>> print(x+y)
[Matrix] (2x3)
11 22 33
44 55 66

>> print(x+10)
[Matrix] (2x3)
11 12 13
14 15 16

Subtraction – Matrix subtraction is similar to addition. It is done using the subtraction


operator, -. Subtraction can be done between matrices of the same dimensions as
well as between a scalar and a matrix. Below is an example of both.
>> x=[4,5,6]; y=[1,2,3]
>> print(x-y)
[Matrix] (1x3)
3 3 3
>> print(x-10)
[Matrix] (1x3)
-6 -5 -4

Multiplication – Matrix multiplication is done with the star, *, operator. When doing
matrix operation, the inner dimensions must be equal (meaning that when A*B, the
number of columns in A must be equal to the number of rows in B).
>> A=[10,20,30;40,50,60]; print(A)
[Matrix] (2x3)
10 20 30
40 50 60
>> B = [10;20;30]; print(B)

21
[Matrix] (3x1)
10
20
30

>> print(A*B)
[Matrix] (2x1)
1400
3200

Multiplication can also be done between a matrix and a scalar. Also, complex data is
supported within a matrix when doing multiplication.
Also, element wise multiplication is also supported. This is done by using the dot
notation (.*)
>> Z = [1,2,3]; Y = [3,2,1]; W=Z.*Y; print(W)
[Matrix] (1x3)
3 4 3

Matrices can also be raised to a power. For example, to raise each element is a
matrix to the power of 2, the .^ is used.
>> print(Z.^2)
[Matrix] (1x3)
1 4 9
Scalars can also be raised to a matrix. In such cases a matrix is created with each
element in it being the scalar raised to the power of each element in the input matrix.
If an integer is used in place of a variable for the base, it must be separated by a
space from the period, as shown in the following example.
>> print(2 .^Z)
[Matrix] (1x3)
1 4 8

Division – Matrix division is also supported in HyperMath. There are two types of
division; forward division (using the forward slash operator /) and backward division
(using the back slash operator \).
Forward division, such as A/B, is equivalent to A*Inv(B) where Inv refers to the
inverse. Also, B must be a square matrix and A and B must have the same number of
columns.

22
>> A=[1,3]; B=[1,3;2,4]
>> print(A/B)
[Matrix] (1x2)
1 0
Backward division, such as C\D, is equivalent to Inv(C)*D. In this case, C must be
square and C and D must have the same number of rows.
>> C=[1,2;3,4]; D=[4;5]
>> print(C\D)
[Matrix] (2x1)
-3
3.5

23

Potrebbero piacerti anche