Skip to content

Commit

Permalink
Merge pull request #50 from PESchoenberg/develop
Browse files Browse the repository at this point in the history
Added functions.
  • Loading branch information
PESchoenberg authored Apr 3, 2020
2 parents 9600c6c + 264b00b commit 02ba942
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 3 deletions.
28 changes: 26 additions & 2 deletions grsp2.scm
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
grsp-method-newton
grsp-method-euler
grsp-lerp
grsp-givens-rotation))
grsp-givens-rotation
grsp-fitin))


; grsp-gtels - Finds if p_n1 is greater, equal or smaller than p_n2.
Expand Down Expand Up @@ -545,7 +546,7 @@
res))


; grsp-fermat-number - Produces a Fermat numbernumber.
; grsp-fermat-number - Produces a Fermat number.
;
; Arguments:
; - p_n: non-negative integer.
Expand Down Expand Up @@ -758,3 +759,26 @@

res1))


; grsp-fitin - truncates p_n1 if it does not fit in the interval [p_nmin, p_nmax].
;
; Arguments:
; - p_n1: real.
; - p_nmin: lower bounday of interval.
; - p_nmax: higher boundary of th interval.
;
; Output:
; - p_n1 if it is in [p_nmin,p:nmax]
; - p_nmin if p_n1 < p_nmin.
; - p_nmax if p_n1 > p_nmax.
;
(define (grsp-fitin p_n1 p_nmin p_nmax)
(let ((res1 p_n1))

(cond ((> p_n1 p_nmax)
(set! res1 p_nmax))
((< p_n1 p_nmin)
(set! res1 p_nmin)))

res1))

23 changes: 22 additions & 1 deletion grsp3.scm
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@
; - "#CH": 0-1 checkerboard pattern matrix.
; - "#+IJ": matrix containing the sum of i and j values.
; - "#-IJ": matrix containing the substraction of i and j values.
; - "#+IJ": matrix containing the product of i and j values.
; - "#-IJ": matrix containing the quotient of i and j values.
; - "#US": upper shift matrix.
; - "#LS": lower shift matrix.

; - p_m: rows, positive integer.
; - p_n: cols, positive integer.
Expand Down Expand Up @@ -206,6 +210,12 @@
((equal? p_s "#/IJ")
(set! s 1)
(set! n m))
((equal? p_s "#US")
(set! s 0)
(set! n m))
((equal? p_s "#LS")
(set! s 0)
(set! n m))

(else (set! s p_s)))

Expand Down Expand Up @@ -330,6 +340,17 @@
(array-set! res (/ i j) i j)))
(set! j (+ j 1)))
(set! i (+ i 1))))
((equal? p_s "#US")
(while (< i m)
(set! j 0)
(while (< j (- n 1))
(cond ((eq? i j)
(array-set! res 1 i (+ j 1))))
(set! j (+ j 1)))
(set! i (+ i 1))))
((equal? p_s "#LS")
(set! res (grsp-matrix-create "#US" m n))
(set! res (grsp-matrix-transpose res)))

((equal? p_s "#Q")
(array-set! res -1 (- m 2) (- n 1))))))))
Expand Down Expand Up @@ -2066,4 +2087,4 @@

res1))


209 changes: 209 additions & 0 deletions grsp5.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
; ==============================================================================
;
; grsp5.scm
;
; Statisitical and probabilistic functions.
;
; ==============================================================================
;
; Copyright (C) 2018 Pablo Edronkin (pablo.edronkin at yahoo.com)
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU Lesser General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU Lesser General Public License for more details.
;
; You should have received a copy of the GNU Lesser General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
;
; ==============================================================================

; https://en.wikipedia.org/wiki/Probability

(define-module (grsp grsp5)
#:use-module (grsp grsp0)
#:use-module (grsp grsp1)
#:use-module (grsp grsp2)
#:use-module (grsp grsp3)
#:use-module (grsp grsp4)
#:export (grsp-feature-scaling
grsp-z-score
grsp-binop
grsp-pnot
grsp-pand
grsp-pnand
grsp-por
grsp-pxor
grsp-pcond))


; grsp-feature-scaling - scales (normalizes) p_n between [0,1].
;
; Arguments:
; - p_n: scalar.
; - p_nmin: min value for p_n.
; - p_max: max value for p_x.
;
; Sources:
; - https://www.statisticshowto.datasciencecentral.com/normalized/
;
(define (grsp-feature-scaling p_n p_nmin p_nmax)
(let ((res1 0))

(set! res1 (* 1.0 (/ (- p_n p_nmin) (- p_nmax p_nmin))))
res1))


; grsp-z-score - calculates the z score for a sample data point.
;
; Arguments:
; - p_n1: data point.
; - p_m1: sample mean.
; - p_s1: sample standard deviation.
;
; Sources:
; - https://www.statisticshowto.datasciencecentral.com/normalized/
;
(define (grsp-z-score p_n1 p_m1 p_s1)
(let ((res1 0))

(set! res1 (* 1.0 (/ (- p_n1 p_m1) p_s1)))
res1))


; grsp-binop - performs operation p_s1 on p_n1 and p_n2 and calculates the p_n3
; power of that binomial operation.
;
; Arguments:
; - p_s1: string determining the operation.
; - "#+": sum.
; - "#-": substraction.
; - "#*": multiplication.
; - "#/": division.
; - p_n1: real.
; - p_n2: real.
; - p_n3: real.
;
(define (grsp-binop p_s1 p_n1 p_n2 p_n3)
(let ((res1 0))

(cond ((equal? p_s1 "#+")
(set! res1 (expt (+ p_n1 p_n2) p_n3)))
((equal? p_s1 "#-")
(set! res1 (expt (- p_n1 p_n2) p_n3)))
((equal? p_s1 "#*")
(set! res1 (expt (- p_n1 p_n2) p_n3)))
((equal? p_s1 "#/")
(set! res1 (expt (- p_n1 p_n2) p_n3))))

res1))


; grsp-pnot - calculates the complementary probability of p_n1.
;
; Arguments:
;- p_n1: real representing a probability in [0,1]
;
(define (grsp-pnot p_n1)
(let ((res1 1))

(set! res1 (- res1 (grsp-fitin p_n1 0.0 1.0)))

res1))


; grsp-pand - calculates the probability of p_n1 and p_n2 happening,
; being independent.
;
; Arguments:
;- p_n1: real repesenting a probability in [0,1]
;- p_n2: real repesenting a probability in [0,1]
;
(define (grsp-pand p_n1 p_n2)
(let ((res1 1))

(set! res1 (* (grsp-fitin p_n1 0.0 1.0) (grsp-fitin p_n2 0.0 1.0)))

res1))


; grsp-pnand - calculates the probability of p_n1 and p_n2 happening,
; being not independent.
;
; Arguments:
;- p_n1: real repesenting a probability in [0,1]
;- p_n2: real repesenting a probability in [0,1]
;
(define (grsp-pnand p_n1 p_n2)
(let ((res1 1)
(n1 0)
(n2 0))

(set! n1 (grsp-fitin p_n1 0.0 1.0))
(set! n2 (grsp-fitin p_n2 0.0 1.0))
(set! res1 (* (grsp-pcond n1 n2) n2))

res1))


; grsp-por - calculates the probability of p_n1 or p_n2 happening.
;
; Arguments:
;- p_n1: real repesenting a probability in [0,1]
;- p_n2: real repesenting a probability in [0,1]
;
(define (grsp-por p_n1 p_n2)
(let ((res1 1)
(n1 0)
(n2 0))

(set! n1 (grsp-fitin p_n1 0.0 1.0))
(set! n2 (grsp-fitin p_n2 0.0 1.0))
(set! res1 (- (+ n1 n2) (grsp-pand n1 n2)))

res1))


; grsp-pxor - calculates the probability of p_n1 or p_n2 happening,
; beign mutually exclusive.
;
; Arguments:
;- p_n1: real repesenting a probability in [0,1]
;- p_n2: real repesenting a probability in [0,1]
;
(define (grsp-pxor p_n1 p_n2)
(let ((res1 0)
(n1 0)
(n2 0))

(set! n1 (grsp-fitin p_n1 0.0 1.0))
(set! n2 (grsp-fitin p_n2 0.0 1.0))
(cond ((<= (+ n1 n2) 1)
(set! res1 (+ n1 n2))))

res1))


; grsp-pcond - calculates the probability of p_n1 given p_n2 happening.
;
; Arguments:
;- p_n1: real repesenting a probability in [0,1]
;- p_n2: real repesenting a probability in [0,1]
;
(define (grsp-pcond p_n1 p_n2)
(let ((res1 0)
(n1 0)
(n2 0))

(set! n1 (grsp-fitin p_n1 0.0 1.0))
(set! n2 (grsp-fitin p_n2 0.0 1.0))
(cond ((> n2 0)
(set! res1 (/ (grsp-pand n1 n2) n2))))

res1))

0 comments on commit 02ba942

Please sign in to comment.