Karim Belabas on Sun, 06 Nov 2022 02:56:32 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Error message with pareval


* Jean-Luc ARNAUD [2022-11-06 02:09]:
> Hi all,
> 
> I don't understand why this result in an error "incorrect type (t_REAL).
> 
> Same behavior with versions 2.13.3, 2.15.0 and 2.15.1, all multithreading.
> 
> Thanks for any explanation.
> 
> 
> ? pareval([16*atan(1/5), 4*atan(1/239)])
>   ***   at top-level: pareval([16*atan(1/5),4*atan(1/239)])
>   ***                 ^-------------------------------------
>   *** pareval: incorrect type in pareval (t_REAL).
>   ***   Break loop: type 'break' to go back to GP prompt

pareval evaluates a vector of functions (closures). Since you give it a
vector of real numbers, you get an error.

Here's the correct syntax, using inline closures

 ? pareval([()->16*atan(1/5), ()->4*atan(1/239)])
 %1 = [3.1583289575980921339207962431166446952, 0.016736304008298895458152859837141810964]

Another version using named functions:
 ? f() = 16*atan(1/5);
 ? g() =  4*atan(1/239);
 ? pareval([f, g])
 %4 = [3.1583289575980921339207962431166446952, 0.016736304008298895458152859837141810964]

Notes :
- there is no automatic conversion of 'a constant C' into 'a function
  whose constant value is C'; the construction ()->C explicitly builds
  the latter. 

- in ()->C, the right hand side C is left unevaluated when the function
  is defined: ()->1+1 is a function that only computes 1+1 = 2 when (and
  each time) it is evaluated.

- the last remark is very useful when you want to prevent evaluation
  until some point in the future. Cleaner (and faster) than using
  character strings and eval(), as in s = "1 + 1"; and later eval(s).

Cheers,

    K.B.
--
Karim Belabas  /  U. Bordeaux,  vice-président en charge du Numérique
Institut de Mathématiques de Bordeaux UMR 5251 - (+33) 05 40 00 29 77
http://www.math.u-bordeaux.fr/~kbelabas/
`