Home > User Guide > Laws
Numeric Tools for Laws
One of the advantages of laws is that they offer a uniform interface so that one set of numerical tools can be made for all cases. In addition to the evaluation methods, several numerical functions are available for laws. This includes:
- Finding global maximums and minimums
- Finding multidimensional local minimums
- Performing numerical integration
- Finding roots
- Performing numerical differentiation
Scheme Example
(define my_law (law "(20*sin(x)+x^2-10)*.2"))
;; my_law
(law:nroot my_law -10 10)
;; (-3.14655291240276 0.508720501792063 3.13209276518624
;; 5.23133412213485)
(define my_veclaw (law "vec(x,law,0)" my_law))
;; my_veclaw
(define fedge(edge:law my_veclaw -10 10))
;; fedge
(define x_axis(edge:law "vec(x,0,0)" -10 10))
;; x_axis
(define y_axis(edge:law "vec(0,x,0)" -10 20))
;; y_axisExample. Finding Roots
Scheme Example
(define f (law "cos(x)"))
;; f
(define df (law:derivative f))
;; df
(define ddf (law:derivative df))
;; ddf
(define dddf (law:derivative ddf))
;; dddf
f
;; #[law "COS(X)"]
df
;; #[law "-SIN(X)"]
ddf
;; #[law "-COS(X)"]
dddf
;; #[law "SIN(X)"]
(define ndf(law "d(cos(x),x,1)"))
;; ndf
(define nddf(law:derivative ndf))
;; nddf
(define ndddf(law:derivative nddf))
;; ndddf
ndf
;; #[law "D(COS(X),X,1)"]
nddf
;; #[law "D(COS(X),X,2)"]
ndddf
;; #[law "D(COS(X),X,3)"]
(law:eval df 1)
;; -0.841470984807897
(law:eval ndf 1)
;; -0.841470984805174
(law:eval ddf 1)
;; -0.54030230586814
(law:eval nddf 1)
;; -0.540302277606634
(law:eval dddf 1)
;; 0.841470984807897
(law:eval ndddf 1)
;; 0.842631563290711Mathematical Calculations
The following example defines my_law to be a law for the mathematical function x+x2-cos(x). This law can be evaluated for a given value of x using the law:eval Scheme extension.
Note: Trigonometry law symbols assume that the input argument is in radians.
Scheme Example
(define my_law (law "x+x^2-cos(x)"))
;; my_law
; my_law => #[law "(X+X^2)-COS(X)"]
; Evaluate the given law at 1.5 radians
(law:eval my_law 1.5)
;; 3.6792627983323In addition to creating a law from a string, a law may also be created from a string and a list of laws and edges. The following example defines my_newlaw to be a law that returns the curvature of the curve in my_edge, plus the value of my_law.
Note: Law symbols relating to curves require edges or coedges, because these are bounded. In other words, construction geometry curves are not supported.
Scheme Example
(define my_edge (edge:circular (position 0 0 0) 30 0 90))
;; my_edge
; my_edge => #[entity 2 1]
(define my_law (law "x+x^2-cos(x)"))
;; my_law
; my_law => #[law "(X+X^2)-COS(X)"]
(define my_newlaw (law "curC(edge1)+law2" my_edge my_law))
;; my_newlaw
; my_newlaw => #[law "CURC(EDGE1)+((X+X^2)-COS(X))"]curC is a function that returns the curvature of a curve. Its argument in this case is edge1; the one (1) means that a curve should be the first input element. Indexing into argument lists always begins with one (1). law2 is the notation that means a law my_newlaw includes an existing law into its definition. The two (2) means that a law should be the second input element. The input list which follows the law definition contains my_edge, which has an underlying curve, and my_law as its first and second arguments, respectively. The curC law differs from other laws, such as cos, in that curC works on a data structure while cos works on a sublaw. Laws that work on data structures are called data laws.
A new law may also be created from another law by either taking the original laws derivative or by simplifying the original law, as in the following example:
Scheme Example
(define my_dlaw (law:derivative "x^2+cos(x)"))
;; my_dlaw
; my_dlaw => #[law "2*X-SIN(X)"]
(define my_simplaw (law:simplify "x^2/x-3*x"))
;; my_simplaw
; my_simplaw => #[law "-(2*x)"]Evaluation of Laws
There are five evaluate Scheme extensions for laws: law:eval, law:eval-vector, law:eval-position, law:nderivative and law:nintegrate.
law:eval, law:eval-vector, and law:eval-position are very similar in nature. law:eval returns a real or a list of reals. law:eval-vector requires that its input law be three dimensions and returns a gvector. law:eval-position returns either a position or a par-pos.
Scheme Example
(define my_law (law "x^2"))
;; my_law
; my_law => #[law "X^2"]
(law:eval my_law 3)
;; 9
(law:nderivative my_law 3 "x" 1)
;; 5.9999999999838
(law:nderivative my_law 3 "x" 2)
;; 1.9999997294066law:nderivative in the previous example returns the numerical approximation of the first derivative of x2 evaluated at 3. To get the exact value, evaluate the exact derivative, as seen next, which returns 6. The n in the command law:nderivative stands for numerical. All law commands that contain an n return approximate results. All other law commands return exact results to machine precision.
Scheme Example
(law:eval (law:derivative "x^2") 3)
;; 6
(law:eval (law:derivative "x^2") 3)
;; 6The third law evaluation extension for laws is law:nintegrate. This takes the numerical integral of a given law symbol over a specified range.
Scheme Example
; Define a law to integrate.
(define my_law (law "x + 1"))
;; my_law
; my_law => #[law "X+1"]
; Evaluate the law over the range 0 and 1.
(law:nintegrate my_law 0 1)
;; 1.5[Top]
© 1989-2007 Spatial Corp., a Dassault Systèmes company. All rights reserved.