that was fun.

on your 3rd link, isn't sneaky equivalent to just `:0 ?


----- Original Message -----
From: Dan Bron <[email protected]>
To: [email protected]
Cc: 
Sent: Friday, October 4, 2013 5:40:42 PM
Subject: Re: [Jprogramming] Matrix power puzzle

Kip asked:
>  Is there a tacit way?
>    mp =: +/ . *  NB. matrix product
>    mpwr =: 4 : 'x mp^:y =i.#x' NB. square matrix x to integer power y
>    A =: 1 1 ,: 1 0

The gerund form of ^: allows you to write such expressions tacitly.  The
full form is  x f^:(newX`thePower`newY) y which resolves to (x newX y) f^:(x
thePower y) (x newY y), or, effectively, newX f^:thePower newY . In your
case:  



       mpwrT=:mp^:([`]`(=@i.@#@[)) 

       A mpwrT 3
    3 2
    2 1

       A mpwrT _1
    0  1
    1 _1

Here, newX=:[ , i.e. the x argument to the derived function is just a
pass-through of the original x, which is common enough that ^: provides a
shortcut:
x f^:(thePower`newY) y is equivalent to x f^:([`thePower`newY) y . So you
could simplify this to:

       mpwrT =: mp^:(]`(=@i.@#@[))

-Dan

PS: A really subtle implication of the gerund form of ^: is that it lets you
do sneaky things write code that uses simple-looking powers (e.g. f^:]) but
then _pass in a pre-calculated gerund as an argument_.  

For example, here's a series of equivalent, but increasingly indirect, ways
to express the largest 32-bit signed integer:

     (2^31)-1 
     (2^(32-1))-1
     (2^((2^5)-1))-1
    _1+2^ _1+2^ 5
     (_1+2&^)(_1+2&^) 5
     (_1+2&^)^:2: 5
     2&(_1+^)^:2: 5
     2 (_1+^)^:2: 5
     2 <:@^^:2: 5
     2 <:@^^:(2:`5:) ''
     <:@^^:(2:`2:`5:)~ ''
     <:@^^:]~ 2:`2:`5:

This last separates the core function, exponentiate-and-decrement, and the
underlying data, 2 2 5.  It's the moral equivalent of <:@^/ 2 2 5 , but at
the meta-function level. For more fun with this, see

[1]  Calculating 2147483647:
    http://www.jsoftware.com/jwiki/Puzzles/2147483647 

[2]  Using our ^: for good 

http://www.jsoftware.com/pipermail/programming/2012-February/027116.html

[3]  But ^: corrupts
    http://www.jsoftware.com/pipermail/programming/2013-January/031234.html

[3]  ... absolutely:
    http://www.jsoftware.com/pipermail/programming/2013-January/031236.html
     Mwahahahahaa.


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

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

Reply via email to