1.4 Numbers¶
FriCAS distinguishes very carefully between different kinds of numbers, how they are represented and what their properties are. Here are a sampling of some of these kinds of numbers and some things you can do with them.
Integer arithmetic is always exact.
11^13 * 13^11 * 17^7 - 19^5 * 23^3
Type: PositiveInteger
Integers can be represented in factored form.
factor 643238070748569023720594412551704344145570763243
Type: Factored Integer
Results stay factored when you do arithmetic. Note that the 12 is automatically factored for you.
% * 12
Type: Factored Integer
Integers can also be displayed to bases other than 10. This is an integer in base 11.
radix(25937424601,11)
Type: RadixExpansion 11
Roman numerals are also available for those special occasions. Roman numerals
roman(1992)
Type: RomanNumeral
Rational number arithmetic is also exact.
r := 10 + 9/2 + 8/3 + 7/4 + 6/5 + 5/6 + 4/7 + 3/8 + 2/9
Type: Fraction Integer
To factor fractions, you have to pmap factor onto the numerator and denominator.
map(factor,r)
Type: Fraction Factored Integer
SingleInteger refers to machine word-length integers.
In English, this expression means 11 as a small integer.
11@SingleInteger
Type: SingleInteger
Machine double-precision floating-point numbers are also available for numeric and graphical applications.
123.21@DoubleFloat
Type: DoubleFloat
The normal floating-point type in FriCAS, Float, is a software implementation of floating-point numbers in which the exponent and the mantissa may have any number of digits. The types Complex(Float) and Complex(DoubleFloat) are the corresponding software implementations of complex floating-point numbers.
This is a floating-point approximation to about twenty digits. floating
point The ::
is used here to change from one kind of object (here,
a rational number) to another (a floating-point number).
r :: Float
Type: Float
Use digits to change the number of digits in the representation. This operation returns the previous value so you can reset it later.
digits(22)
Type: PositiveInteger
To 22 digits of precision, the number eπ163.0 appears to be an integer.
exp(%pi * sqrt 163.0)
Type: Float
Increase the precision to forty digits and try again.
digits(40); exp(%pi * sqrt 163.0)
Type: Float
Here are complex numbers with rational numbers as real and complex numbers imaginary parts.
(2/3 + %i)^3
Type: Complex Fraction Integer
The standard operations on complex numbers are available.
conjugate %
Type: Complex Fraction Integer
You can factor complex integers.
factor(89 - 23 * %i)
Type: Factored Complex Integer
Complex numbers with floating point parts are also available.
exp(%pi/4.0 * %i)
Type: Complex Float
The real and imaginary parts can be symbolic.
complex(u,v)
Type: Complex Polynomial Integer
Of course, you can do complex arithmetic with these also.
% ^ 2
Type: Complex Polynomial Integer
Every rational number has an exact representation as a repeating decimal expansion
decimal(1/352)
Type: DecimalExpansion
A rational number can also be expressed as a continued fraction.
continuedFraction(6543/210)
Type: ContinuedFraction Integer
Also, partial fractions can be used and can be displayed in a partial fraction compact format fraction:partial
partialFraction(1,factorial(10))
Type: PartialFraction Integer
or expanded format.
padicFraction(%)
Type: PartialFraction Integer
Like integers, bases (radices) other than ten can be used for rational numbers. Here we use base eight.
radix(4/7, 8)
Type: RadixExpansion 8
Of course, there are complex versions of these as well. FriCAS decides to make the result a complex rational number.
% + 2/3*%i
Type: Complex Fraction Integer
You can also use FriCAS to manipulate fractional powers.
(5 + sqrt 63 + sqrt 847)^(1/3)
Type: AlgebraicNumber
You can also compute with integers modulo a prime.
x : PrimeField 7 := 5
Type: PrimeField 7
Arithmetic is then done modulo 7.
x^3
Type: PrimeField 7
Since 7 is prime, you can invert nonzero values.
1/x
Type: PrimeField 7
You can also compute modulo an integer that is not a prime.
y : IntegerMod 6 := 5
Type: IntegerMod 6
All of the usual arithmetic operations are available.
y^3
Type: IntegerMod 6
Inversion is not available if the modulus is not a prime number. Modular arithmetic and prime fields are discussed in Section ugxProblemFinitePrime .
1/y
There are 12 exposed and 13 unexposed library operations named /
having 2 argument(s) but none was determined to be applicable.
Use HyperDoc Browse, or issue
)display op /
to learn more about the available operations. Perhaps
package-calling the operation or using coercions on the arguments
will allow you to apply the operation.
Cannot find a definition or applicable library operation named /
with argument type(s)
PositiveInteger
IntegerMod 6
Perhaps you should use "@" to indicate the required return type,
or "$" to specify which version of the function you need.
This defines a to be an algebraic number, that is, a root of a polynomial equation.
a := rootOf(a^5 + a^3 + a^2 + 3,a)
Type: Expression Integer
Computations with a are reduced according to the polynomial equation.
(a + 1)^10
Type: Expression Integer
Define b to be an algebraic number involving a.
b := rootOf(b^4 + a,b)
Type: Expression Integer
Do some arithmetic.
2/(b - 1)
Type: Expression Integer
To expand and simplify this, call ratDenom to rationalize the denominator.
ratDenom(%)
Type: Expression Integer
If we do this, we should get b.
2/%+1
Type: Expression Integer
But we need to rationalize the denominator again.
ratDenom(%)
Type: Expression Integer
Types Quaternion and Octonion are also available. Multiplication of quaternions is non-commutative, as expected.
q:=quatern(1,2,3,4)*quatern(5,6,7,8) - quatern(5,6,7,8)*quatern(1,2,3,4)
Type: Quaternion Integer