LIPS-scheme / lips

Scheme based powerful lisp interpreter in JavaScript
https://lips.js.org
Other
422 stars 35 forks source link

Numerical tower #34

Open jcubic opened 4 years ago

jcubic commented 4 years ago

TODO:

Bugs

jcubic commented 3 years ago

Arithmetic error:

;; Returns the arithmetic, geometric, and
;; harmonic means of a nested list of numbers
(define (means ton)
  (letrec*
     ((mean
        (lambda (f g)
          (f (/ (sum g ton) n))))
      (sum
        (lambda (g ton)
          (if (null? ton)
            (+)
            (if (number? ton)
                (g ton)
                (+ (sum g (car ton))
                   (sum g (cdr ton)))))))
      (n (sum (lambda (x) 1) ton)))
    (values (mean values values)
            (mean exp log)
            (mean / /))))

Note that evaluating (means '(3 (1 4))) returns three values: 8/3, 2.28942848510666 (approximately), and 36/19.

LIPS devel returns:

8/3
2.2894284851066633
8/3

Ref: https://small.r7rs.org/wiki/R7RSSmallErrata/

jcubic commented 3 years ago

Function (means '(3 (1 4))) is fixed the problem was that (/ 2) was returning 2 and it was even in doc string. According to spec it should return 1/2.

jpellegrini commented 1 year ago

Hi, Not sure if this is supposed to be working already (I see expt is not marked in your checklist), but anyway - (expt 1.5 0) and (expt 2 3.0) return an error saying that LIPS can't convert BigInt to number...

jcubic commented 1 year ago

@jpellegrini thanks I will make sure to add those to unit tests when will work on that function.

jcubic commented 1 year ago

fixed the simple case and Started adding tests for bigint + float combination, but still rational need some work:

lips> (expt 1/2 2)
1/4
lips> (expt 2 1/2)
2/1

there is a need to add different root calculations (I'm not sure if Scheme has something like this out of the box, it may be useful to expose).

And also added an exception when invoking expt on complex numbers since this will require more work, need to think how to handle trigonometry functions, that are required to calculate power operation on complex numbers.

jpellegrini commented 1 year ago

there is a need to add different root calculations (I'm not sure if Scheme has something like this out of the box, it may be useful to expose).

R7RS has only square roots... I usually do (expt x 1/n) for the n-th root.

And also added an exception when invoking expt on complex numbers since this will require more work, need to think how to handle trigonometry functions, that are required to calculate power operation on complex numbers.

Maybe (expt x y) could be implemented as (exp (* (log x) y)), if you already have log for complexes?

And (log z), with z complex, would be

REAL: (log (magnitude z)) IMAG: (angle z)

jpellegrini commented 1 year ago

Maybe (expt x y) could be implemented as (exp (* (log x) y)), if you already have log for complexes?

See PR #247

And I see you already have implemented complex log...

jcubic commented 9 months ago

@jpellegrini expt was fully implemented and fully tested. It will be released in the next beta version.

It matches the implementation of Kawa and doesn't have rounding errors when the power is an integer.