User:Spekkio: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 33: | Line 33: | ||
maclaurin series for different functions sin/cos/sqrt/pow etc... (see my recent post in [[Talk:Nth root]]) |
maclaurin series for different functions sin/cos/sqrt/pow etc... (see my recent post in [[Talk:Nth root]]) |
||
<lang lisp>(defun pie() 31415926535897932384626433832795028841971693993751058209749/10000000000000000000000000000000000000000000000000000000000) |
|||
<lang lisp>(defun pie() 31415926535897932385/10000000000000000000) |
|||
(defun factorial (n) |
(defun factorial (n) |
||
Line 51: | Line 51: | ||
(defun binom (alpha n) |
(defun binom (alpha n) |
||
(if (= n 0) |
|||
1 |
|||
(if (= n 1) |
(if (= n 1) |
||
alpha |
alpha |
||
(* (binom alpha (- n 1)) (/ (+ (- alpha n) 1) n)))) |
(* (binom alpha (- n 1)) (/ (+ (- alpha n) 1) n)))) |
||
) |
|||
(defun power (a b n) |
(defun power (a b n) |
||
(if (= n 0) |
(if (= n 0) |
||
1 |
1 |
||
(+ |
|||
(+ (* (binom b n) (powint (- a 1) n)) (power a b (- n 1))))) |
|||
(* (binom b n) (powint (- a 1) n)) |
|||
(power a b (- n 1))))) |
|||
(defun |
(defun powerw(a b n prev) |
||
(let (( |
(let ((cosn (* (binom b n) (powint (- a 1) n)) |
||
)) |
|||
(if |
|||
(handler-case |
|||
(= (* power10 1.0) (* prev 1.0)) |
|||
(progn |
|||
power10 |
|||
(float cosn) |
|||
(powertest a b (+ 10 n) power10) |
|||
) |
(if (= (+ prev cosn) prev ) prev |
||
(powerw a b (1+ n) (+ prev cosn))) |
|||
) |
|||
(arithmetic-error (x) prev )) |
|||
) |
) |
||
) |
) |
||
Line 74: | Line 82: | ||
(if (< b 1) |
(if (< b 1) |
||
0 |
0 |
||
( |
(powerw a b 0 0) |
||
) |
) |
||
(if (< a 2) |
(if (< a 2) |
||
( |
(powerw a b 0 0) |
||
0 |
0 |
||
) |
) |
||
Line 83: | Line 91: | ||
) |
) |
||
(defun logw(x n prev) |
|||
(defun logS(x n) (if (= n 0) (* 2 x) (+ (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1))) (logS x (- n 1)) ) )) |
|||
(let ((cosn (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1))) |
|||
)) |
|||
(defun loge(x n) (logS (/ (- x 1) (+ 1 x)) n)) |
|||
(handler-case |
|||
(progn |
|||
(defun expon(x n) (if (= n 0) 1 (+ (/ (powint x n) (factorial n)) (expon x (- n 1))))) |
|||
(float cosn) |
|||
( |
(logw x (1+ n) (+ prev cosn)) |
||
) |
|||
( |
(arithmetic-error (x) prev )) |
||
(let (( square10 (squaren a (+ 10 n)) )) |
|||
(if |
|||
(= (* square10 1.0) (* prev 1.0)) |
|||
square10 |
|||
(squarentest a (+ 10 n) square10) |
|||
) |
|||
) |
) |
||
) |
) |
||
(defun |
(defun logS(x n) (if (= n 0) (* 2 x) |
||
(+ (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1))) |
|||
(logS x (- n 1)) ) )) |
|||
(defun |
(defun loge(x) (logw (/ (- x 1) (+ 1 x)) 0 0) ) |
||
(defun |
(defun logetest(a n prev) |
||
(let ((n10 (+ 10 n))) |
(let ((n10 (+ 10 n))) |
||
(let (( |
(let (( log10 (loge a n10) )) |
||
(if |
(if |
||
(= ( |
(= (float log10) (float prev)) |
||
log10 |
|||
( |
(logetest a n10 log10) |
||
) |
) |
||
) |
) |
||
)) |
)) |
||
(defun |
(defun ln (x) |
||
(if (> x 0) |
|||
(loge x) |
|||
nil |
|||
)) |
|||
(defun |
(defun expw(x n prev) |
||
( |
(let ((cosn (/ (powint x n) (factorial n)) |
||
)) |
|||
x |
|||
(handler-case |
|||
(+ (* (/ (powint -1 n) (factorial (+ (* 2 n) 1)) ) (powint x (+ (* 2 n) 1))) (sine x (- n 1))))) |
|||
(progn |
|||
(float cosn) |
|||
(defun sinetest(x n) |
|||
(expw x (1+ n) (+ prev cosn)) |
|||
(if |
|||
) |
|||
(= (* (sine x (+ 10 n)) 1.0) (* (sine x n) 1.0)) |
|||
(arithmetic-error (x) prev )) |
|||
(sine x (+ 10 n)) |
|||
) |
|||
(sinetest x (+ 10 n)) |
|||
) |
|||
) |
) |
||
(defun |
(defun expon(x) (expw x 0 0)) |
||
(defun |
(defun squareroot(x) (expon (* 1/2 (loge x)))) |
||
(defun |
(defun powern(x a) (expon (* a (loge x)))) |
||
(defun |
(defun e (x) (expon x)) |
||
(if (= n 0) |
|||
x |
|||
(+ (/ (* (factorial (* 2 n)) (powint x (+ (* 2 n) 1))) (* (powint 4 n) (powint (factorial n) 2) (+ (* 2 n) 1))) (asine x (- n 1))))) |
|||
(defun pown (a b) (powern a b)) |
|||
(defun |
(defun sinew(x n prev) |
||
(let (( |
(let ((cosn (* (/ (powint -1 n) (factorial (+ (* 2 n) 1)) ) (powint x (+ (* 2 n) 1))) |
||
)) |
|||
(if |
|||
(handler-case |
|||
(= (* asine10 1.0) (* prev 1.0)) |
|||
(progn |
|||
asine10 |
|||
(float cosn) |
|||
(asinetest x (+ 10 n) asine10) |
|||
) |
(sinew x (1+ n) (+ prev cosn)) |
||
) |
|||
(arithmetic-error (x) (+ prev cosn) )) |
|||
) |
|||
) |
|||
(defun sine(x) (sinew x 0 0)) |
|||
(defun cosinew(x n prev) |
|||
(let ((cosn (* (/ (powint -1 n) (factorial (* 2 n))) (powint x (* 2 n))))) |
|||
(handler-case |
|||
(progn |
|||
(float cosn) |
|||
(cosinew x (1+ n) (+ prev cosn)) |
|||
) |
|||
(arithmetic-error (x) (+ prev cosn) )) |
|||
) |
|||
) |
|||
(defun cosine(x) (cosinew x 0 0)) |
|||
(defun tang(x) (/ (sine x) (cosine x))) |
|||
(defun asinew(x n prev) |
|||
(let ((cosn (/ (* (factorial (* 2 n)) (powint x (+ (* 2 n) 1))) (* (powint 4 n) (powint (factorial n) 2) (+ (* 2 n) 1))) |
|||
)) |
|||
(handler-case |
|||
(progn |
|||
(float cosn) |
|||
(asinew x (1+ n) (+ prev cosn)) |
|||
) |
|||
(arithmetic-error (x) prev )) |
|||
) |
) |
||
) |
) |
||
(defun |
(defun asine (x) |
||
(if (< x 1) |
(if (< x 1) |
||
(if (> x -1) |
(if (> x -1) |
||
( |
(asinew x 0 0) |
||
(/ (pie) -2) |
(/ (pie) -2) |
||
) |
) |
||
Line 178: | Line 214: | ||
test: |
test: |
||
test: |
test: |
||
[14]> ( |
[14]> (float (asine 9/100)) |
||
0.09012195 |
0.09012195 |
||
[14]> (asin 9/100) |
[14]> (asin 9/100) |
||
Line 191: | Line 227: | ||
Test 2: |
Test 2: |
||
[4]> ( |
[4]> (float (asine (sine 1))) |
||
1.0 |
1.0 |
||
[4]> (asin (sin 1)) |
[4]> (asin (sin 1)) |
||
Line 197: | Line 233: | ||
Test 3: |
Test 3: |
||
[15]> ( |
[15]> (float (sine (* 45 (/ (* 2 (pie)) 360) ))) |
||
0.70710677 |
0.70710677 |
||
[15]> ( |
[15]> (float (squareroot 1/2)) |
||
0.70710677 |
0.70710677 |
||
[15]> (= ( |
[15]> (= (float (squareroot 1/2) 1.0) (* (sine (* 45 (/ (* 2 (pie)) 360) )))) |
||
T |
T |