Review of some Maxima Syntax and Semantics

Download Report

Transcript Review of some Maxima Syntax and Semantics

Review of some Maxima
Syntax and Semantics
1
Arithmetic and algebra
• Exact, unlimited precision
• Standard arithmetic operators: + - * /
• Use *, not juxtaposition, for multiplication
– Use 2 * x, not 2 x
• Use ^ for raising to a power
• Many algebraic operators:
–
–
–
–
expand()
factor()
ratsimp
…
2
Execution of statements
• Terminate with a semicolon and type
<enter> in XMaxima
• Terminate with a semicolon and type
<shift-enter> in wxMaxima
• Use a $ instead of a semicolon to
suppress output
3
Variables and constants
• Easy to name a variable and assign a
value to it
• Examples:
x:3;
abc:4! ;
z3: 7 ;
• Use % with standard constants
– %pi, %e
4
Functions
• Built-in functions are always in lower case:
– sin, cos, exp, …
– sin(%pi/2), log(%e^x)
• Use sin(%pi/2) not Sin(%pi/2)
• Maxima is case-sensitive
5
Assignments to variables,
constants, and functions
• x: 3;
• z: x + y/2;
• f(x,y,z) := x + 2 *y + 3 *z;
• g(w) := w/2;
• wow(a) := sin(%pi* a);
6
Formal and actual arguments
• f(x,y,z) := x + 2 *y + 3 *z^2;
7
Formal and actual arguments
• f(x,y,z) := x + 2 *y + 3 *z; Function definition
8
Formal and actual arguments
• f(x,y,z) := x + 2 *y + 3 *z; Function definition
Formal arguments
9
Formal and actual arguments
• f(x,y,z) := x + 2 *y + 3 *z; Function definition
Formal arguments
Actual arguments
• f(Fred, Ethel, 7) has the value
Fred + 2 * Ethel + 3 *7
10
Lists and arrays
• Both data structures are needed in
Maxima
• There are drop-down menus in wxMaxima
to help with creation of either lists or
arrays.
11
Lists in Maxima
12
Lists in Maxima
Remove the %
13
Can also use the command
makelist(i, i, 1, 20);
to get the output
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
18,19,20]
Also,
A: makelist(i, i, 1, 20);
assigns the result to the list named A
14
Can use lists for the polynomial
algebra exercise
Plus(p1, p2) := p1 + p2; (I used upper case to
denote a user-defined function)
Similarly, we need a function for subtracting
Minus(p1,p2):= p1 – p2;
• Remember, we needed to have the lists be the
same length when adding or subtracting
• Do this by appending [0] as much as necessary
15
Some useful array functions
first
x : first(A)
Returns first
element
tenth
x : tenth(A)
Returns 10th
element
delete
delete(first(A),
A);
Deletes
indicated
element from
array
16
Arrays in Maxima
• Creation, other operations
• Max # of dimensions is 5 (system limit)
• Arrays and matrices are the same in
Maxima
• Unlike most programming languages,
vectors are not precisely one-dimensional
arrays
– Vectors are akin to their use in graphics and
mechanics for 2- and 3-d drawing
17
Two ways to create matrices in
Maxima
• Select Enter from the Algebra menu
18
19
Operations on arrays
• They can be created, deleted, and have
their entries listed
• They can be added, subtracted, and
multiplied by numbers and simple
variables (scalars).
• As with lists, two arrays must have the
same size to be operated on together.
20
One way to create them
21
22
Other ways to create arrays
• array(B,20);
• C: make_array( any , 20);
• Array(D, 20, 10);
• E: make_array( any, 20, 10);
23
Other ways to create arrays
array(B,20);
C: make_array( any , 20);
array(D, 20, 10);
E: make_array( any, 20, 10);
24
listarray()
Lists all array
entries
Uses ##### for
undefined
entries
25
Array entries
• Array entries can be accessed by their
indicies.
• Warning: make sure each index stays
within the appropriate range limit.
• Often this will require programming.
26