Ruud H.G. van Tol on Sun, 18 Aug 2024 14:05:50 +0200


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

Re: Applying a function to all elements of a vector



On 2024-08-18 04:20, Joe Slater wrote:
This is long, but I thought I should attempt to address this with respect to all operators, not just the first one to give unexpected results. The user manual (p. 10) says

    PARI operations are in general quite permissive. For instance
    taking the exponential of a vector should not make sense. However,
    it frequently happens that one wants to apply a given function to
    all elements in a vector. This is easily done using a loop, or
    using the apply built-in function, but in fact PARI assumes that
    this is exactly what you want to do when you apply a scalar
    function to a vector.

[...]

An operator is not really a function, but since one can do:

  alias("pow", "_^_")

I'd expect similar.


    > 1^vector(3) \\ /All the values of the vector get converted to
    t_REAL for some reason. /
    %3 = [1.000, 1.000, 1.000]
    > 0^vector(3)
      ***   at top-level: 0^vector(3)
      ***                  ^----------
      *** _^_: domain error in gpow(0,n): n <= 0
  ***   Break loop: type 'break' to go back to GP prompt

Surprises me as well, but appears to be as documented at the end of "??^".


Maybe hyper-operators should be made to look differently,
to prevent different behavior if dedicated support for some non-scalar parameter was added in the future.
(obviously implying that implicit-apply will be moved out)


For example like:

? 2 [^] [1, 2, 3]
% [2, 4, 8]   \\ exact

or like:

? 2 ^* [1, 2, 3]
% [2, 4, 8]   \\ exact

? [1, 2, 3] *^ 2
% [1, 4, 9]

or like:

? 2 ^<< [1, 2, 3]
% [2, 4, 8]   \\ exact

? [1, 2, 3] >>^ 2
% [1, 4, 9]

-- Ruud