NSB Cosmic Center Image

N.S.B. Cosmic Center

NSB Cosmic Center Image

JavaScript & MathScript Interactive Document Generator-Demo-07

This document is generated by N.S.B. JavaScript & MathScript Interactive Document Generator version 001.

Introduction to N.S.B. JavaScript & MathScript Interactive Document Generator

MathScript Functions (cont.)

We continue exploring the MathScript functions that can be used for general symbolic manipulations.

The function join() can be used to combine the elements of two function with the same head. Let us first write and execute this:

input(1)=> join[f,g]
output(1)=> join[f, g]

Notice that f and g in the above are just symbols. So let us replace them with appropriate functions:

input(2)=> %/.{f=>F[a,b], g=>F[x,y,z]}
output(2)=> F[a, b, x, y, z]

So we obtained a function that joins F[a,b] and F[x,y,z], both functions have the same head. Needless to say that we can also join familiar math functions, those pertaining to addition, multiplication, etc. as the following code sequence demonstrates:

input(3)=> join[x+2y, 3x-4y+z]
output(3)=> x+(2*y)+(3*x)+((-4)*y)+z
input(4)=> %+2z 
output(4)=> (4*x)+((-2)*y)+(3*z)
input(5)=> join[x y^2 z, 2 z x] 
output(5)=> x*(y^2)*z*2*x*z
input(6)=> %*1 
output(6)=> 2*(x^2)*(y^2)*(z^2)

You should have noticed that join() puts the elements together without further processing. Subsequent manipulation of the result would suffice to do the rest.

The function reverse() would reverse the order of elements in a function. Let's take the result of output(2) and reverse it. Here, we shall use of the postfix operator // which you'll find very convenient as you become fluent with MathScript manipulations:

input(7)=> %2 // reverse
output(7)=> F[z, y, x, b, a]

The functions rotlft() and rotrgt() would rotate the elements of a MathScript function from the left and from the right, respectively, while the function rotations() gives all possible rotations of a function. Let us apply them to the above result:

input(8)=> %// rotlft
output(8)=> F[y, x, b, a, z]
input(9)=> %// rotlft 
output(9)=> F[x, b, a, z, y]
input(10)=> %// rotrgt 
output(10)=> F[y, x, b, a, z]
input(11)=> %// rotations 
output(11)=> {F[y, x, b, a, z], F[x, b, a, z, y], F[b, a, z, y, x], F[a, z, y, x, b], F[z, y, x, b, a]}

In the above array, we have five different orders of elements, neither of which is a sorted order. This gives us the opportunity to apply the function isSorted(), and the function sort(), along with the function map(). The latter function applies a given function to each element of an array (or any MathScript function as well). So let us map isSorted() to check if any of the above elements is sorted, then map sort() to put them all in order, and then check again:

input(12)=> map[isSorted, %]
output(12)=> {false, false, false, false, false}
input(13)=> map[sort, %%] 
output(13)=> {F[a, b, x, y, z], F[a, b, x, y, z], F[a, b, x, y, z], F[a, b, x, y, z], F[a, b, x, y, z]}
input(14)=> map[isSorted, %] 
output(14)=> {true, true, true, true, true}

The function permutations() gives all possible permutations of the elements of a MathScript function. Let us begin with an array of four elements. The number of possible permutations is 24 (or 4!):

input(15)=> {x,y,z,w}
output(15)=> {x, y, z, w}
input(16)=> %//permutations 
output(16)=> {{x, y, z, w}, {y, x, z, w}, {y, z, x, w}, {y, z, w, x}, {x, z, y, w}, {z, x, y, w}, {z, y, x, w}, {z, y, w, x}, {x, z, w, y}, {z, x, w, y}, {z, w, x, y}, {z, w, y, x}, {x, y, w, z}, {y, x, w, z}, {y, w, x, z}, {y, w, z, x}, {x, w, y, z}, {w, x, y, z}, {w, y, x, z}, {w, y, z, x}, {x, w, z, y}, {w, x, z, y}, {w, z, x, y}, {w, z, y, x}}

The function signature() checks the order of elements; if any two elements are similar, it gives 0; if the order of elements corresponds to an even number of permutations, it gives +1; if an odd number of permutations, it gives -1. So let us map signature() to the above result:

input(17)=> map[signature, %]
output(17)=> {-1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1}

In the above result, we see that the number of positive permutations is equal to the number of negative ones. To check this, it is our opportunity to use the function countMemberOf(). This and similar functions concerning membership will be illustrated further later on. However, just for now:

input(18)=> countMemberOf[1,%17] 
output(18)=> 12
input(19)=> countMemberOf[-1,%17] 
output(19)=> 12

Before proceeding to the next demodoc, where we shall continue to introduce more functions and features of MathScript, why don't you practice using MathScript live by clicking the program page below, and apply what you learned on this page, whichever way you like.

Program Page
Next
NSB Cosmic Center Image

Home
N.S.B. Cosmic Center

NSB Cosmic Center Image