Skip to content

HadruMaths (Scala) Cheat sheet

thevpc edited this page Jul 29, 2017 · 2 revisions

Math Library Cheat Sheet (v3.0.60)

Recommanded Imports

Command Description
import net.vpc.scholar.hadrumaths._ Recommanded Imports
import net.vpc.scholar.hadrumaths.Maths._ Recommanded Imports
import net.vpc.scholar.hadrumaths.MathScala._ Recommanded Imports

Variable declarations

Command Description
var l = 12.5 * MM define a variable l of 12.5 mm
var fr= 12 * GHZ define a frequency fr of 12 GHz
var t=isteps(1,3) array of integers from 1 to 3 ={1,2,3}
var s=t.map(i=>"c"+i) transform t into a string array = {"c1","c2","c3"}
var uplet=t :_ * transform the table into an uplet ("c1","c2","c3") . Required for vararg methods such as exprList(...)
var d1=dsteps(1,3,0.5) an array of doubles from 1 to 3 with a step of 0.5 aka {1,1.5,2,2.5,3}
var d2=dtimes(1,3,5) a 5 samples array of doubles over 1 to 3 aka {1,1.5,2,2.5,3}

Domains

Command Description
val d1=domain(2.0 -> 5.0) Define an expression definition domain over IR , [2, 5[ (semi-open)
val d2=domain(2.0->5.0, 1.0->9.0, 0.0->10.0) [2, 5[ x [1, 9[ x[ 0, 10[ domain over IR3
var y=(d2/8).yvalues 8+1 samples over the y axis of d2 thus {1,2,…,9}

Complexes, Matrices and Vectors

Command Description
val c=0*î Define a complex var initialized to zero (î2=-1)
val c2=(c+1-î)^^2 evaluates (c+1-î)2
val M=matrix(10,20,(m,n)=>m*î+1+n) 10 by 20 matrix of generator cell m*î+1+n for each m,n cell
val M=symmetricMatrix( 10,(m,n)=>m*î+1+n) 10x10 square matrix of generator cell m*î+1+n for each m,n cell, obviously half of the matrix will be evaluated
val M=columnMatrix( 10,(m)=>m) 10 elements column matrix [0,1,2,3,4,5,6,7,8,9]
val M=rowVector( 10,(m)=>m) 10 elements row vector [0,1,2,3,4,5,6,7,8,9]
M(i,j)=M(j,i)+î handling matrix elements
M(i)=M(j)+î handling vector elements
var dbl=M1.getError(M2) relative error between 2 matrices/vectors
var mx=M1.getErrorMatrix(M2) matrix with relative error between each of M1 and M2 cells

Expressions

Command Description
val c=0*ê Define an expression var initialized to zero
X unit function f(x)=x
Y unit function f(x,y)=y
Z unit function f(x,y,z)=z
val e=î+X Define an expression function f(x)=î+x
val p=param("p") Define an expression parameter named p
val f=2*p*X*sin(X)*sin(Y) * domain(0 -> 2*PI,0 -> 2*PI) Define f(x)=2 p x sin(x) sin(x) function over domain [0 -> 2PI[ x [0 -> 2PI[ and zero elswhere
var g=f(p->3) Instantiate expression with the right p value ; evaluates to f(x)=23 x sin(x) sin(x) function over domain [0 -> 2PI[ x [0 -> 2*PI[
f(p->3) !! Simplifies expression ; evaluates to f(x)=6 x sin(x) sin(x) function over domain [0 -> 2PI[ x [0 -> 2PI[
f+g creates a new function as the sum of f and g (according to the domain of each)
f^^2 The square of the f function
f ** g the scalar product of f and g aka <f,g>=integral(f*conj(g)) over intersection-domain
var V=vector(f,g) expression vector where Vx=f and Vy=g

Expression lists

Command Description
var list1=f :+ g Create a list of {f,g}
var list2=exprList Create an empty list
var list3=exprList(list1) Create a list copy
var list=exprList(f,g) Create a list of {f,g}
var list=exprList(arr :_ *) Create a list all elements of array arr
list1 :+ f Concatenate f to list1
var list=list1 :+ list2 Add list2 elements to list1 and puts lists1 reference into list
var list=list1 + list2 Update list1 by list1(i)=list1(i)+list2(i) e=for each i, must be of the same size
var M=list1 ** list2 Create the scalar product matrix M as Mij=<list1(i),list2(j)>

Plots

Command Description
Plot.plot(g) Plot g, g could be an expression, an expression list, and array or a matrix
Plot.title("Hello").asMesh.plot(g1,g2,g3) Plot the list {g1,g2,g3} as 2D Mesh and defines a title "Hello"