chickadee » math-utils

math-utils

Documentation

Miscellaneous Math Functions

Usage

(import math-utils)

log-with-base

log/base

log-with-base Bprocedure
log/base Bprocedure

Returns a function for the base B logarithm.

coprime?

(coprime? M [N0 ...]) -> booleanprocedure

Are the integers M N0 ... coprime?

pairwise-coprime?

(pairwise-coprime? M [N0 ...]) -> booleanprocedure

Are the pairs of integers in M N0 ... coprime?

fxcoprime?

fxcoprime? M Nprocedure

Are the fixnums M N coprime?

(import (only (srfi 1) filter iota))
(import (only (math-utils) fxcoprime?))

(define (coprimes n)
  (filter (cut fxcoprime? n <>) (iota (- n 1) 1)) )
(import (only (streams derived) stream-range stream-filter))
(import (only (math-utils) fxcoprime?))

(define (coprime-numbers-stream n)
  (stream-filter (cut fxcoprime? n <>) (stream-range 1 n)) )

simple-interest

simple-interest RATE TIME #!optional PRINprocedure

The accumulation function, the principle is assumed 1. Returns the simple interest for the RATE over TIME.

RATE
number ; interest rate
TIME
number ; time period to cover
PRIN
number ; principle, default 1

compound-interest

compound-interest RATE FREQ TIME #!optional PRINprocedure

The accumulation function, the principle is assumed 1. Returns the compound interest for the RATE, applied with FREQ, over TIME.

RATE
number ; interest rate
FREQ
number ; compounding frequency
TIME
number ; time period to cover
PRIN
number ; principle, default 1
(compound-interest 0.043 4 6 1500)
;=> 1938.84 ;rounded to 2 places

fibonacci

*fibonacci

fibonacci/memo

*fibonacci/memo

fibonacci Nprocedure
*fibonacci Nprocedure
fibonacci/memo Nprocedure
*fibonacci/memo Nprocedure

Returns fibonacci of integer N.

fibonacci/approximate

fibonacci/approximate Nprocedure

Returns an approximate fibonacci of integer N, with an error of > 1 at N ~= 70.

binomial

binomial N1 N2procedure

Returns the Binomial in N1 to N2.

N1
integer
N2
integer

cross-ratio

cross-ratio N1 N2 N3 N4procedure

Returns the Cross-ratio of N1, N2 and N3, N4.

N1
number
N2
number
N3
number
N4
number

~=

(~= A B [EPSILON 1e-05]) -> booleanprocedure

Does A approximately equal B?

chinum

*chinum

chinum Nprocedure
(*chinum N) -> fixnumsyntax

Returns 1 for even? and -1 for odd; (expt -1 N).

square

sqr

square Nprocedure
(sqr N) -> numbersyntax

cube

cub

cube Nprocedure
(cub N) -> numbersyntax

*average

*average LSprocedure
LS
(list-of number)

average

(average [N0 ...]) -> numberprocedure
N0
number

average*

(average* [TREE ...]) -> numberprocedure
TREE
(tree-of number)

least-squares

least-squares PNTSprocedure

Returns b & e such that y ~= b * x + e.

PNTS
(list-of (pair number number)) : list of x,y pairs

trapezoid

trapezoid F N1 N2procedure

Returns a function to calculate the area under F between N1 & N2 using the Trapezoid Rule. The function takes the number of estimations as an argument, larger for a "better" result.

F
(number -> number)
N1
number
N2
number

euclidian-distance

euclidian-distance X1 Y1 X2 Y2procedure
X1 Y1
number number ; start point
X2 Y2
number number ; end point

manhattan-distance

manhattan-distance X1 Y1 X2 Y2procedure
X1 Y1
number number ; start point
X2 Y2
number number ; end point

factorial

*factorial

factorial/memo

*factorial/memo

factorial Nprocedure
*factorial Nprocedure
factorial/memo Nprocedure
*factorial/memo Nprocedure
N
integer

factorial-

*factorial-

factorial- N FALLprocedure
*factorial- N FALLprocedure

The falling factorial.

N
real
FALL
integer

factorial+

*factorial+

factorial+ N RISEprocedure
*factorial+ N RISEprocedure

The rising factorial.

N
real
RISE
integer

harmonic

*harmonic

harmonic/memo

*harmonic/memo

harmonic Nprocedure
*harmonic Nprocedure

procedure>(harmonic/memo N) -> real</procedure> <procedure>(*harmonic/memo N) -> real</procedure>

Result of Harmonic series expansion to N terms.

N
integer

euclidian-distance

euclidian-distance X1 Y1 X2 Y2procedure

Distance between X1,Y1 and X2,Y2.

X1 Y1
number number ; from point
X2 Y2
number number ; to point

manhattan-distance

manhattan-distance X1 Y1 X2 Y2procedure

Distance between X1,Y1 and X2,Y2.

X1 Y1
number number ; from point
X2 Y2
number number ; to point

to-places

@prec

(to-places PREC (FUNC EXPR))syntax
(@prec PREC EXPR [FUNC truncate])procedure

Truncate the result of (FUNC EXPR) to PREC decimal places.

PREC
fixnum ; precision
FUNC
symbol ; clip operation, i.e. round, etc.
EXPR
* ; value to clip

big-pi

(big-pi F N1 N2) -> numbersyntax

Product of F in N1 to N2.

F
(number -> number) ;
N1
number ;
N2
number ;

big-sigma

(big-sigma F N1 N2) -> numbersyntax

Sum of F in N1 to N2.

F
(number -> number) ;
N1
number ;
N2
number ;

prod

(prod VAR ([N1 [N2 [INC]]]) BODY...) -> numbersyntax

Product of BODY... in N1 to N2 by INC.

VAR
symbol ; VAR bound to current N in BODY...
BODY...
number ;
N1
number ;
N2
number ;
INC
number ;

summ

(summ VAR ([N1 [N2 [INC]]]) BODY...) -> numbersyntax

Sum of BODY... in N1 to N2 by INC.

VAR
symbol ; VAR bound to current N in BODY...
BODY...
number ;
N1
number ;
N2
number ;
INC
number ;

Miscellaneous Vector Math Functions

Usage

(import (math-utils vector))

absolute-magnitude

absolute-magnitude NUMVECprocedure
NUMVEC
(vector-of number) : .

cosine-similarity

cosine-similarity NUMVEC1 NUMVEC2procedure
NUMVEC1
(vector-of number) : .
NUMVEC2
(vector-of number) : .

Must be same vector-length.

vector-compare

vector-compare2

vector-compare NUMVEC ...procedure
vector-compare2 NUMVEC NUMVECprocedure

Result is negative, zero, or positive. Comparison by length when unequal & element-wise when equal.

NUMVEC
(vector-of number) : .

dot-product

dot-product NUMVEC1 NUMVEC2procedure
NUMVEC1
(vector-of number) : .
NUMVEC2
(vector-of number) : .

Must be same vector-length.

cross-product

cross-product NUMVEC1 NUMVEC2procedure

Only defined for a vector-length of (0 1 2 3 4 8). All others generate an error. Must be same vector-length.

NUMVEC1
(vector-of number) : .
NUMVEC2
(vector-of number) : .

vector-sum

vector-sum NUMVECprocedure

softmax

softmax NUMVECprocedure

Returns the softmax of the vector elements. Normalize the vector elements so (= 1 (vector-sum NUMVEC)).

softmax*

softmax* NUMVEC #!optional TEMPprocedure

Returns the softmax of the tempered vector elements. Spread, (< 1 TEMP), or shrink, (> 1 TEMP), data before normalization. A TEMP of 1 has no effect.

TEMP
real ; temperature, default 1.

Note that a TEMP near zero is currently an error!

vector-apply

vector-apply FUNC VEC1 VEC2 #!optional VEC...procedure
FUNC
(* * !#rest * -> *) : .
VEC#
vector : .

Must be same vector-length.

(import (only (math-utils vector) vector-apply))

(define (vector-mul . vs) (apply vector-apply * vs))
(define (vector-add . vs) (apply vector-apply + vs))
(define (vector-dif . vs) (apply vector-apply - vs))

vector-scale

vector-shift

vector-scale VEC FACTORprocedure
vector-shift VEC BIASprocedure

normalize-vector-scale

normalize-vector-shift

normalize-vector-scale VECprocedure
normalize-vector-shift VECprocedure

vector-scale by the (/ (vector-maximum)). vector-shift by the (- (vector-minimum)).

Examples

(import scheme)
(import (chicken base))
(import (rename (only math-utils
                  summ sqr
                  ;non-checking & memoized version is used
                  *chinum factorial/memo *factorial+/memo)
                (*chinum chinum)
                (factorial/memo factorial)
                (*factorial+/memo factorial+)))

;https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.132.221601
;@lam `lambda' param
(define (saha-sinha-pi #!optional (lim 100) (lam 4))
  (+ 4 (summ n (1 lim)
             (let* ((2n (* 2 n)) (2n+1 (+ 2n 1)))
               (* (/ 1 (factorial n))
                  (- (/ 1 (+ n lam)) (/ 4 2n+1))
                  (factorial+ (- (/ (sqr 2n+1) (+ (* 2 2n) (* 4 lam))) n)
                              (- n 1)))))) )

(define (madhava-leibniz-pi #!optional (lim 100))
  (* 4 (summ n (1 lim) (/ (chinum (- n 1)) (- (* 2 n) 1)))) )

(- (acos -1) (saha-sinha-pi))       ;=> 1.90993887372315e-11
(- (acos -1) (madhava-leibniz-pi))  ;=> -0.0101007524813226

Requirements

memoize miscmacros vector-lib

test test-utils

Author

Kon Lovett

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/math-utils

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Version history

1.6.0
Add ~=, vector-scale, vector-shift, normalize-vector-scale, normalize-vector-shift.
1.5.3
factorial+-/memo.
1.5.2
.
1.5.1
Add *average, average*. Fix signatures.
1.5.0
Add softmax*, chinum, *chinum, summ, prod, sqr, cub, *... (no type checks) & ../memo (memoized) series routines. Rename *fibonacci fibonacci/approximate
1.4.0
Add softmax. Allow real arguments to factorial+, factorial-, harmonic. Add euclidian-distance & manhattan-distance.
1.3.2
Fix cross-product return.
1.3.1
Fix average signature.
1.3.0
cross-product returns number-vector. Add vector-compare2. Rename fibonacci*. Drop deprecated.
1.2.0
Add to-places. Deprecate with-places.
1.1.0
Add with-places.
1.0.6
Remove mathh dependency since include is unreliable.
1.0.5
Use gcd based algorithm.
1.0.4
Add fxcoprime?.
1.0.3
*coprime? too slow.
1.0.2
Fix mathh dependency.
1.0.1
Add vector-compare.
1.0.0
From mathh:4.6.5.

License

This code is in the public domain.

Contents »