math-utils

  1. math-utils
  2. Documentation
    1. Miscellaneous Math Functions
      1. Usage
      2. number->sign
      3. log-with-base
      4. log/base
      5. coprime?
      6. pairwise-coprime?
      7. fxcoprime?
      8. simple-interest
      9. compound-interest
      10. fibonacci
      11. fibonacci*
      12. binomial
      13. cross-ratio
      14. square
      15. cube
      16. average
      17. least-squares
      18. trapezoid
      19. factorial
      20. factorial-
      21. factorial+
      22. harmonic
      23. big-pi
      24. big-sigma
    2. Miscellaneous Vector Math Functions
      1. Usage
      2. absolute-magnitude
      3. cosine-similarity
      4. vector-compare
      5. dot-product
      6. cross-product
      7. vector-mul
      8. vector-sum
      9. vector-div
      10. vector-dif
      11. vector-min
      12. vector-max
      13. vector-lcm
      14. vector-gcd
      15. vector-apply
  3. Requirements
  4. Author
  5. Repository
  6. Version history
  7. License

Documentation

Miscellaneous Math Functions

Usage

(import math-utils)

number->sign

[procedure] (number->sign N) -> fixnum

Returns -1, 0, or +1 to represent the sign of the number N.

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

[procedure] (fibonacci N) -> number

Returns fibonacci of N.

fibonacci*

[procedure] (fibonacci* N) -> number

Returns an approximate fibonacci of N.

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

square

[procedure] (square N) -> number

cube

[procedure] (cube N) -> number

average

[procedure] (average N1 N2 ...) -> number
[procedure] (average NUMS) -> number
N1 ; number
N1 ; number
NUMS ; (list-of 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+

[procedure] (factorial N) -> number
[procedure] (factorial- N1 FALL) -> number
[procedure] (factorial+ N1 RISE) -> number
N ; integer
FALL
number : falling factorial
RISE
number : rising factorial

harmonic

[procedure] (harmonic N) -> number

Result of Harmonic series expansion to N terms.

N ; integer

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 ;

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

[procedure] (vector-compare 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))
NUMVEC1 ; (vector-of number)
.
NUMVEC2 ; (vector-of number)
.

Must be same vector-length.

Only defined for a vector-length of (0 1 2), returning a number, and (3 4 8), returning a (vector-of number). All others generate an error.

vector-mul

vector-sum

vector-div

vector-dif

vector-min

vector-max

vector-lcm

vector-gcd

[procedure] (vector-mul NUMVEC...) -> (vector-of number)
[procedure] (vector-sum NUMVEC...) -> (vector-of number)
[procedure] (vector-div NUMVEC...) -> (vector-of number)
[procedure] (vector-dif NUMVEC...) -> (vector-of number)
[procedure] (vector-min NUMVEC...) -> (vector-of number)
[procedure] (vector-max NUMVEC...) -> (vector-of number)
[procedure] (vector-lcm NUMVEC...) -> (vector-of number)
[procedure] (vector-gcd NUMVEC...) -> (vector-of number)
NUMVEC ; (vector-of number)
.

Must be same vector-length.

vector-apply

[procedure] (vector-apply FUNC VEC1 VEC2 [VEC...]) -> vector
FUNC ; (* * !#rest * -> *)
.
VEC# ; vector
.

Must be same vector-length.

Requirements

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