math-utils

  1. math-utils
  2. Documentation
    1. Miscellaneous Math Functions
      1. Usage
      2. log-with-base
      3. log/base
      4. coprime?
      5. pairwise-coprime?
      6. fxcoprime?
      7. simple-interest
      8. compound-interest
      9. fibonacci
      10. *fibonacci
      11. fibonacci/memo
      12. *fibonacci/memo
      13. fibonacci/approximate
      14. binomial
      15. cross-ratio
      16. chinum
      17. *chinum
      18. square
      19. sqr
      20. cube
      21. cub
      22. average
      23. least-squares
      24. trapezoid
      25. factorial
      26. *factorial
      27. factorial/memo
      28. *factorial/memo
      29. factorial-
      30. *factorial-
      31. factorial+
      32. *factorial+
      33. harmonic
      34. *harmonic
      35. harmonic/memo
      36. *harmonic/memo
      37. euclidian-distance
      38. manhattan-distance
      39. to-places
      40. @prec
      41. big-pi
      42. big-sigma
      43. prod
      44. summ
    2. Miscellaneous Vector Math Functions
      1. Usage
      2. absolute-magnitude
      3. cosine-similarity
      4. vector-compare
      5. vector-compare2
      6. dot-product
      7. cross-product
      8. vector-sum
      9. softmax
      10. softmax*
      11. vector-apply
  3. Requirements
  4. Author
  5. Repository
  6. Version history
  7. License

Documentation

Miscellaneous Math Functions

Usage

(import math-utils)

log-with-base

log/base

[procedure] (log-with-base B) -> (real -> real)
[procedure] (log/base B) -> (real -> real)

Returns a function for the base B logarithm.

coprime?

[procedure] (coprime? M [N0 ...]) -> boolean

Are the integers M N0 ... coprime?

pairwise-coprime?

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

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

fxcoprime?

[procedure] (fxcoprime? M N) -> boolean

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

[procedure] (simple-interest RATE TIME [PRIN]) -> number

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

[procedure] (compound-interest RATE FREQ TIME [PRIN]) -> number

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

[procedure] (fibonacci N) -> integer
[procedure] (*fibonacci N) -> integer
[procedure] (fibonacci/memo N) -> integer
[procedure] (*fibonacci/memo N) -> integer

Returns fibonacci of integer N.

fibonacci/approximate

[procedure] (fibonacci/approximate N) -> real

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

binomial

[procedure] (binomial N1 N2) -> integer

Returns the Binomial in N1 to N2.

N1
integer
N2
integer

cross-ratio

[procedure] (cross-ratio N1 N2 N3 N4) -> number

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

N1
number
N2
number
N3
number
N4
number

chinum

*chinum

[procedure] (chinum N) -> fixnum
[syntax] (*chinum N) -> fixnum

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

square

sqr

[procedure] (square N) -> number
[syntax] (sqr N) -> number

cube

cub

[procedure] (cube N) -> number
[syntax] (cub N) -> number

average

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

least-squares

[procedure] (least-squares PNTS) -> number number

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

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

trapezoid

[procedure] (trapezoid F N1 N2) -> (fixnum -> number)

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

factorial

*factorial

factorial/memo

*factorial/memo

[procedure] (factorial N) -> integer
[procedure] (*factorial N) -> integer
[procedure] (factorial/memo N) -> integer
[procedure] (*factorial/memo N) -> integer
N
integer

factorial-

*factorial-

[procedure] (factorial- N FALL) -> real
[procedure] (*factorial- N FALL) -> real

The falling factorial.

N
real
FALL
integer

factorial+

*factorial+

[procedure] (factorial+ N RISE) -> real
[procedure] (*factorial+ N RISE) -> real

The rising factorial.

N
real
RISE
integer

harmonic

*harmonic

harmonic/memo

*harmonic/memo

[procedure] (harmonic N) -> real
[procedure] (*harmonic N) -> real

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

Result of Harmonic series expansion to N terms.

N
integer

euclidian-distance

[procedure] (euclidian-distance X1 Y1 X2 Y2) -> number

Distance between X1,Y1 and X2,Y2.

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

manhattan-distance

[procedure] (manhattan-distance X1 Y1 X2 Y2) -> number

Distance between X1,Y1 and X2,Y2.

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

to-places

@prec

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

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

[syntax] (big-pi F N1 N2) -> number

Product of F in N1 to N2.

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

big-sigma

[syntax] (big-sigma F N1 N2) -> number

Sum of F in N1 to N2.

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

prod

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

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

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

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

[procedure] (absolute-magnitude NUMVEC) -> number
NUMVEC
(vector-of number) : .

cosine-similarity

[procedure] (cosine-similarity NUMVEC1 NUMVEC2) -> number
NUMVEC1
(vector-of number) : .
NUMVEC2
(vector-of number) : .

Must be same vector-length.

vector-compare

vector-compare2

[procedure] (vector-compare NUMVEC ...) -> number
[procedure] (vector-compare2 NUMVEC NUMVEC) -> number

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

NUMVEC
(vector-of number) : .

dot-product

[procedure] (dot-product NUMVEC1 NUMVEC2) -> number
NUMVEC1
(vector-of number) : .
NUMVEC2
(vector-of number) : .

Must be same vector-length.

cross-product

[procedure] (cross-product NUMVEC1 NUMVEC2) -> (or number (vector-of number))

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

[procedure] (vector-sum NUMVEC) -> number

softmax

[procedure] (softmax NUMVEC) -> (vector-of number)

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

softmax*

[procedure] (softmax* NUMVEC [TEMP]) -> (vector-of number)

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

[procedure] (vector-apply FUNC VEC1 VEC2 [VEC...]) -> vector
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))

Requirements

memoize miscmacros

test test-utils vector-lib

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.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.