Next: , Previous: featpost3D, Up: Import


4.13.7 math

This package extends Asymptote's mathematical capabilities with radian/degree conversion routines, point-in-polygon and intersection algorithms, matrix arithmetic and inversion, and a linear equation solver (via Gauss-Jordan elimination).

Unlike MetaPost, Asymptote does not implicitly solve linear equations and therefore does not have the notion of a whatever unknown. The following routine provides a useful replacement for a common use of whatever: finding the intersection point of the lines through P, Q and p, q, respectively:

pair extension(pair P, pair Q, pair p, pair q);
Return the intersection point of the extensions of the line segments PQ and pq.

Here are some additional routines provided in the math package:

void drawline(picture pic=currentpicture, pair P, pair Q, pen p=currentpen);
draw the visible portion of the (infinite) line going through P and Q, without altering the size of picture pic, using pen p.


real intersect(triple P, triple Q, triple n, triple Z);
Return the intersection time of the extension of the line segment PQ with the plane perpendicular to n and passing through Z.


triple intersectionpoint(triple n0, triple P0, triple n1, triple P1);
Return any point on the intersection of the two planes with normals n0 and n1 passing through points P0 and P1, respectively. If the planes are parallel, return (infinity,infinity,infinity).


real[] solve(real[][] a, real[] b)
Solve the linear equation ax=b by Gauss-Jordan elimination, returning the solution x, where a is an n \times n matrix and b is an array of length n. For example:
     
     import math;
     real[][] a={{1,-2,3,0},{4,-5,6,2},{-7,-8,10,5},{1,50,1,-2}};
     real[] b={7,19,33,3};
     real[] x=solve(a,b);
     write(a); write();
     write(b); write();
     write(x); write();
     write(a*x);
If the matrix a is tridiagonal, the routine tridiagonal provides a more efficient algorithm (see tridiagonal).


real[][] solve(real[][] a, real[][] b, bool overwrite=false)
Solve the linear equation ax=b returning the solution x, where a is an n \times n matrix and b is an n \times m matrix. If overwrite=true, b is replaced by x.


bool straight(path p)
returns true iff the path p is straight.