lips icon indicating copy to clipboard operation
lips copied to clipboard

Numerical tower

Open jcubic opened this issue 4 years ago • 8 comments

TODO:

  • [x] Complex Type
    • [x] Complex-Float
    • [x] Complex Integer
    • [x] Complex Rational
      • [x] (make-rectangular 1/2 2/4)
      • [x] (/ 10+10i 10+2i) works in Kawa ~~(Lips convert to Float)~~
      • [x] Complex NaN e.g. +nan.0+5.0i
      • [x] Complex Infinite e.g. 3.0+inf.0i
    • [x] Mixed complex 1/2+0.1i (all combinations)
    • [x] Rational Complex literal
      • [x] inexact #i1/2+2/4i -> (exact->inexact 1/2+2/4i).
    • [x] big num complex 10e+10i.
  • [x] Float Type
  • [x] Rational
  • [x] BigInteger
  • [x] Number literals
  • [x] hex octal and binary and decimal literals (#b #o #x #d)
    • [x] binary/hex/octal with complex and rational #b1/100 #b100+100i
  • [x] big literal numbers in scientific notation should parse as big int
  • [x] #e #i exact inexact literals that do conversion
    • [x] #b#x #x#b ~~replace in tokenizer~~ (1/4 == 0.25)
    • [x] #i#b1/100 inexact rational binary
    • [x] big exact fractions #e1e-1000 or #e1.2e-1000
  • [x] +nan.0 and -nan.0 return by parser
  • [x] proper negative 0
  • [x] Case insensitive mnemonics
  • [x] string->number should parse all tokens #[ieoxb]
  • [x] proper casting and all combination of operations
  • [x] properly working string->number
  • [ ] Unit tests for operations (all types)
    • [x] sqrt
    • [ ] abs
    • [x] /
      • [x] nan
      • [ ] inf
    • [x] number->string (check if they are ok)
    • [x] string->number
    • [x] +
      • [x] nan
      • [x] inf
    • [x] -
      • [ ] nan
      • [ ] inf
    • [x] *
      • [ ] nan
      • [ ] inf
    • [ ] modulo integer integer (test types)
    • [ ] quotient integer integer (test types)
    • [ ] reminder int int (test types)
    • [ ] max
    • [ ] min
    • [ ] gcd integer ...
    • [ ] lcm integer ...
    • [x] exp
    • [x] expt
    • [ ] log
    • [ ] trigonometry
      • [x] sin
      • [x] cos
      • [x] tan
      • [ ] asin
      • [ ] acos
      • [x] atan
    • [ ] round, truncate, floor, ceiling
    • [ ] positive? / negative?
    • [x] exact->inexact
    • [x] inexact->exact

Bugs

  • [x] https://github.com/jcubic/lips/issues/299
  • [x] https://github.com/jcubic/lips/issues/300
  • [x] https://github.com/jcubic/lips/issues/301
  • [x] https://github.com/jcubic/lips/issues/340

jcubic avatar Mar 24 '20 16:03 jcubic

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 avatar Mar 01 '21 13:03 jcubic

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.

jcubic avatar Mar 04 '21 11:03 jcubic

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...

jpellegrini avatar Apr 13 '23 12:04 jpellegrini

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

jcubic avatar Apr 13 '23 15:04 jcubic

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.

jcubic avatar Apr 13 '23 17:04 jcubic

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 avatar Apr 13 '23 17:04 jpellegrini

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...

jpellegrini avatar Apr 13 '23 19:04 jpellegrini

@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.

jcubic avatar Feb 12 '24 10:02 jcubic