As an easy first step to supplying Qi with a standard library, I've codified most of the maths in CL. The comments below are taken directly from Common Lisp: the Language and explain each function. The presentation has been rationalised in some cases to accomodate Qi type conventions. The entries are not in alphabetical order (yet) reflecting the order they appear in CLTL. plus? : number --> number This predicate is true if number is strictly greater than zero, and is false otherwise. It is an error if the argument number is not a non-complex number. minus? : number --> number This predicate is true if number is strictly less than zero, and is false otherwise. It is an error if the argument number is not a non-complex number. even? : number --> boolean This predicate is true if the argument integer is even (divisible by 2), and otherwise is false. It is an error if the argument is not an integer. odd? : number --> boolean This predicate is true if the argument integer is is odd (not divisible by 2), and otherwise is false. It is an error if the argument is not an integer. min : [number] --> number Returns the minimum of a list of numbers. max : [number] --> number incf symbol delta
Increments and decrements a global variable by given value. These are special forms (i.e. are not regular functions and are not curried or subject to partial application, they have their own type rules). (incf S 4) = (set S (+ (value S) 4)) conjugate : number --> number This returns the complex conjugate of number. gcd : [number] --> number This returns the greatest common divisor of all the elements of the list, which must be integers. The result of gcd is always a non-negative integer. If the list is a unit list, the absolute value of its element is returned. If the list is empty, gcd returns 0. lcm [number] --> number This returns the least common multiple of the elements of its non-empty list argument, which must be integers. The result of lcm is always a non-negative integer. For a one element list, lcm returns the absolute value of that element. exp : number --> number Returns e raised to the power number, where e is the base of the natural logarithms. expt : number --> number --> number Returns base-number raised to the power power-number. If the base-number is of type rational and the power-number is an integer, the calculation will be exact and the result will be of type rational; otherwise a floating-point approximation may result. log : number --> number --> number Returns the logarithm of number in the base base. isqrt : number --> number Integer square root: the argument must be a non-negative integer, and the result is the greatest integer less than or equal to the exact positive square root of the argument. abs : number --> number Returns the absolute value of the argument. phase : number --> number The phase of a number is the angle part of its polar representation as a complex number. That is, (phase Z) = (atan (imagpart Z) (realpart Z)) The result is in radians, in the range -pi (exclusive) to +pi (inclusive). The phase of a positive non-complex number is zero; that of a negative non-complex number is pi. The phase of zero is arbitrarily defined to be zero. sin : number -->
number sin returns the sine of the argument, cos the cosine, and tan the tangent. The argument is in radians. The argument may be complex. cis : number --> number The argument is in radians and may be any non-complex number. The result is a complex number whose real part is the cosine of the argument and whose imaginary part is the sine. Put another way, the result is a complex number whose phase is the equal to the argument (mod 2) and whose magnitude is unity. asin : number -->
number asin returns the arc sine of the argument, and acos the arc cosine. The result is in radians. The argument may be complex. atan : number --> number An arc tangent is calculated and the result is returned in radians. sinh : number -->
number These functions compute the hyperbolic sine, cosine, tangent, arc sine, arc cosine, and arc tangent functions float : number --> number This converts any non-complex number to a floating-point number. rational : number -->
number Each of these functions converts any non-complex number to a rational number. If the argument is already rational, it is returned. The two functions differ in their treatment of floating-point numbers. rational assumes that the floating-point number is completely accurate and returns a rational number mathematically equal to the precise value of the floating-point number. rationalize assumes that the floating-point number is accurate only to the precision of the floating-point representation and may return any rational number for which the floating-point number is the best available approximation of its format; in doing this it attempts to keep both numerator and denominator small. numerator : number -->
number These functions take a rational number (an integer or ratio) and return as an integer the numerator or denominator of the canonical reduced form of the rational. The numerator of an integer is that integer; the denominator of an integer is 1. The denominator will always be a strictly positive integer; the numerator may be any integer. For example: floor : number -->
number Each of these functions converts its argument number (which must not be complex) to an integer. If the argument is already an integer, it is returned directly. If the argument is a ratio or floating-point number, the functions use different algorithms for the conversion. floor converts its argument by truncating toward negative infinity; that is, the result is the largest integer that is not larger than the argument. ceiling converts its argument by truncating toward positive infinity; that is, the result is the smallest integer that is not smaller than the argument. truncate converts its argument by truncating toward zero; that is, the result is the integer of the same sign as the argument and which has the greatest integral magnitude not greater than that of the argument. mod : number -->
number --> number mod performs the operation floor on its two arguments and returns the second result of floor as its only result. Similarly, rem performs the operation truncate on its arguments and returns the second result of truncate as its only result. mod and rem are therefore the usual modulus and remainder functions when applied to two integer arguments. In general, however, the arguments may be integers or floating-point numbers. complex : number --> number --> number The arguments must be non-complex numbers; a number is returned that has realpart as its real part and imagpart as its imaginary part, possibly converted according to the rule of floating-point contagion (thus both components will be of the same type). Note that if both the realpart and imagpart are rational and the imagpart is zero, then the result is just the realpart because of the rule of canonical representation for complex rationals. It follows that the result of complex is not always a complex number; it may be simply a rational. Mark Copyright (c) 2008, Mark
Tarver |
||