Jsoftware
High-Performance Development Platform

easy one.. dropping the leading axis

View: New views
14 Messages — Rating Filter:   Alert me  

easy one.. dropping the leading axis

by Matthew Brand-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

   $ }. i. 2 3 4
1 3 4

How do I drop the leading axis to make the shape 3 4 ?

Thanks,
Matthew.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Matthew Brand-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

By the way, the problem for me arises because of the way I am appending data
in a loop. Is there a better way to do this rather than , ,:  followed by }.
at the end?

   $ example ''
1 2 3
   example =: 3 : 0
output =. ''
for_i. i. y do.
output =. output , ,: i. 2 3
end.
}. output
)

   $ example 2 NB. what I want...
2 2 3
   $ example 1 NB. Not what I want, I want 2 3
1 2 3

2009/11/9 Matthew Brand <mtthwbrnd@...>

>    $ }. i. 2 3 4
> 1 3 4
>
> How do I drop the leading axis to make the shape 3 4 ?
>
> Thanks,
> Matthew.
>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> From: Matthew Brand
>
>    $ }. i. 2 3 4
> 1 3 4
>
> How do I drop the leading axis to make the shape 3 4 ?
>
Either of these will do in this case.

   $ ,/ }. i. 2 3 4
3 4
   $ {. }. i. 2 3 4
3 4

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Matthew Brand-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I should have known {. }.

Would not have thought of ,/ though.

Thanks.

2009/11/9 Sherlock, Ric <R.G.Sherlock@...>

> > From: Matthew Brand
> >
> >    $ }. i. 2 3 4
> > 1 3 4
> >
> > How do I drop the leading axis to make the shape 3 4 ?
> >
> Either of these will do in this case.
>
>   $ ,/ }. i. 2 3 4
> 3 4
>   $ {. }. i. 2 3 4
> 3 4
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> From: Matthew Brand
>
> By the way, the problem for me arises because of the way I am appending
> data in a loop. Is there a better way to do this rather than , ,:  
> followed by }. at the end?
>
>    $ example ''
> 1 2 3
>    example =: 3 : 0
> output =. ''
> for_i. i. y do.
> output =. output , ,: i. 2 3
> end.
> }. output
> )
>
>    $ example 2 NB. what I want...
> 2 2 3
>    $ example 1 NB. Not what I want, I want 2 3
> 1 2 3

This is perhaps a bit nicer. Saves you dropping the first item each time, but still have to handle the special case where there is only one item.

   example =: 3 : 0
output =. 0 2 3$''
for_i. i. y do.
output =. output , i. 2 3
end.
{.^:(1 = #) output
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Istvan Kadar :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

   ${. i. 2 3 4
3 4

regards,
Istvan

2009/11/9 Matthew Brand <mtthwbrnd@...>

>   $ }. i. 2 3 4
> 1 3 4
>
> How do I drop the leading axis to make the shape 3 4 ?
>
> Thanks,
> Matthew.
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by neitzel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Firstly, as Istvan wrote already, simply extracting the first element
with {. is a straight-forward way to "de-itemize" your data.
Taking that last description literally, you can (once again) let J figure
out how to de-itemize, given that itemize (monadic ,:) and inverse
are easily at hand:

        Welcome to J! (ctrl+d to quit) 4.05+GDS changes/Feb 16 2008/19:19:49

           inv =. ^: _1
           ,: inv i. 2 3 4
        0 1  2  3
        4 5  6  7
        8 9 10 11
           $ ,: inv i. 2 3 4
        3 4

It is not really possible for me to derive what your are actually
trying to achieve, based on your code and your two single examples.

Your code looks a bit suspicious to me, though: beheading "output"
after taking an extra turn through the loop just looks irksome to
me.  Also, I'd rather expect the initialization and loop to be more
along the typical pattern of

        output =. i. 0 2 3
        for. i. y do.
                output =. output , ,: i. 2 3
        end.

which caters for a rank-3 result in all cases.  Usually such invariants
make it easier to build upon.  Don't introduce special cases too early;
they will start pestering you on their own soon enough.  Often enough,
you will have to "undo" the special case.

You may have noticed that I dropped the "_i" from the "for."  Since
you are not referring to the loop index anyway, why mention it?

In the end, I would dispense with the entire loop and if you really
insist on the special casing, I'd use a simple if.:

example =. monad define
        if. y=1 do.
                i. 2 3
        else.
                y # ,: i. 2 3
        end.
)

Or "(<:y)" -- again: it is not clear to me what you are trying to do.

                                                        Martin Neitzel
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> From: neitzel@...
>
> Taking that last description literally, you can (once again) let J
> figure out how to de-itemize, given that itemize (monadic ,:)
> and inverse are easily at hand:
>
>   inv =. ^: _1
>   ,: inv i. 2 3 4
> 0 1  2  3
> 4 5  6  7
> 8 9 10 11
>   $ ,: inv i. 2 3 4
> 3 4

Additionally you can ask J what the inverse for ,: is:

   ,: b. _1
{.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by neitzel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>Additionally you can ask J what the inverse for ,: is:
>
>   ,: b. _1
>{.

Thanks for the reminder, Ric!  We could warn people that this result
is not the ob/inverse function itself but just its linear representation.
Otherwise, a naive

           (,: b. _1)  i. 2 3 4
        2 2 2

might disappoint quite a few people.  The full story goes like this:

   ] hd =. ,: b. _1
{.
   datatype hd
+-------+
|literal|
+-------+
   $hd
2
   hd i. 2 3 4 NB. You should be able to explain this by now:
2 2 2

   (<hd) `: 0   i. 2 3 4
0 1  2  3
4 5  6  7
8 9 10 11

                                                                Martin
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Devon McCormick :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I don't know how simplified your example is compared to your real problem,
but any loop in J makes me suspicious.

Your example, as stated, could be done without looping like this:

   example=: 3 : '>y$<i.2 3'
   $example 3
3 2 3

But perhaps your real problem can't be simplified this far.

On Mon, Nov 9, 2009 at 8:17 AM, Matthew Brand <mtthwbrnd@...>wrote:

> By the way, the problem for me arises because of the way I am appending
> data
> in a loop. Is there a better way to do this rather than , ,:  followed by
> }.
> at the end?
>
>   $ example ''
> 1 2 3
>   example =: 3 : 0
> output =. ''
> for_i. i. y do.
> output =. output , ,: i. 2 3
> end.
> }. output
> )
> ...
>

--
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> From: Devon McCormick
>
> I don't know how simplified your example is compared to your real
> problem,
> but any loop in J makes me suspicious.
>
> Your example, as stated, could be done without looping like this:
>
>    example=: 3 : '>y$<i.2 3'
>    $example 3
> 3 2 3
>

I've tried to unlearn a tendency I had earlier to box things when it is not necessary.
I would now prefer:
   example=: 3 : 'y # ,: i.2 3'

or tacitly:
   example=: (# ,:)&(i.2 3)

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Matthew Brand-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for all of the responses and pointers.

My example is purposefully "bad" in a loop wise sense because the problem I
am solving does not know the shape of the items before the loop starts,
although  all of the items do share a common (unknown before the loop)
shape. That is why I initialise using output =. ''  and end up having to
drop the first item.

thanks,
Matthew.


2009/11/10 Sherlock, Ric <R.G.Sherlock@...>

> > From: Devon McCormick
> >
> > I don't know how simplified your example is compared to your real
> > problem,
> > but any loop in J makes me suspicious.
> >
> > Your example, as stated, could be done without looping like this:
> >
> >    example=: 3 : '>y$<i.2 3'
> >    $example 3
> > 3 2 3
> >
>
> I've tried to unlearn a tendency I had earlier to box things when it is not
> necessary.
> I would now prefer:
>    example=: 3 : 'y # ,: i.2 3'
>
> or tacitly:
>   example=: (# ,:)&(i.2 3)
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by neitzel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

RS> or tacitly:
RS>    example=: (# ,:)&(i.2 3)

Why that hook? You can simply write

        example =: #&(,: i. 2 3)

In case you wonder:  in both lines, the parenthesized part on the
right will be computed into a simple noun at definition time.
Don't let that "i." confuse you when you ask J about "example":

           example
        #&(i.1 2 3)

This is just J's way of displaying a 2-row constant with a substitute
expression keeping things on a single line.  There's no i.ing going on
when you invoke "example".

                                                        Martin
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: easy one.. dropping the leading axis

by Sherlock, Ric :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> From: Martin Neitzel
>
> RS> or tacitly:
> RS>    example=: (# ,:)&(i.2 3)
>
> Why that hook? You can simply write
>
> example =: #&(,: i. 2 3)
>
> In case you wonder:  in both lines, the parenthesized part on the
> right will be computed into a simple noun at definition time.
> Don't let that "i." confuse you when you ask J about "example":
>
>   example
> #&(i.1 2 3)
>
> This is just J's way of displaying a 2-row constant with a substitute
> expression keeping things on a single line.  There's no i.ing going on
> when you invoke "example".
>

Thanks Martin,
I wasn't confused about that point. I use both linear and boxed notations which help on that point.
   example
#&(i.1 2 3)
+-+-+-----+
|#|&|0 1 2|
| | |3 4 5|
+-+-+-----+

I just took the idiom (# ,:) (give me x copies of y) and gave it a right argument.
You're right that it can be simplified further.

Ric

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm