;;; Generic Arithmetic

(+ 1 2 3)
;Value: 6

(+ 2/3 7/12)
;Value: 5/4

(+ 2/3 1)
;Value: 5/3

(+ 1/2 0.5)
;Value: 1.

(exact? 2/3)
;Value: #t

(inexact? 2/3)
;Value: #f

(exact->inexact 2/3)
;Value: .6666666666666666

(inexact->exact (exact->inexact 2/3))
;Value: 6004799503160661/9007199254740992
 
(exact->inexact
 (inexact->exact (exact->inexact 2/3)))
;Value: .6666666666666666

(+ 2/3 0.5)
;Value: 1.1666666666666665

(sqrt 2)
;Value: 1.4142135623730951

(square (sqrt 2))
;Value: 2.0000000000000004

(magnitude 3+4i)
;Value: 5

(angle 3+4i)
;Value: .9272952180016122

(make-polar (magnitude 3+4i) (angle 3+4i))
;Value: 
 3.0000000000000004+3.9999999999999996i


(define (sigma f low high)
  (if (> low high)
      0
      (let lp ((i (+ low 1)) (sum (f low)))
	(if (> i high)
	    sum
	    (lp (+ i 1) (+ sum (f i)))))))


(sigma (lambda (x) x) 1 100)
;Value: 5050

(sigma square 1 100)
;Value: 338350

(* 4
   (- 1
      (* 2 
	 (sigma (lambda (n)
		  (/ 1.
		     (* (- (* 4 n) 1)
			(+ (* 4 n) 1))))
		1 100000))))
;Value: 3.141597653564787

;;; But watch out!

(* 4
   (- 1
      (* 2 
	 (sigma (lambda (n)
		  (/ 1
		     (* (- (* 4 n) 1)
			(+ (* 4 n) 1))))
		1 10))))
;Value: 21831981985010836/6845630929362225

;;; Extensions to Vectors over R^n

;;; Addition

(+ #(1 2) #(3 4))
;Value: #(4 6)


;;; Scalar Multiplication

(* 3 #(1 2))
;Value: #(3 6)


;;; Outer Product

(* #(1 2) #(3 4))
;Value: #(#(3 6) #(4 8))

(dot-product #(1 2) #(3 4))
;Value: 11


;;; Scheme Vectors are 
;;;  interpreted as Column Vectors

(pe #(1 2))
(up 1 2)

;;; Matrices

(pe 
 (+ (matrix-by-rows (list 1 2)
		    (list 3 4))
    (matrix-by-rows (list 5 6)
		    (list 7 8))))
(matrix-by-rows (list 6 8)
		(list 10 12))

(pe
 (* (matrix-by-rows (list 1 2 3)
		    (list 5 4 6)
		    (list 7 8 9))
    (up 10 11 12)))
(up 68 166 266)

(pe
 (/ (up 68 166 266)
    (matrix-by-rows (list 1 2 3)
		    (list 5 4 6)
		    (list 7 8 9))))
(up 10 11 12)

;;; Functions

(cos 3)
;Value: -.9899924966004454

(sin 3)
;Value: .1411200080598672

(+ (cos 3) (sin 3))
;Value: -.8488724885405782

((+ cos sin) 3)
;Value: -.8488724885405782

((+ cos (lambda (x) (* x x x))) 3)
;Value: 26.010007503399553

;;; Operators

((D cos) 3)
;Value: -.1411200080598672

(- (sin 3))
;Value: -.1411200080598672

(((expt D 2) cos) 3)
;Value: .9899924966004454

(- (cos 3))
;Value: .9899924966004454

((D (lambda (x) (* x x x))) 2)
;Value: 12

;;; Quantities with units

(pe :e)
(& 1.60217733e-19 coulomb)

(pe :k)
(& 1.380658e-23
   (/ (* kilogram (expt meter 2))
      (* kelvin (expt second 2))))


(pe (/ (* :k (& 300 kelvin)) :e))
(& .02585215707677003 volt)



(pe ((D (lambda (x) (* x x x)))
     (& 2 (/ kilogram second))))
(& 12
   (/ (expt kilogram 2)
      (expt second 2)))


(pe (definite-integral
      (lambda (r)
	(/ (* :G earth-mass (& 1 kilogram))
	   (square (+ earth-radius r))))
      (& 0 meter) (& 1 meter)))
(& 9.824031599863007 joule)

;;; Abstract quantities

(pe (+ (literal-number 'a) 1))
(+ 1 a)

(pe (* (- 'x 'y) (+ 'x 'y)))
(+ (expt x 2) (* -1 (expt y 2)))

(pe ((D (lambda (x) (* x x x))) 'a))
(* 3 (expt a 2))


(pe ((D (lambda (x) (* x x x)))
     (& 'a (/ kilogram second))))
(& (* 3 (expt a 2))
   (/ (expt kilogram 2)
      (expt second 2)))
