domain( )
"Z7"
We define how to create new elements of this field and how to print them.
>> Z7::new:= proc(x) begin new(Z7, x mod 7); end_proc: Z7::print:= proc(x) begin subs( hold(q mod 7), hold(q)=extop(x,1) ) end_proc:
After that we are able to create elements of Z/Z7.
2 mod 7
2 mod 7
What we also need are functions to add and multiply elements of Z/Z7.
>> Z7::_plus:= proc() local x, i; begin x:= extop(args(1), 1); for i from 2 to args(0) do x:= (x + extop(args(i), 1)) mod 7; end_for; Z7::new(x); end_proc: Z7::_mult:= proc() local x, i; begin x:= extop(args(1), 1); for i from 2 to args(0) do x:= (x * extop(args(i), 1)) mod 7; end_for; Z7::new(x); end_proc:
Now we are able to operate on elements of Z/Z7 in the usual way.
1 mod 7
4 mod 7
0 mod 7
2 mod 7
To complete the definition of Z/Z7 we also declare the zero and the one element of the field and define some further operations.
>> Z7::zero:= Z7::new(0): Z7::one:= Z7::new(1): Z7::divide:= proc(x,y) begin Z7::new((extop(x,1) / extop(x,2)) mod 7); end_proc: Z7::negate:= proc(x) begin Z7::new( - extop(x,1) mod 7); end_proc: Z7::invert:= proc(x) begin Z7::new((1/extop(x,1)) mod 7); end_proc: Z7::_power:= proc(x,y) local help, i; begin if y=0 then return( Z7::one ); end_if; if y>0 then help:= x; else help:= Z7::invert(x); x:= help; end_if; for i from 2 to abs(y) do help:= Z7::_mult(help, x); end_for; end_proc:
And now we are able to perform all arithmetical operations for the elements of Z7 in the same way as we do for predefined types like integers or rationals.
2 mod 7
4 mod 7
Using the library package domains a field like Z/Z7 can be created easier by using the constructor IntegerMod. Refer to section "The library domains".
domain()
"NUMERIC"
>> NUMERIC::testtype:= proc(x, y) begin if contains({DOM_INT,DOM_FLOAT,DOM_COMPLEX,DOM_RAT}, domtype(x)) then TRUE; else FAIL; end_if; end_proc:
And now it can be used to test if an expresion is numerical.
>> testtype(123, NUMERIC), testtype(4.5, NUMERIC), testtype(1/2, NUMERIC), testtype(2*a, NUMERIC);
TRUE, TRUE, TRUE, FALSE
>> loadlib("domains"): export(domains): Z7:= IntegerMod(7);
IntegerMod(7)
After creating some elements of Z/Z7 we can perform usual arithmetical operations in the usual way.
5 mod 7
1 mod 7
2 mod 7
4 mod 7
5 mod 7
We can also test if Z/Z7 is a field or not.
TRUE
We know Z/Z8 is not a field but only a ring.
IntegerMod(8)
6 mod 8
FAIL
FALSE
TRUE