Thursday, May 31, 2012

Mathematics with Fuzuli

Our new general purpose interpreter and language Fuzuli was first introduced in Practical Code Solutions and has the official web page in FuzuliProject.org.

Fuzuli has several packages which are mostly wrappers of C++ libraries. One of them is the math package. It is math.nfl and stated in /usr/lib/fuzuli/nfl in Linux installations by default.

The math.nfl package simple wraps some basic mathematical functions such as square root, exponential, logarithms, floor, ceil and round as well as trigonometric functions such as sin, cos, tan, atan, acos, etc. These functions are directly inherited from the standard C++ library.  A list of functions in the math.nfl package is shown below:


FunctionDescriptionExampleResult
PIReturns a short presentation of PI
(let a (sin (/ PI 2)))
1.0
sinReturns sin of a given reference
(let a (sin 0))
0.0
cosReturns cos of a given reference
(let a (cos 0))
1.0
tanReturns tan of a given reference
(print (tan 1))
1.55741
absReturns absolute value
(print (abs -5))
5
sqrtReturns square root
(print (sqrt 25))
5
expReturns E^x
(print (exp 1))
2.71828
logReturns natural logarithm
(print (log (exp 1)))
1.0
log10Returns logarithm in base 10
(print (log10 10))
1.0
log2Returns logarithm in base 2
(print (log2 2))
1.0
coshReturns hyperbolic cos
(cosh x)
sinhReturns hyperbolic sin
(sinh x)
tanhReturns hyperbolic tan
(tanh x)
asinReturns inverse sine
(asin 1)
acosReturns inverse cosine
(acos 0)
atanReturns inverse tangent
(atan 1.55741)
1
atan2Two arguments arctan function
(atan2 1 1)
0.7853981634
powReturns a^x
(print (pow 5 2))
25
ceilReturns ceil of a given reference
(print (ceil 2.3))
3.0
floorReturns floor of a given reference
(print (floor 2.3))
2.0
roundReturns round of a given reference. (nearest integer)
(print (round 2.3))
2.0
isinfReturns 1 if the given reference is either negative or positive infinitive else returns 0
(print (isinf (/ 19 0)))
1


The table shown above is hopefully enough to introduce our functions in the math package. Any Fuzuli program should include the math package using the (require) expression if any math function shown above is planned to use. Look at the code below:


(require "/usr/lib/fuzuli/nfl/math.nfl")
(print (isinf (/ 19 0)))


Although math functions are collected in an external package, Fuzuli supports built-in arithmetic functions. The code shown below is calculating ordinary least squares regression coefficients. As you see, no math functions required and only the arithmetic functions are used.

(let x (list 1 2 3 4 5 6 7 8 9 10))
(let y (list 10 20 30 40 50 60 70 80 90 100))

(function sum (params a)
 (block
  (def s FLOAT)
  (def i INTEGER)
  (let s 0.0)
  (for (let i 0) (< i (length a)) (inc i)
   (let s (+ s (nth a i)))
  )
  (return s)
 )
)

(function mean (params a)
 (block 
  (def total FLOAT)
  (def result FLOAT)

  (let total (sum a))
  (let result (/ total (length a)))
   
  (return result)
 )
)


(function covariance (params c d)
 (block
  (def meana FLOAT)
  (def meanb FLOAT)
  (def xy FLOAT)
  (def i INTEGER)
  (let meana (mean c)) 
  (let meanb (mean d))
  (let xy 0.0)
  (for (let i 0) (< i (length a)) (inc i)
   (block
    (let xy (+ xy (* (- (nth c i) meana) (- (nth d i) meanb ))))
   )
  ) 
  (return xy)
 )

)


(let beta1 (/ (covariance x y) (covariance x x)))
(let beta0 (- (mean y) (* beta1 (mean x))))
(print "beta0 " beta0  "\n") 
(print "beta1 " beta1  "\n")


The result is 0.0 for beta0 and 10.0 for beta1.

See you in next entry. Have fun!

1 comment:

  1. Thanks for such a great information.The symbols which you have described here are used in C++ for coding.

    ReplyDelete

Thanks