mth(3)
πΈ mth(3) β standard math module πΈ
NAME
mth β standard math module π
STANDARD MATH MODULE
Okay bestie, listen up: the Standard Mathematical module is an original implementation of various mathematical facilities. It's giving convenient functions, linear algebra, and real analysis β the whole squad. π«Ά
Random number
The math module provides various functions that generate random numbers in different formats. No cap, it's a whole vibe.
get-random-integerβ return a random integer number π²get-random-realβ return a random real number between0.0and1.0β¨get-random-relatifβ return a random relatif number π«get-random-primeβ return a random probable prime relatif number π
The numbers are generated with the help of the system random generator. That generator is machine dependent, so results can vary from one machine to another. It's not a bug, it's a feature of the machine, iykyk.
Primality testing
The math module provides various predicates that test a number for a primality condition. Most of these predicates are intricate and are normally not used except the prime-probable-p predicate. So if you're not trying to be extra, just use that one, periodt.
fermat-pβ Fermat test predicate π§ͺmiller-rabin-pβ Miller-Rabin test predicate π¬prime-probable-pβ general purpose prime probable test πget-random-primeβ return a random probable prime relatif number π²
The fermat-p and miller-rabin-p predicates return true if the primality condition is verified. These predicates operate with a base number. The prime number to test is the second argument. Got it? π
Fermat primality testing
The fermat-p predicate is a simple primality test based on the "little Fermat theorem". A base number greater than 1 and less than the number to test must be given to run the test.
afnix:mth:fermat-p 2 7
In the preceding example, the number 7 is tested, and the fermat-p predicate returns true. If a number is prime, it is guaranteed to pass the test. The opposite is not true. For example, 561 is a composite number, but the Fermat test will succeed with the base 2. Numbers that successfully pass the Fermat test but which are composite are called Carmichael numbers. For those numbers, a better test needs to be employed, such like the Miller-Rabin test. So don't get tricked, bestie. π
Miller-Rabin primality testing
The miller-rabin-p predicate is a complex primality test that is more efficient in detecting prime number at the cost of a longer computation. A base number greater than 1 and less than the number to test must be given to run the test.
afnix:mth:miller-rabin-p 2 561
In the preceding example, the number 561, which is a Carmichael number, is tested, and the miller-rabin-p predicate returns false. The probability that a number is prime depends on the number of times the test is run. Numerous studies have been made to determine the optimal number of passes that are needed to declare that a number is prime with a good probability. The prime-probable-p predicate takes care to run the optimal number of passes. So you don't have to do the math, it's already doing the most. π
General primality testing
The prime-probable-p predicate is a complex primality test that incorporates various primality tests. To make the story short, the prime candidate is first tested with a series of small prime numbers. Then a fast Fermat test is executed. Finally, a series of Miller-Rabin tests are executed. Unlike the other primality tests, this predicate operates with a number only and optionally, the number of test passes. This predicate is the recommended test for the folks who want to test their numbers. It's the main character of primality testing, no cap. π
afnix:mth:prime-probable-p 17863
Linear algebra
The math module provides an original and extensive support for linear and non linear algebra. This includes vector, matrix and solvers. Complex methods for non linear operations are also integrated tightly in this module. It's giving math girlie energy. π
Real vector
The math module provides the Rvector object which implements the real vector interface Rvi. Such interface provides numerous operators and methods for manipulating vectors as traditionally found in linear algebra packages.
Operators:
==β compare two vectors for equality!=β compare two vectors for difference?=β compare two vectors upto a precision+=β add a scalar or vector to the vector-=β substract a scalar or vector to the vector*=β multiply a scalar or vector to the vector/=β divide a vector by a scalar
Methods:
setβ set a vector component by indexgetβ get a vector component by indexclearβ clear a vectorresetβ reset a vectorget-sizeβ get the vector dimensiondotβ compute the dot product with another vectornormβ compute the vector norm
Creating a vector
A vector is always created by size. A null size is perfectly valid. When a vector is created, it can be filled by setting the components by index.
# create a simple vector
const rv (afnix:mth:Rvector 3)
# set the components by index
rv:set 0 0.0
rv:set 1 3.0
rv:set 2 4.0
Real matrix
The math module provides the Rmatrix object which implements the real matrix interface Rmi. This interface is designed to operate with the vector interface and can handle sparse or full matrix.
Operators:
==β compare two matrices for equality!=β compare two matrices for difference?=β compare two matrices upto a precision
Methods:
setβ set a matrix component by indexgetβ get a matrix component by indexclearβ clear a vectorget-row-sizeβ get the matrix row dimensionget-col-sizeβ get the matrix column dimensionnormβ compute the matrix norm
π¦ Rvi π¦
okay so Rvi is an abstract class β it models the behavior of a real based vector. It's the blueprint, not the finished fit: it defines the vector length plus all the accessor and mutator methods. Think of it as the vibe, and Rvector as the outfit. β¨
π Predicate π
rvi-p
𧬠Inheritance π§¬
Serial
π₯ Operators π₯
==->Boolean(Vector) β returnstrueif the calling object is equal to the vector argument. equality check, slay π!=->Boolean(Vector) β returnstrueif the calling object is not equal to the vector argument. (and yes, the original text literally says "The==operator" right here β copy-paste crime, we're leaving it, she's doing her best π)?=->Boolean(Vector) β returnstrueif the calling object is equal to the vector argument up to a certain precision. floats, amirite π+=->Vector(Real|Vector) β returns the calling vector after adding the argument object. Form one: the real argument gets added to all vector components. Form two: the vector components get added one by one. π«Ά-=->Vector(Real|Vector) β returns the calling vector after subtracting the argument object. Form one: the real argument is subtracted from all vector components. Form two: the components are subtracted one by one.*=->Vector(Real|Vector) β returns the calling vector after multiplying by the argument object. Form one: the real argument multiplies all vector components. Form two: the components multiply one by one./=->Vector(Real) β returns the calling vector after dividing by the argument object. The vector components get divided by the real argument. (only the real form, bestie β no vector-on-vector division here π)
π Methods π
set->None(IntegerReal) β sets a vector component by index.get->Real(Integer) β gets a vector component by index.clear->None(None) β clears a vector. The dimension is not changed. πΈreset->None(None) β resets a vector. The size is set to0. gone, reduced to atoms πget-size->Real(None) β returns the vector dimension.dot->Real(Vector) β computes the dot product with the vector argument. math girlies rise πnorm->Real(None) β computes the vector norm.permutate->Vector(Cpi) β permutates the vector components with the help of a combinatoric permutation object.reverse->Vector(Cpi) β reverses (permutates) the vector components with the help of a combinatoric permutation object. β¨
π Rvector π
Rvector is the default implementation of the real vector interface. no cap, this is the one you'll actually be touching. π
π Predicate π
r-vector-p
𧬠Inheritance π§¬
Rvi
πΌ Constructors πΌ
Rvector(None) β creates a default null real vector.Rvector(Integer) β creates a real vector whose dimension is given as the calling argument.
π Functions π
get-random-integer->Integer(none|Integer) β returns a random integer number. With no argument, the integer range is machine dependent. With one integer argument, the resulting integer number is less than the specified maximum bound. π²get-random-real->Real(none|Boolean) β returns a random real number between0.0and1.0. First form (no argument): the random number is between0.0and1.0with1.0included. Second form: the boolean flag controls whether or not1.0is included in the result. If the argument isfalse, the1.0value is guaranteed to be excluded from the result. If the argument istrue,1.0is a possible random real value. Calling this function with the argument set totrueis equivalent to the first form without argument. π¦get-random-relatif->Relatif(Integer|IntegerBoolean) β returns a n bits random positive relatif number. First form: the argument is the number of bits. Second form: the first argument is the number of bits, and the second argument produces an odd number whentrue, or an even number whenfalse.get-random-prime->Relatif(Integer) β returns a n bits random positive relatif probable prime number. The argument is the number of bits. The prime is generated using the Miller-Rabin primality test, so the returned number is declared probable prime. The more bits you need, the longer it takes to generate such a number. π (be patient, queen)get-random-bitset->Bitset(Integer) β returns a n bits random bitset. The argument is the number of bits.fermat-p->Boolean(Integer|RelatifInteger|Relatif) β returnstrueif the little fermat theorem is validated. The first argument is the base number, the second argument is the prime number to validate.miller-rabin-p->Boolean(Integer|RelatifInteger|Relatif) β returnstrueif the Miller-Rabin test is validated. The first argument is the base number, the second argument is the prime number to validate.prime-probable-p->Boolean(Integer|Relatif[Integer]) β returnstrueif the argument is a probable prime. First form: just an integer or relatif number, that's it. Second form: the number of iterations is specified as the second argument. By default, the number of iterations is specified to56. β¨
okay that's the whole vibe check, bestie π this page ships as part of the AFNIX Module documentation, under the AFNIX banner, and it lives in section mth(3) β go forth and vectorize responsibly, ily ππ¦
π¦ where this page comes from
- debian trixie: afnix 3.8.0-1.1 Β· AFNIX
- ubuntu noble: afnix 3.8.0-1 Β· AFNIX
- ubuntu resolute: afnix 3.8.0-1.1ubuntu1 Β· AFNIX
- ubuntu stonking: afnix 3.8.0-1.1ubuntu1 Β· AFNIX
- Copyright (c) 1999-2023 Amaury Darsch
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms