home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-10-06 | 11.4 KB | 342 lines | [TEXT/NCII] |
- ####################################
- # Miscellaneous Routines
- #
- # These routines don't use any xCOD's, just NCII's built-in
- # programming language.
- #
- # Truncate(x) = Int(x*(1+1e-19) - .5) # Returns integer <= x.;
- # FractionalPart(x) = x - Truncate(x)# Returns fractional part of x>0.;
- # Round(x, DecimalDigits) = int(x*10^DecimalDigits)/10^DecimalDigits # rounds off x.;
- # Mod(a,b) = a - b*Truncate(a/b) # Returns a mod b.;
- # function IsEven(n) # Returns 1 if n is even, 0 if odd.
- #
- # Length(V) = sqrt(V•V) # length of a vector. ;
- # AngleBetween(V,W) = acos( V•W /length(V) /length(W) ) # angle from vector V to W, in radians.;
- # function Cross3D(X,Y) # Returns the 3D cross product of two 3D vectors x,y.
- # Hermitian(theMat) = Transpose(theMat†);
- # Commutator(a,b) = a•b - b•a;
- # function Trace(theMatrix) # Returns the sum of the diagonal elements of a matrix.
- #
- # Differentiate(f,x,dx) = [ f(x+dx/2) - f(x-dx/2) ]/dx # Returns df/dx at x[1…N]
- #
- # Mean(V) = sum(V)/size(V) # Returns the mean of an array V;
- # Variance(V) = Mean( | V-Mean(V) |^2 ) # Returns the sigma^2 of an array V;
- # StandardDeviation(V) = sqrt(Mean(|V|^2) - |Mean(V)|^2) #Returns standard deviation of an array V;
- # Gaussian(x,x0,sigma)=1/sqrt(2πsigma^2) exp(-0.5{(x-x0)/sigma}^2);
- #
- # Log(x) = ln(x)/ln(10) # log base 10 of x.;
- # function LogGrid(min, max, N) # Makes an N point logarithmic grid.
- #
- # Sigma(x) = (1 + Sign(x+1e-16))/2 # is 1 for x=>0, 0 for x<0.
- # aTan2(s,c) = if c>=0 then atan(s/c) else sign(s+1e-18)*π+atan(s/c)# is -π<theta<=π from s=sin(theta), c=cos(theta);
- # FixRoundOff(x) = (x+1)-1 # Returns x rounded off to about 1e-19.;
- # program BeepIfComplex(z) # Beeps if z is complex ;
- # program ConvertToComplex(x) # Converts x to a complex quantity.
- # SetNegativeToZero(A) = (Sign(A+1e-16) + 1)/2 * A #Zeros the elements of an array A which are < 0.
- #
- #
- # This text file explains and gives examples of
- # some of the routines in the file All Library Routines, which
- # should be Opened before trying any of these examples.
- ####################################
-
-
- ##############
- # Integer Arithmetic
-
- # Truncate(x) truncates the fractional part of a number.
- # Int (a built-in) rounds to the nearest even integer,
- # while 1e-19 is near the smallest SANE number eps such that
- # (1+eps)-eps <> 0.
- Truncate(x) = Int(x*(1+1e-19) - .5) # Returns integer <= x.;
-
- FractionalPart(x) = x - Truncate(x)# Returns fractional part of x>0.;
-
- Round(x, DecimalDigits) = int(x*10^DecimalDigits)/10^DecimalDigits # rounds off x.;
-
- # Examples:
- Truncate(2.9872)
- 2
- FractionalPart(2.9872)
- 0.9872
- Round(2.9872, 3)
- 2.987
- Round(2.9872, 2)
- 2.99
-
-
- # A simpler way to implement Trunc is to use the fact
- # that array indeces are truncated. Therefore, if you know
- # the range of integers of interest, just define an integer
- # array in that range and subscript it with the value you want
- # to truncate. For example, if you only use integers 1…100,
- Trunc[1…100] = 1…100;
- # Then Trunc(1.9) = Trunc[1] = 1, Trunc(2.3) = 2, etc.
-
-
- # Mod(a,b) is "a mod b", i.e. the remainder of a/b.
- Mod(a,b) = a - b*Truncate(a/b) # Returns a mod b.;
-
-
- # Also using Int, we can test for even/odd.
- # IsEven returns 1 if n is even, 0 if odd.
- function IsEven(n) # Returns 1 if n is even, 0 if odd.
- . begin
- . if n=int(n+0.5) then
- . IsEven=1
- . else
- . IsEven=0
- . end
- .
-
-
- ####################################
- # Matrix and Vector
- # Also see the xCOD files for Inverse, Determinant, and Eigens.,
- # which have many more matrix routines, and the sample file
- # Vetor and Matrix.
-
- # Find the length of a one-dimensional vector. ("•" is opt-8, or use the Shortcuts menu.)
- Length(V) = sqrt(V•V) # length of a vector. ;
-
- # The angle between two vectors.
- AngleBetween(V,W) = acos( V•W /length(V) /length(W) ) # angle from vector V to W, in radians.;
-
- # The three-dimensional cross product.
- function Cross3D(X,Y) # Returns the 3D cross product of two 3D vectors x,y.
- . var Z
- . # Input: X,Y[1…3] 3-D vectors
- . # Output: Cross3D = X cross Y
- . begin
- . if size(x) <> 3 then
- . Cross3D = " ERROR: X must be a 3D vector for Cross3D"
- . else if size(y) <> 3 then
- . Cross3D = " ERROR: Y must be a 3D vector for Cross3D"
- . else
- . begin
- . z[1] = x[2] y[3] - x[3] y[2];
- . z[2] = x[3] y[1] - x[1] y[3];
- . z[3] = x[1] y[2] - x[2] y[1];
- . Cross3D = z
- . end
- . end
- .
-
- # The Hermitian is the conjugate transpose of a matrix.
- Hermitian(theMat) = Transpose(theMat†);
-
- # This should only be applied to square matrices.
- Commutator(a,b) = a•b - b•a;
-
- function Trace(theMatrix) # Returns the sum of the diagonal elements of a matrix.
- . var d,Answer
- . begin
- . d = dimension(theMatrix)
- . Answer = 0
- . if size(d)<>2 then
- . Answer = " ERROR: argument to Trace must be a matrix."
- . else if d[1]<>d[2] then
- . Answer = " ERROR: matrix for Trace must be square."
- . else
- . for j=1…d[1] do Answer = Answer + theMatrix[j,j]
- . Trace = Answer
- . end
- .
-
-
- ####################################
- # Calculus
-
- # All of these are crude but fairly simple, and use only
- # NCII's built in programming language.
- #
- # Since the xCOD based routines in the Discrete Calculus file
- # do most of these same task quicker and more accurately,
- # all but the first of these have been commented out, leaving
- # the rest only for examples of the NCII programming language.
-
-
- # The numerical derivative of a function f(x), returned
- # at the values of the array x. dx is any "small" number.
- Differentiate(f,x,dx) = [ f(x+dx/2) - f(x-dx/2) ]/dx # Returns df/dx at x[1…N]
- # For example,
- f(x) = sin(x); x =0…2π@.01; dsin = DifferentiateFunc(f,x,1e-6)
-
- # # This one returns an array. x should be monotomic with y=f(x).
- # function DifferentiateArray(y,x)
- # . var y0, y1, n0, n, DA,DA0,DA1, ans
- # . begin
- # . n = size(y); n0 = FirstIndex(y); nN=n0+n-1;
- # . y0 = y[n0…(nN-1)]; y1 = y[(n0+1)…nN];
- # . DA = (y1-y0)/(x[2]-x[1]); # derivs at in-between points.
- # . DA0 = DA[1…nN-2]; DA1=DA[2…nN-1];
- # . ans = y;
- # . ans[(n0+1)…(nN-1)] = (DA0+DA1)/2; # interpolate middle pts
- # . ans[n0] = (3 DA0[1] - DA0[2])/2; # extrapolate start
- # . ans[nN] = (3 DA1[nN-2] - DA1[nN-3])/2; # extrapolate end.
- # . DifferentiateArray = ans
- # . end
- # .
-
- # # This assumes that y and x are arrays with y=f(x) and that
- # # x is monotomic. Simple trapezoidal integration is done.
- # function IntegrateArray(y,x) = sum(y) *(x[2]-x[1]);
- # . var n0, nN, ans
- # . begin
- # . n0 = FirstIndex(y);
- # . nN = n0 + Size(y)-1;
- # . ans = sum(y) - { y[n0]+y[nN] }/2
- # . ans = ans*{ x[2] - x[1] }
- # . IntegrateArray = ans
- # . end
- # .
-
- # function Integrate(F,from,to);
- # . var x,dx,y
- # . begin
- # . dx = (to-from)/300; # we'll do the integral with 300 points.
- # . x = from…to@dx;
- # . y = F(x);
- # . IntegrateFunc = IntegrateArray(y,x)
- # . end
- # .
-
- # # Finally an indefinite integral. Both y and x are arrays,
- # # y=f(x) and x is monotomic.
- # # Because of the FOR loop, this one is slow.
- # function IndefiniteIntegral(y,x)
- # . var n0,j, ans
- # . begin
- # . n0 = FirstIndex(x)
- # . ans = y # save space for answer
- # . ans[n0] = 0 # integral with same limits
- # . for j=(n0+1) to n0+size(x)-1 do
- # . ans[j] = IntegrateArray(y[n0…j],x[n0…j])
- # . IndefiniteIntegral = ans
- # . end
- # .
- # # Example:
- # x = 0…2π@.01; y = sin(x); IntegralY = IndefiniteIntegral(y,x)
- # # Then plotting [x,y] ; [x,IntegralY] will show
- # # sin(x) and (cos(x)-1). (The -1 happens because
- # # the integration is only to within a constant which makes the
- # # integral 0 at the smallest value of x.)
-
-
-
- ####################################
- # Statistics
- # Also see the files for Sorting and Mean, as well as
- # Special Functions (which has the Error Function and Chi Square probability).
-
- # Standard measures of an array of values.
- Mean(V) = sum(V)/size(V) # Returns the mean of an array V;
- Variance(V) = Mean( | V-Mean(V) |^2 ) # Returns the sigma^2 of an array V;
- StandardDeviation(V) = sqrt(Mean(|V|^2) - |Mean(V)|^2) # Returns standard deviation of an array V;
-
- # A "bell-curve" with mean x0 and Std. Dev. sigma.
- Gaussian(x,x0,sigma)=1/sqrt(2πsigma^2) exp(-0.5{(x-x0)/sigma}^2);
- #
- # Example:
- dx=0.01;x = -4…4@dx; y = Gaussian(x,0,1); # Now plot [x,y] .
- #
- sum(y) dx # normalization of gaussian
- 0.99993799
- #
- sum(y x^2) dx
- 0.9988873 # variance of x.
-
-
- ####################################
- # Log Utilities
-
- # In general, log A base B is ln(a)/ln(b).
- # ("e" is not defined by default, but "e=exp(1)" will define it.)
- Log(x) = ln(x)/ln(10) # log base 10 of x.;
-
- # This is useful for making an even grid when doing
- # log plots.
- #
- function LogGrid(min, max, N) # Makes an N point logarithmic grid.
- . var x,dx, logMax, logMin
- . # Input:
- . # min = lowest grid value
- . # max = highest grid value
- . # N = number of points
- . # Output:
- . # LogGrid = [min, min*Z, min*Z*Z, ..., max]
- . # (evenly spaced points in a log plot).
- . begin
- . logMin = log(min); logMax = log(max);
- . dx = (logMax - logMin)/(N-1);
- . x = 10^( logMin…logMax @ dx)
- . LogGrid = x
- . end
- .
-
-
- ####################################
- # Other
-
- # Sigma is sometimes called the "step" function.
- Sigma(x) = (1 + Sign(x+1e-16))/2 # is 1 for x=>0, 0 for x<0. ;
- # for example,
- Table( -2:2, Sigma(-2:2) )
- #
- # - - - - - - - - - - - - - - -
- -2 0
- -1 0
- 0 1
- 1 1
- 2 1
-
-
- # aTan2 is takes 2 arguments, sin(theta), cos(theta), and returns -π< theta < π.
- # This uses an "if" as part of a one line function. A full-fledged function might be
- # clearer, but not as much fun.
- aTan2(s,c) = if c>=0 then atan(s/c) else sign(s+1e-18)*π+atan(s/c)# is -π<theta<=π from s=sin(theta), c=cos(theta);
-
-
-
- # Return 1 if an object exists, 0 if it doesn't.
- function Exists(Obj) # Return 1 if Obj exists, 0 if not.
- . begin
- . if Obj=Obj then Exists=1 else Exists=0
- . end
- .
-
- # Sometimes useful for setting small numbers due to
- # round off errors to zero. This works for numbers,
- # arrays, or matrices. However, if x is complex
- # this will only affect the real part; (i+x)-i will do the
- # same for the complex part. (And will make it complex
- # if it isn't already.)
- FixRoundOff(x) = (x+1)-1 # Returns x rounded off to about 1e-19.;
-
- # Usually real to complex conversions are handled
- # automatically; however, it is possible to tell how and object
- # is stored.
- program BeepIfComplex(z) # Beeps if z is complex ;
- . begin
- . if Size(z) <> SizeAsIfReal(z) then beep
- . end
- .
-
- # This makes x complex, which means it will
- # take up twice as much space. Usually this conversion
- # is done automatically when needed. Under some circumstances
- # (such as "if x=1") it will be converted back to real
- # automatically if the complex part is zero.
- program ConvertToComplex(x) # Converts x to a complex quantity.
- . begin
- . x = (x+i)-i
- . end
- .
-
- # The Sign function may be used to do the work of an IF on arrays.
- # Sign(x) is 1 for x>0, 0 for x=0, and -1 for x<0.
- # On array, it does Sign on each element of the array to produce a new array.
- # This example sets all the elements of an array which are less than zero to zero.
- SetNegativeToZero(A) = (Sign(A+1e-16) + 1)/2 * A #Zeros the elements of an array A which are < 0.
-
-