The functions for manipulating lists can also operate on most expressions. Be sure to quote the expression with Expr(). For example:
Remove( Expr( A + B + C + D ), 2 ); // results in the expression A + C + D
b = Substitute( Expr( Log( 2 ) ^ 2 / 2 ), 2, 3 ); // results in the expression Log( 3 ) ^ 3 / 3
As with lists, remember that the first argument for in-place functions must be an L-value. An L-value is an entity such as a global variable whose value can be set. In-place functions are those that operate on lists or expressions directly. They have From or Into in their names (for example, Remove From and Insert Into). They do not return a result; you have to show the expression to see the result.
polynomial = Expr( a * x ^ 2 + b * x + c );
Insert Into( polynomial, Expr( d * x ^ 3 ), 1 );
Show( polynomial );
polynomial = d * x ^ 3 + a * x ^ 2 + b * x + c
For the not-in-place functions, you must either state the expression directly or else quote a name that evaluates to an expression using NameExpr. Such functions do not have From or Into in their names. They return manipulated lists or expressions without changing the original list or expression given in the first argument.
cubic = Insert( Expr( a * x ^ 2 + b * x + c ), Expr( d * x ^ 3 ), 1 );
d * x ^ 3 + a * x ^ 2 + b * x + c
quadratic = Expr( a * x ^ 2 + b * x + c );
cubic = Insert( Name Expr( quadratic ), Expr( d * x ^ 3 ), 1 );
d * x ^ 3 + a * x ^ 2 + b * x + c
Substituting is extremely powerful; please review the earlier discussion Substituting. Here are a few notes regarding substituting for expressions.
Substitute(pattern,name,replacement) substitutes for names in expressions
NameExpr() looks through the name but copies instead of evaluates:
a = Expr(
Distribution( Column( x ), Normal Quantile Plot )
);
Show( Name Expr( a ) );
Name Expr(a) = Distribution(Column(x), Normal Quantile Plot);
Substitute() evaluates all its arguments, so they must be quoted correctly:
b = Substitute( Name Expr( a ), Expr( x ), Expr( :weight ) );
Show( Name Expr( b ) );
Name Expr(b) = Distribution(Column(:weight), Normal Quantile Plot);
SubstituteInto() needs an L-value, so the first argument is not quoted:
Substitute Into( a, Expr( x ), Expr( :weight ) );
Show( Name Expr( a ) );
Name Expr(a) = Distribution(Column(:weight), Normal Quantile Plot);
Substitute() is useful for changing parts of expressions, such as in the following example that tests the Is functions:
data = {1, {1, 2, 3}, [1 2 3], "abc", x, x( y )};
ops = {is number, is list, is matrix, is string, is name, is expr};
m = J( N Items( data ), N Items( ops ), 0 );
test = Expr(
m[r, c] = _op( data[r] )
);
For( r = 1, r <= N Items( data ), r++,
For( c = 1, c <= N Items( ops ), c++,
Eval( Substitute( Name Expr( test ), Expr( _op ), ops[c] ) )
)
);
Show( m );
m =
[1 0 0 0 0 0,
0 1 0 0 0 1,
0 0 1 0 0 0,
0 0 0 1 0 0,
0 0 0 0 1 1,
0 0 0 0 0 1];
You can use SubstituteInto() to have JMP solve quadratic equations. The following example solves 4x2 - 9 = 0:
/* FIND THE ROOTS FOR THE EQUATION: */
/* a*x^2 + b*x + c = 0 */
// The quadratic formula is x=(-b + - sqrt(b^2 - 4ac))/2a.
// Use a list to store both the + and - results of the +- operation
x = {Expr(
(-b + Sqrt( b ^ 2 - 4 * a * c )) / (2 * a)
), Expr(
(-b - Sqrt( b ^ 2 - 4 * a * c )) / (2 * a)
)};
Substitute Into( x, Expr( a ), 4, Expr( b ), 0, Expr( c ), -9 );
// Plug in the coefficients
Show( x ); // see the result of substitution
Show( Eval Expr( x ) ); // see the solution
x = {Expr(((-0) + Sqrt(0 ^ 2 - 4 * 4 * -9)) / (2 * 4)), Expr(((-0) - Sqrt(0 ^ 2 - 4 * 4 * -9)) / (2 * 4))};
Eval Expr(x) = {1.5, -1.5};
The functions for manipulating lists and expressions are discussed in the previous section, Manipulating lists, and summarized in Table 7.4.
Remove |
x = Remove(list |expr) x = Remove(list |expr, position) x = Remove(list |expr, {positions}) x = Remove(list |expr, position, n) |
Copies the list or expression, deleting the item(s) at the indicated position. If position is omitted, items are deleted from the end. Position can be a list of positions. An extra argument, n, deletes n items instead of just 1.
|
Remove From |
Remove From(list |expr, position) Remove From(list |expr) Remove From(list |expr, position, n) |
|
Insert |
x = Insert(list |expr, item, position) x = Insert(list |expr, item) |
Inserts a new item into the list or expression at the given position. If position is not given, it is inserted at the end.
|
Insert Into |
Insert Into(list |expr, item, position) Insert Into(list |expr, item) |
|
Shift |
x = Shift(list |expr) x = Shift(list |expr, n) |
Shift an item or n items from the front to the back of the list or expression. Shift items from back to front if n is negative.
|
Shift Into |
Shift Into(list |expr) Shift Into(list |expr, n) |
|
Reverse |
x=Reverse(list |expr) |
|
Reverse Into |
Reverse Into(list |expr) |
|
Sort List |
x=Sort List(list |expr) |
Sort the elements of a list or the terms of an expression. Numbers sort low, followed by the name value of names, strings, or operators. For example 1+2 is lower than 1-2 because the name value Add sorts lower than the name value Subtract. {1,2} sorts lower than {1,3}, which sorts lower than {1,3,0}. {1000} sorts lower than {“a”}, but {a} and {“a”} sort as equal.
|
Sort List Into |
Sort List Into(list |expr) |
|
Sort Ascending |
Sort Ascending(list |matrix) |
|
Sort Descending |
Sort Descending(list |matrix) |
|
Loc Sorted |
Loc Sorted(A, B) |
Creates a matrix of subscript positions where the values in matrix A match the values in matrix B. A must be a matrix sorted in ascending order.
|
Substitute |
R = Substitute(list |expr, Expr(pattern), Expr(replacement), ...) |
Finds all matches to the pattern in the list or expression, and replaces them with the replacement expression. Each pattern must be a name. The second and third arguments are evaluated before they are applied, so most of the time you must quote them with an Expr function. To delay evaluating an argument, use Name Expr instead of Expr. You can list multiple pattern-replacement pairs for multiple substitutions in one statement.
|
Substitute Into |
Substitute Into(list |expr, Expr(pattern), Expr(replacement), ...) |