General HydroStatics
Ship Stability Software
Command of the Week
(New or interesting aspects of GHS that you may not know about)

Array Variables

For users unfamiliar with programming concepts, an array variable is simply a multi-dimensional variable in which multiple values can be stored and accessed via indexing. For example, think of that spreadsheet you're working with: the workspace can be considered a two-dimensional array, where each value in the array is referenced by its row and column index. Not only are arrays a compact way to store data, but they enable efficient algorithms which may be difficult to implement otherwise.

In GHS, array variables are simply a collection of regular user variables with a common prefix. For example, to create a 1x10 "array" one could use:

macro make_array
 variable %2_%1
/
.make_array(10,1)1 "A"

...where the macro make_array is being called 10 times, each time incrementing the first parameter (%1) by 1 starting at 1. The second parameter (%2) is the array variable prefix. %1 acts as the index, and is appended to the variable name on each loop to create a unique variable name. If we want to look at our collection of array variables, we might use:

variable /list:A_*

...which would list all variables that start with "A_", as yet undefined:


We can then SET, access, or otherwise manipulate any element of an array by specifying its index as part of its name:

macro count
 set %2_%1=%1
/
.count(10,1)1 "A"
set A_10={A_6} TIMES {A_7}
\\The answer = {A_10}\


But what about higher dimensional arrays? Easy, just append a second index:

macro make_array2
 macro make_column
  variable %3_%%92_%%91=%4
 //
 macro make_row
  .make_column(%2,1)1 %%91
 //
 .make_row(%1,1)1
/
.make_array2 6 7 "A" 0 `6x7 array "A" initialized to 0

But what about matrix operations, like Gaussian elimination and LU decomposition? Not as easy, but we've done it. Just let us know if you're brave enough to try it too.

Questions, comments, or requests?
Contact Creative Systems, Inc.

support@ghsport.com

USA phone: 360-385-6212 Fax: 360-385-6213
Office hours: 7:00 am - 4:00 pm Pacific Time, Monday - Friday

Mailing address:
PO Box 1910
Port Townsend, WA 98368 USA

www.ghsport.com

Click here for an index to this and previous COWs