Lecture #14
Programming Languages
MISHRA 95
C via MathLink
1, 1/2, 0.5, 1+2I,
E^( 2 I Pi)
+, -, * (x*y
x y),
/, ^,
<, <=, >, >=
1/Infinity
0Infinity/Infinity
Indeterminate1/0
ComplexInfinityFulForm[Infinity]
DirectedInfinity[1]
N (e.g., N[sin[5]] or N[sin[5],20])
computes the numerical value of an expression to the required precision
conj
conj[z_?NumberQ] := Re[z] - I Im[z];
conj[DirectedInfinity[z_]] :=
DirectedInfinity[Re[z] - I Im[z]];
conj[DirectedInfinity[]] := DirectedInfinity[];
conj[Indeterminate] := Indeterminate;
NSum, NProd, NIntegrateNSum[expr[i], {i, L, U}]
NProd[expr[i], {i, L, U}]
NIntegrate[expr[x], {x, L, U}]


cheb1[0,x_] := 2;
cheb1[1,x_] := x;
cheb1[n_,x_] := (x cheb1[n-1,x] - cheb1[n-2,x])//
Expand;
cheb2[n_,x_] := TrigReduce[2 Cos[n th]]/.
Cos[th] -> x/2 //
Expand;
Why does it work?

Why is it faster?
MAPCAR)
A = Table[n!, {n,1,5}]
=> {1, 2, 6, 24, 120}
Log[A]
=> {0, Log[2], Log[6], Log[24], Log[120]}
Table[{i + j x}, {i, 2}, {j, 5, 7}]
=> {{{1 + 5 x}, {1 + 6 x}, {1 + 7 x}},
{{2 + 5 x}, {2 + 6 x}, {2 + 7 x}}}
Table[expression, iterator]
listA = Table[Random[Integer, {0, 15}], {10}]
=> {12, 7, 4, 12, 2, 5, 2, 5, 10, 3}
listA[[3]]
=> 4
Take[listA, 4]
=> {12, 7, 4, 12}
First[listA]
=> 12
Rest[listA]
=> {7, 4, 12, 2, 5, 2, 5, 10, 3}
Prepend[listA, 5]
=> {5,12, 7, 4, 12, 2, 5, 2, 5, 10, 3}
>> Sort, Reverse, RotateLeft, RotateRight
>> Drop, Take, Insert, Append, Prepend,'
First, Last, Rest
>> Length, Dimension
>> Partition, Flatten
A = {1, 2, 3, 4}; B = {3, 4, 5, 6};
Intersection[A, B]
=> {3, 4}
Union[A, B]
=> {1, 2, 3, 4, 5, 6}
Join[A, B]
=> {1, 2, 3, 4, 3, 4, 5, 6}
Select
Select[{-3, 2, 4.5, 7, a, 5, -5}, Positive]
=> {2, 4.5, 7, 5}
Apply and Map:
Apply[f, {a, b, c}]
=> f[a, b, c]
Map[f, {a, b, c}]
=> {f[a], f[b], f[c]}
Apply[Plus, {1, 2, 3, 4}]
=> 10
Map[#^2&, {a, b, c, d}]
=> {a^2, b^2, c^2, d^2}
='' a = 3 => 3
a + 7 => 10
2
a = x^2 + y => x + y
1
1/a => ------
2
x + y
A value can be unassigned as follows:
a = .
a => a
:=''L-val := R-val, then L-val's value is an
``unevaluated expression.'' Whenever MATHEMATICA needs to evaluate
L-val subsequently, only then the evaluation of the R-val
is made.
a = 3
f = a => 3
g := a
a = 5
f => 3
g => 5
random1 = Random[] => 0.495874
random2 := Random[]
Table[random1, {5}]
=> {0.495874, 0.495874, 0.495874, 0.495874, 0.495874}
Table[random2, {5}]
=> {0.02327, 0.71788, 0.430938, 0.862341, 0.128564}
factorial[1] := 1; factorial[n_] := factorial[n] = n factorial[n-1]
a = b = c = d = e => e
lhs := rhs /; test
g[x_] := 1/; x > 0 g[x_] := -1/; x <= 0
If-Then-Else
h[x_] := If[ x > 0, 1, -1, 0]
Which and Switch
j[x_] := Which[ x > 0, 1, x < 0, -1, True, 0]
foo[x_] := Switch[Mod[x,3]
0, a, 1, b, 2, c, -, error]
== (EQUAL), != (UNEQUAL), === (SAME), =!= (UNSAME) TrueQ, && (AND), || (OR)
Do, While and For
Do[ Print[i^2], {i, 1, 5}]
Do[ Print[i], {i, 2a, 6a, a} ]
Iterator =
, start, stop, incr+
Do[ Print[{i, j}], {i, 3}, {j, i} ]
=> {1, 1}
{2, 1}
{2, 2}
{3, 1}
{3, 2}
{3, 3}
While[test, body]
For[start, test, step, body]
Fib[k_Integer?Positive] :=
Module[{fn1 = 1, fn2 = 0},
While[ fn1 < k,
{fn1, fn2} = {fn1 + fn2, fn1}
]; fn2
]
SameTest[l_List] :=
Module[{i},
For[i=1,
(i<Length[l]) && (l[[1]] === l[[i]]),
i++,
];
l[[1]] === l[[i]]
]
Break[] and Continue[]
Break exits the innermost enclosing loop.
Continue skips the remaining portion of the loop body for the
current iteration
Until
Until[body, test]
Begin["`Private`"]
Attributes[Until] = {HoldAll}
Until[body_, test_] :=
Module[{t},
For[t=False, !t, t=test, body]
]
End[]
HoldAll
prevents evaluation of the arguments body & test
Until
i = 1; Until[ Print[i++], i== 5]
Until in the infix notation
i = 1; (Print[i++]) ~Until~ (i==5)
translates to E[#1, #2, ..., #n] &
Map[#^2 &, a + b + c]
2 2 2
=> a + b + c
Map[Take[#,2]&, {{2,1,7}, {4,1,5}, {3,1,2}}]
=> {{2, 1}, {4, 1}, {3, 1}}
r = 2;
Nest[(# + r/#)/2&, N[1,40], 7]
=> 1.41421356237309504880168872420969807857
Nest[f, expr, n] applies the function f to expr
n times.
FixedPoint, Fold, FoldList
PowerSet
CopyPut[l_List, x_] :=
Join[l, Table[
Append[l[[i]], x], {i, Length[l]}
]]
PowerSet[l_List] := Fold[CopyPut, {{}}, l]
PowerSet[{a, b, c}]
=> {{}, {a}, {b}, {a, b},
{c}, {a, c}, {b, c}, {a, b, c}}
SS2L that takes three numbers as
arguments and returns the sum of the squares of the two larger numbers.
SS2L1[x_, y_, z_] :=
If[x > y,
If[y > z, x^2 + y^2, x^2 + z^2],
If[x > z, x^2 + y^2, y^2 + z^2]
]
SS2L2[x_, y_, z_] :=
Apply[Plus, Take[
Sort[{x, y, z}], -2
]^2 ]
_'' and can be added ``name''
(e.g., name_), ``type'' and ``?test'' (e.g.,
arg_type?test)
lg[n1_ n2_] := lg[n1] + lg[n2] lg[n1_^n2_] := n2 lg[n1] lg[x y z] => lg[x] + lg[y] + lg[z] lg[x y x y x y] => 3 lg[x] + 3 lg[y]
g[l_List] := First[l]^Last[l]
g[{2, 2}] => 4
g[{2, 3, 3, 2}] => 4
g[{first_,last_}] := first^lastg[list:{_, _}] := First[list]^last[list]
myMap[f_,{}] := {}
myMap[f_,{a_,b___}] := Prepend[myMap[f,{b}],f[a]]
myMap[g, {1, 2, 3}]
=> {g[1], g[2], g[3]}
find9[{_,".",a___,"9","9","9","9","9","9",___}]
:= Langth[{a}]
find9[Characters[ToString[N[Pi,800]]]] => 761
---Last Slide---
Final Remarks
Block,
Module and With.[End of Lecture #14]