Home > User Guide > Laws
Passing Simple Input into Laws
Some law classes use the identity_law to acquire input. This is why first a law class instance x is created of the identity_law, and why this law instance is then used in the creation of subsequent laws. The integers passed to the identity_law at creation specify the index of its argument from an input list given in a later operation. The index in C++ starts at zero.
Consider the following code fragment:
law *x = new identity_law(8);
law *my_cos = new cos_law(x);This code fragment creates an identity_law instance with an index of 8. Then it creates a cos_law class instance. Assume that my_input_array is a list of values. When one of cos_laws methods is evoked, such as cos_law->evaluateS(my_input_array), it requires that my_input_array have at least 9 items (because indexing starts at 0); the ninth item is used as input to calculate the cosine.
This is the technique used to create more complex laws that have multiple input tags. If the C++ law classes are used directly, then the index into any later input list needs to be known at the creation of the class.
When working with the parsed strings from the law symbols, note the following:
- x, a1, and identity_law(0) represent the same index into the input list
- y, a2, and identity_law(1) represent the same index into the input list
- z, a3, and identity_law(2) represent the same index into the input list
- a# and identity_law(#-1) represent the same index into the input list
The following example is a code fragment that illustrates passing input into C++ law classes.
Note: Trigonometry law symbols assume that the input argument is in radians. Law symbols relating to curves require edges or coedges, because these are bounded. In other words, construction geometry curves are not supported.
C++ Example
law *x = new identity_law(0);
law *y = new identity_law(1);
law *z = new identity_law(2);
law *a1 = new identity_law(0); // same input as x
law *a2 = new identity_law(1); // same input as y
law *a3 = new identity_law(2); // same input as z
// Create special law:
// my_law = cos(x) + sin(y) + tan(z);
law *my_cos = new cos_law(x);
law *my_sin = new sin_law(a2); // same as sin_law(y)
law *my_tan = new tan_law(z);
law *my_law2 = new plus_law( my_cos, my_sin);
law *my_law = new plus_law( my_law2, my_tan);
// Initial clean up of memory usage
x->remove();
y->remove();
z->remove();
a1->remove();
a2->remove();
a3->remove();
my_cos->remove();
my_sin->remove();
my_tan->remove();
my_law2->remove();
int my_input[3];
double my_answer;
// Use new law instance to evaluate at 1 radian increments
for (int x=0; x<10; x++) {
for (int y=0; y<10; y++) {
for (int z=0; z<10; z++) {
printf(input is: %d %d %d /t, x, y, z);
my_input[0] = x;
my_input[1] = y;
my_input[2] = z;
// my_answer = cos(x) + sin(y) + tan(z);
my_answer = my_law->evaluateS(my_input);
printf(output is: %lf /t, my_answer);
my_answer = my_law->evaluateD(my_input, 1);
printf(1st derivative wrt x is: %lf /t, my_answer);
my_answer = my_law->evaluateD(my_input, 2);
printf(2nd derivative wrt x is: %lf /t, my_answer);
my_answer = my_law->evaluateD(my_input, 3);
printf(3rd derivative wrt x is: %lf /t, my_answer);
}
}
}
// Clean up memory usage
my_law->remove();Example. Passing Input to Law Classes
[Top]
© 1989-2007 Spatial Corp., a Dassault Systèmes company. All rights reserved.