source("http://macosa.dima.unige.it/r.R")    # If I have not already loaded the library
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
S 19 Complex numbers. Matrices. Functions of two variables. Series

# Graph of a function of two variables. Example: f: (x,y)-> sin(x)+cos(2y)
 
# theta, phi e d  indicate the position of the eye: the direction of the look
# projected on the horizontal plane, its inclination relative to that plane and the
# distance of the eye from the center of the box.
 
# The following picture shows the meaning of theta and phi:
 
 
#                                  (see S11 too)
 
f <- function(x,y) sin(x)+cos(2*y)
# I choose the size of the domain and the amount of grid lines
x <- seq(-pi,pi, len=20); y <- seq(-pi,pi, len=20)
# I create the "table" that x and y values associate z values
z <- outer(x,y,f)
marg = function(x) par( mai=c(x,x,x,x) )               # I can change the margin size
marg(0.2); persp(x,y,z,phi=60,theta=30,d=20,col="yellow",ticktype="detailed")
# If I want no color, and reduce the distance
marg(0.2); persp(x,y,z,phi=60,theta=30,d=2,col=NULL,ticktype="detailed")
dev.new()                     # If I want open a new window (to retain the last graph)
#       If I want to choose the size of the window I can use  BF=…; HF=…; NW()  instead
# With "expand" I can apply an expansion to the z coordinates
marg(0.2); persp(x,y,z,phi=60,theta=30,d=20,col="cyan",ticktype="detailed",expand=0.5)
# without "ticktype":
dev.new(); marg(0.2); persp(x,y,z,phi=60,theta=30,d=20,col="green", expand=1.5)
 
 
# To have the chart without the box I add: ",box=FALSE".
# I can add pictures (with trans3d and pmat)
 
x <- y <- seq(-pi,pi, len=20); z <- outer(x,y,f)
marg(0.2)
fig = persp(x,y,z,phi=45,theta=65,d=20,col=NULL,box=FALSE)
lines (trans3d(x=c(-pi,pi),y=0,z=0,pmat=fig), col="red",lwd=2)
lines (trans3d(y=c(-pi,pi),x=0,z=0,pmat=fig), col="red",lwd=2)
lines (trans3d(z=c(-1,1),x=0,y=0, pmat=fig), col="red",lwd=2)
# The contour lines: x <- seq(-pi,pi, len=200); y <- seq(-pi,pi, len=200); z <- outer(x,y,f) marg(0.5); contour(x,y,z, levels = c(-1,-0.5,0,0.5,1) ) # See here for partial derivatives and hessian # See here to go deep # A curve in space (5 laps of a propeller) t <- seq(0,10*pi,len=1000) z0 <- c(0,10*pi); u <- rep(z0[1],4) # I put the box base in u z <- array(u,dim=c(2,2)); x <- c(-2,2); y <- c(-2,2) F <- persp(x,y,z,theta=30,phi=20,scale=TRUE,zlim=z0,xlim=x,ylim=y,d=1) lines(trans3d(cos(t),sin(t),t,pmat=F),col="red") # To have it without the box I would have added in persp: border="white",box=FALSE # See here how to obtatin the figure below in the center: # See here for the figure on the right (a conformal map): for other references to # complex numbers see K3 # See here for sequences, series and Fourier series. # See also Math & Music (slides and references) # For figure above and the our space (which is non-Euclidean) see here and # Walking on the balls (see "animazione" and "approfondimenti") # The matrices (see) are table of data that can be introduced as arrays: A = array(data=c(2,6,9), dim=c(1,3)) nrow(A); ncol(A); A; t(A) # [1] 1 # [1] 3 # [,1] [,2] [,3] A # [1,] 2 6 9 # [,1] t(A): the transpose of A # [1,] 2 # [2,] 6 # [3,] 9 B = array(data=c(1,5,8),dim=c(3,1) ); B # [,1] # [1,] 1 # [2,] 5 # [3,] 8 C = array(data=c(1,2, 5,3, 7,8), dim=c(2,3) ); C # [,1] [,2] [,3] the data are introduced by columns # [1,] 1 5 7 # [2,] 2 3 8 D = array(data=c(1,2, 5,3), dim=c(2,2) ); D # [,1] [,2] # [1,] 1 5 # [2,] 2 3 # After the name I can put the indices to identify an element: B[3]; D[1,2] # 8 5 A %*% B # %*% multiplies two matrices (1st row * 1st col, # [,1] # 2nd row * 1st col, …) # [1,] 104 # C %*% D is not defined B %*% A # I can compute t(A)+B and t(A)-B because t(A) and # [,1] [,2] [,3] # B have the same number of row and col # [1,] 2 6 9 # [2,] 10 30 45 # [3,] 16 48 72 M = INV(D) # Inverse: the matrix M such that M %*% D (and D %*% M) is the fraction( M ) # identity matrix (diagonal entries equal to 1, other to 0) # [,1] [,2] # [1,] -3/7 5/7 # [2,] 2/7 -1/7 fraction(D %*% M) # [,1] [,2] # [1,] 1 0 # [2,] 0 1 det(D) # the determinant of the matrix D (see) # [1] -7 # An alternative method to solve linear systems. An example: # x+3y=3 AND 4x+2y=7, that we can already solve: S = c(1,3,3, 4,2,7); eqSystem(S) # [1] 1.5 0.5 # I put the coefficients col by col ma = array(data=c(1,4, 3,2),dim=c(2,2)); no = array(data(3,7),dim=c(2,1)); ma; no # [,1] [,2] # [1,] 1 3 # [2,] 4 2 # [,1] # [1,] "3" # [2,] "7" solve(ma,no) # [,1] # [1,] 1.5 # [2,] 0.5 # An alternative method to calculate scalar (or dot) product. An example: v1 = c(1,-2,2); v2 = c(-4,1,2); prods(v1,v2) # [1] -2 v1 %*% v2; drop(v1 %*% v2) # drop the dimensions of the array # [,1] # [1,] -2 # [1] -2 # For the use of arrays in statistics see here. Other examples of use