Power Operator-- A Simple Question about a Simple Scalar
2 posts
• Page 1 of 1
Power Operator-- A Simple Question about a Simple Scalar
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.
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.
- 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: 143
- Joined: Thu Nov 11, 2010 11:04 pm
Re: Power Operator-- A Simple Question about a Simple Scalar
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:
An example where an array right operand is useful:
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: 238
- Joined: Thu Jul 28, 2011 10:53 am
2 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group