Power Operator-- A Simple Question about a Simple Scalar

General APL language issues

Power Operator-- A Simple Question about a Simple Scalar

Postby petermsiegel on Fri Sep 28, 2018 11:14 pm

I've always wondered why the power operator ⍣ couldn't be lax about its scalar right operand, i.e. why can't it tolerate (overlook just this once?) a single-element vector as well? I find myself doing f ⍣ (⍬⍴...)⊣⍵ (or creating a cover function pow) for use with common functions that alway return vectors, even for simple args.
Code: Select all
  2+⍣(0=⎕NC'fred')⊣3      ⍝ Bad, yet easy on the eyes.    ⎕NC returns 1-element vector
RANK ERROR
  2+⍣(⍬⍴0=⎕NC'fred')⊣3    ⍝ Good syntax, but n⍬isy!
5                                           

No doubt the interpreter is correct and certainly consistent, but those ⍬⍴ can obscure errors, e.g. when a multi-element vector is afoot, ⍬⍴ will hide the error.
petermsiegel
 
Posts: 55
Joined: Thu Nov 11, 2010 11:04 pm

Re: Power Operator-- A Simple Question about a Simple Scalar

Postby Roger|Dyalog on Sat Sep 29, 2018 6:12 am

I don't know the exact reasoning, but my guess is that it goes something like this: f⍣m⊢⍵ may possibly be extended in the future to allow an array right operand, and in such extension the outer shape of the result would be the shape of that right operand. The current ⍣ should not behave in a way that forecloses future correct behavior.

The extended power operator can be modelled as follows:

Code: Select all
      pow←{⍵⍵ ⍺⍺{⍺⍺⍣⍺⊢⍵}⍤0⊢⍵}

      2+pow(0=⎕nc 'fred')⊢3
3
      ⍴2+pow(0=⎕nc 'fred')⊢3
1
      2+pow(⍬⍴0=⎕nc 'fred')⊢3
3
      ⍴2+pow(⍬⍴0=⎕nc 'fred')⊢3


An example where an array right operand is useful:

Code: Select all
      {0.5×⍵+2÷⍵}pow(⍳8)⊢1
1.5 1.41667 1.41422 1.41421 1.41421 1.41421 1.41421 1.41421

      2 - ×⍨{0.5×⍵+2÷⍵}pow(⍳8)⊢1
¯0.25 ¯0.00694444 ¯0.0000060073 ¯4.51061E¯12 4.44089E¯16 4.44089E¯16
          4.44089E¯16 4.44089E¯16

      ⍝ currently you can't do that with ⍣
      {0.5×⍵+2÷⍵}⍣(⍳8)⊢1
RANK ERROR
      {0.5×⍵+2÷⍵}⍣(⍳8)⊢1
                ∧
Roger|Dyalog
 
Posts: 133
Joined: Thu Jul 28, 2011 10:53 am


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest