Home, Up: Insert Files and User Defined Elements
On this page:
Creating a User-Defined Operator, User-Defined 2-Argument Operators, More than 2 Arguments.
User-defined elemens let you create your own operators, which let you perform your calculations without having to remember and type possibly complex mathematical formulas.
For instance, the volume of a cube with an edge length of, let's say, 3.5 is easily calculated: 3.5 CUBE.
But what if you need to calculate the volumes of spheres? The volume of a sphere with the diameter of 3.5 is 3.5 CUBE PI * 6 /.
If you have to perform this calculation more often, you can let your own user-defined operator do the task -- let's give it the name @sphere:
@sphere = CUBE PI * 6 /
When you add the above line to your hy.ini file your operator becomes a part of Hypatia at the next start of the program, or after you give the RESET command.
? 3.5 @sphere
= 22.4492975037771
You can use your operator in any calculation, the same way you can use Hypatia's built-in operators.
You have to remember, though, what exactly your operator does -- for instance, that its argument is the diameter of the sphere, not the radius, and that its result is the volume, not the surface.
You can add this information as a comment to the definition of you operator in the hy.ini file, after double hash signs:
@sphere = CUBE PI * 6 / ## argument is radius, result is volume
Or you can use EDNOTES to enter this information in the file notes.txt.
Instead of user-defined elements you could also use insert files for your own operators, but there is little to recommend this option.
With one argument, you can create any operator you need as long as it can be put together from Hypatia's own operators.
(If this cannot solve your problem, you have to look at variables, scripts and loops.)
Often your 2-argument operator can be straightforward -- for instance, to easily convert feet and inches to centimeters, add this line to hy.ini:
@fi2cm = 12 / + :m 100 *
(Divide inches by 12 to convert them to feet, add them to the feet, convert result to meters, convert to centimeters by multiplying with 100.)
(Any changes to hy.ini become effective when you next start Hypatia, or with the RESET command.)
To calculate the height in centimeter or a person who is 5 feet 7 simply enter:
? 5 7 @fi2cm
= 170.18
But it can also get a bit more complicated -- some user-defined 2-argument operators will need recourse to a special feature, the WHISK pseudo operator, which we have already seen on the page "Pseudo Operators" in chapter "Operators and Constants".
Let's look at these examples -- formulas in infix notation, followed by the corresponding formulas using Hypatia's RPN:
a + b^2 -- SQ +
(a + b)^2 -- + SQ
a^2 + b^2 -- here we hit an obstacle: how would our operator reach the second argument to its left?
WHISK performs the necessary trick. It removes (whisks away) the two values to its left, and stores them under the names A and B, which you can then use in your calculation.
? 3 4 WHISK A SQ B SQ +
= 25
This lets us, for instance, create a user defined 2-argument operator that calculates the hypotenuse of a right-angled triangle from its legs -- the famous a^2 + b^2 = c^2 or c = SQRT(a^2 + b^2) formula:
Add the following line to hy.ini:
@hypot = WHISK A SQ B SQ + SQRT
and Hypatia knows your operator:
? 3 4 @hypot
= 5
True, you might have done this calculation with an n-argument operator, 6 8 SQSUM SQRT, for which you wouldn't need WHISK, but n-argument operators only work at the beginning of a line, or with the help of the delimiter | (see "N-Arguent Operators" in chapter "Operators and Constants").
WHISK does not have this restriction, and also makes more complex calculations possible, in which A and B are used repeatedly.
Take, for instance, Srinivasa Ramanujan's approximation formula for the circumference of an ellipse with the semi-axes a and b -- in infix notation: (3 (a + b) - sqrt(10 a b + 3 (a^2 + b^2))) * pi, in RPN notation: a b + 3 * a b * 10 * a SQ b SQ + 3 * + SQRT - PI *
You can use this formula for a user-defined 2-argument operator which you addd to hy.ini:
@ellips = WHISK A B + 3 * A B * 10 * A SQ B SQ + 3 * + SQRT - PI *
and use it to calculate the approximate circumferences of ellipses:
? 3 6 @ellips
= 29.0652632930139
Even if you may never have to use this formula or any other of such complexity, this is a nice example of what user-defined operators can do.
Again, if WHISK doesn't cover your needs you need to look at variables, scripts and loops.
With the help of WHISK you can create any 2-argument operator that can do whatever can be done in one calculation line, but you can also create operators with three or even more arguments, as long as the operator can work through its arguments from right to left.
A rather trite example: to calculate the volume of a cuboid, you have to multiply length, width and height. You can write this as l w * h * but you could also write it as l w h * * -- in that case, * * would work as a 3-argument operator:
@cubvol = * *
? 3 4 2 @cubvol
= 24
Not quite as trivial: a to b equals c to x -- this is a problem that you may encounter in many contexts, whether in geometry or in daily life.
In infix notation:
c / x = a / b
x / c = b / a
x = b * c / a
but, if you want to write the arguments in the order a b c, you cannot create a 3-argument operator that does this calculation -- unless you turn the fraction on its head:
x = 1 / (a / (b * c))
or in Hypatia's RPN notation:
a b c * / RCP -- multiply b and c, divide a by their product, get reciprocal value of that result.
And for this, we can create a 3-argument operator, let's call it @abcx:
@abcx = * / RCP
? 3 4 12 @abcx
= 16
While you cannot create user-defined operators for all arithmetic tasks that involve three arguments, for many of them you can, particularly with the help of WHISK. In some cases, you may have to put the arguments in a particular order.
Operators with four or even more arguments would in some cases be possible, but such problems will probably be better solved with the help of variables and scripts.
Home, Up: Insert Files and User Defined Elements, Prev: User-Defined Elements, Next: Notes on User-Defined Elements