Usage
define MyRing: Category == with {MyArithmeticType, ...}
MyFloat: Join(MyArithmeticType, ...) with {...} == add {...}
Description
The category of types with addition, subtraction, and multiplication operations.
MyArithmeticType is the category of types that provide operations that can be performed in a ring.
Remarks
MyArithmeticType provides only the signatures of ring operations. There is no statement made about which axioms these operations have to fulfil. MyAdditiveType and MyArithmeticType are useful to provide a ring signatures to domains like Float, DoubleFloat, and similar types that in many respect behave like rings but are by their imprecise nature not mathematical rings themselves.
Exports of MyArithmeticType
*: (%, %) -> % Returns the product of its arguments.
^: (%, MachineInteger) -> % Raises its first argument to the power given by the second argument.
commutative?: Boolean Returns true if * is commutative.
times!: (%, %) -> % Returns the product of its arguments.
Of course, an (additive) group-like structure is provided.
Export of MyArithmeticType
one?: % -> Boolean
Usage
if one? x then ...
Parameters
Element to be tested.
Description
Tests whether its argument is 1.
Remarks
Note that one would usually not use this function on floating point types. A test for 1 might fail because of small errors introduced during a computation.
Export of MyArithmeticType
*: (%, %) -> %
Usage
z := x * y
Parameters
Elements to be multiplied.
Description
Returns the product of its arguments.
Export of MyArithmeticType
^: (%, MachineInteger) -> %
Usage
z := x ^ n
Parameters
Element to be exponentiated.
Exponent.
Description
Raises its first argument to the power given by the second argument.
Export of MyArithmeticType
commutative?: Boolean
Usage
if commutative? then {
-- case where the multiplication is known to be commutative
} else {
-- it is not known whether the multiplication is commutative
}
Description
Returns true if * is commutative.
Export of MyArithmeticType
times!: (%, %) -> %
Usage
z := times!(y, z);
z := 1;
for x in somelist repeat z := times!(z, x);
Parameters
Elements to be multiplied.
Description
Returns the product of its arguments.
Remarks
See add! and note that there are now two constants, namely 0 and 1.
In the implementation we make sure that further destructive changes on the result of times! have no influence on the second argument. Unfortunately, one can only do this if the type is know to be copyable. The default implementation of times! is nearly identical to the one of add!. Also the remarks made there apply here accordingly.