Domain Error

When I calculate the difference between the largest and smallest number in an empty vector (v ← ⍳0) using ⌈⌿ (⌈ / c) - ⌊⌿ (⌊ / c), it gives me a domain error. This statement works fine with normal vectors and matrices.

How to handle an exception so that it does not give me an error when the vector is empty? It should not return anything or just return zero.

+4
source share
2 answers

Defender is the best way to do this:

{0=⍴⍡:0 β‹„ (⌈/⍡)-⌊/⍡} 

Note that using two abbreviations, one with defining an axis, is not really necessary or correct. That is, if you want it to work with all the elements of a simple array of any dimension, just first state the argument:

  {0=⍴⍡:0 β‹„ (⌈/⍡)-⌊/⍡},10 10 ⍴⍳100 99 

Or for an array of any structure or depth, you can use "super ravel":

  {0=⍴⍡:0 β‹„ (⌈/⍡)-⌊/⍡}∊(1 2 3)(7 8 9 10) 9 

Note that quadML (migration level) must be set to 3 to ensure that epsilon is "super ravel".

Also note the equivalence of the following when working on a matrix:

  ⌈⌿⌈/10 10 ⍴⍳100 99 ⌈/⌈/10 10 ⍴⍳100 99 ⌈/⌈⌿10 10 ⍴⍳100 99 ⌈⌿⌈⌿10 10 ⍴⍳100 99 

The use of reduction with an axis in this case is not required and obscures the intention, and is also potentially more expensive. It’s best to parse all this.

+2
source

As I mentioned in the comments, Dyalog APL has guards that can be used for conditional execution, and so you can just check the empty vector and give a different answer.

This can be implemented in a more traditional / pure APL method.

This version only works in 1-dimensional

In APL font:

 Z←DIFFERENCE V ⍝ Calculate difference between vectors, with empty set protection ⍝ Difference is calculated by a reduced ceiling subtracted from the reduced floor ⍝ eg. (⌈⌿(⌈V)) - (⌊⌿(⌊V)) ⍝ Protection is implemented by comparison against the empty set ⍬≑V ⍝ Which yields 0 or 1, and using that result to select an answer from a tuple ⍝ If empty, then it drops the first element, yielding just a zero, otherwise both are retained ⍝ eg. <condition>↓(ab) => 0 = (ab), 1 = (b) ⍝ The final operation is first ↑, to remove the first element from the tuple. Z←↑(⍬≑V)↓(((⌈⌿(⌈V)) - (⌊⌿(⌊V))) 0) 

Or in a brace for people without a font.

 Z{leftarrow}DIFFERENCE V {lamp} Calculate difference between vectors, with empty set protection {lamp} Difference is calculated by a reduced ceiling subtracted from the reduced floor {lamp} eg. ({upstile}{slashbar}({upstile}V)) - ({downstile}{slashbar}({downstile}V)) {lamp} Protection is implemented by comparison against the empty set {zilde}{equalunderbar}V {lamp} Which yields 0 or 1, and using that result to select an answer from a tuple {lamp} If empty, then it drops the first element, yielding just a zero, otherwise both are retained {lamp} eg. <condition>{downarrow}(ab) => 0 = (ab), 1 = (b) {lamp} The final operation is first {uparrow}, to remove the first element from the tuple. Z{leftarrow}{uparrow}({zilde}{equalunderbar}V){downarrow}((({upstile}{slashbar}({upstile}V)) - ({downstile}{slashbar}({downstile}V))) 0) 

and image to save ...

APL Difference Function

Update. multidimensional

 Z←DIFFERENCE V ⍝ Calculate difference between vectors, with empty set protection ⍝ Initially enlist the vector to get reduce to single dimension ⍝ eg. ∊V ⍝ Difference is calculated by a reduced ceiling subtracted from the reduced floor ⍝ eg. (⌈/V) - (⌊/V) ⍝ Protection is implemented by comparison against the empty set ⍬≑V ⍝ Which yields 0 or 1, and using that result to select an answer from a tuple ⍝ If empty, then it drops the first element, yielding just a zero, otherwise both are retained ⍝ eg. <condition>↓(ab) => 0 = (ab), 1 = (b) ⍝ The final operation is first ↑, to remove the first element from the tuple. Vβ†βˆŠV Z←↑(⍬≑V)↓(((⌈/V) - (⌊/V)) 0) 

APL Difference multi-dimensional

+1
source

Source: https://habr.com/ru/post/1483398/


All Articles