This is an extension to my C++ Linear Algebra Library. The primary function that sets this framework apart from the original library is that it is capable of calculating expressions.
Even inside of an expression, vectors and matrices are supported. There are a few important details to note about syntax.
vectors:
• vectors are always inside < >
• all vectors must be in R3
• all vectors must have exactly three values
• when values are separated by commas, the vector is in rectangular format
• when values are separated by semicolons, the vector is in spherical
format
• values can be any expression that evaluates to a single scalar
matrices:
• matrices are always inside [ ]
• matrices are entered in row major order
• commas separated columns
• semicolons separate rows
• values can be any expression that evaluates to a single scalar
operators:
Quite a few operators are currently supported. Following is a list of operators and the associated character that triggers the operation:
scalar/scalar:
add |
+ |
| subtract | - |
| multiply | * |
| divide | / |
| exponent | ^ |
| factorial | ! |
scalar/vector:
multiply |
* |
| divide | / |
scalar/matrix:
multiply |
* |
| divide | / |
| exponent | ^ |
vector/vector:
add |
+ |
| subtract | - |
| cross product | * |
| dot product | • |
| angle | @ |
matrix/matrix:
add |
+ |
| subtract | - |
| multiply | * |
| transpose | ' |
matrix/vector:
multiply |
* |
Notes on operators:
• spaces are generally ignored
• simple negation of non-scalar objects is not supported
• trigonometry is not yet supported
• variables are not yet supported
• matrix exponents are truncated to integers
• any time an operator is missing, a '*' is assumed.
As such, all of the following are true:
• implied multiplication is supported
• cross product is assumed for vectors in implied multiplication.
• spaces between values can imply multiplication.
Coding practice:
Usage of the expression evaluator requires very little coding. The class function +expressionWithString: takes in an NSString and returns the heirarchy of laExpression objects that is used to find the result of the expression. To retrieve the value of the expression, use the laValue function -getType to determine whether the expression evaluates to a double, vector, or a matrix, and then use the appropriate laValue function to retrieve the result. For example:
NSString *input = [NSString stringWithString:@"1+1"];
laExpression *expression = [laExpression expressionWithString: input];
if ([expression getType] == laDouble)
{
NSLog(@"%@ = %g",input,[expression doubleValue]);
}
should output:
1+1 = 2
to the console.
With version 2.1 of the framework, a few order of operators bugs have been worked out. Variables are now supported as are several functions. The functions are listed in detail in the read-me file for Vector Tools 1.0b4.
Variables are supported through the laVariableList class. If you intend to use variables, it would be a good idea to instantiate an laVariableList object yourself and pass that to [laExpression expressionWithString: input andListOfVariables: theList] when you parse expressions.
If you have a request for support of other operators/functions, please email me.
Note: this code is all provided as is. If you find/fix any bugs, please notify me. Otherwise, you can do whatever you want with the code. There are no related fees or royalties. The resulting code is not required to be open source. However, if you do use this code for any publicly released product, I do ask that you give me credit where credit is due and that you notify me (so that I know who thinks my code is reliable and worthy of use).
If you find my code useful, I would greatly appreciate a donation.
This page was last edited on 2/21/2004.