Question

Unboxing or opening boxes with different sizes causes padding with 0 for numerals and a space with literals:

v=.1 4 8 ; 2 6 4 ; 6 8 4 5; 7 8 9; 6 3 7 4 9

>v
1 4 8 0 0
2 6 4 0 0
6 8 4 5 0
7 8 9 0 0
6 3 7 4 9

The fit (!.) conjunction is usually the thing to use for these things, but

>!. _1 v

Is not supported and throws a domain error.

I've got this, but with very large arrays it's not very fast:

(>./ # every y) {.!. _1 every y

Is there an efficient way to define the padding value for opening boxes?

Was it helpful?

Solution

Setting

f =: 3 :'(>./ # every y) {.!. _1 every y'
g =: _1&paddedOpen

and (in the same spirit as your f):

h =: 3 : '((>./# &> y)&($!._1))@> y'

I get the following performances for time and space:

(100&(6!:2) ,: 7!:2) &.> 'f L';'g L';'h L'
┌─────────┬─────────┬─────────┐
│ 0.045602│0.0832403│0.0388146│
│4.72538e6│1.76356e7│4.72538e6│
└─────────┴─────────┴─────────┘

where L is a large array:

L =. (<@(+i.)/)"1 ? 50000 2 $ 10

You can slightly improve f by making it terse; for example:

f =: ] {.!._1&>~ >./@:(#&>)

I don't think that there is much room for more improvements.

OTHER TIPS

My guess is that doing the padding directly will be the path to efficiency, especially if the need is restricted to a specific structure of data (as perhaps suggested by your example.) This solution has not been subjected to performance analysis, but it shows one way to do the padding yourself.

Here I'm making the assumption that the task involves always going from boxed lists to a table, and that the data is always numeric. Additional assert. statements may be worth adding to qualify that the right argument is as expected.

   v=.1 4 8 ; 2 6 4 ; 6 8 4 5; 7 8 9; 6 3 7 4 9   NB. example data

paddedOpen=: dyad define
 assert. 0 = # $ x
 Lengths=. #&> y
 PadTo=. >./ Lengths
 Padding=. x #~&.> PadTo - Lengths
 y ,&> Padding
)

   _1 paddedOpen v
1 4 8 _1 _1
2 6 4 _1 _1
6 8 4  5 _1
7 8 9 _1 _1
6 3 7  4  9

It is only important to first pad with a customized value when the default value cannot be used as an intermediary. If the default value can be used in passing, it will be faster to let the default padding occur then replace all default values with the preferred value. From the nature of your question I assume the default value has meaning in the main domain, so simple replacement won't serve.

Please leave comments informing us of the relative performance of different techniques, or at least whether one does or does not prove fast enough for your purposes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top