I've got this currently:

## See http://rosettacode.org/wiki/Matrix_transposition#PicoLisp
(de trM (M)
   (apply mapcar M list) )

## According to https://en.wikipedia.org/wiki/Matrix_multiplication
## Handles all scenarios just like Python's nympy's dot().
(de mM @
   (let (Am (next)
           Bm (next)
           Mrows '((Ar) (make
                           (for Br (trM Bm)
                              (link (sum */ Ar Br (1.0 .))) ) ) ) )
      (let Rm
         (if2 (exlst~flat? Am) (exlst~flat? Bm)
            ## Both are flat so we just multiply them and sum,
            ## the return will be a number, see
https://en.wikipedia.org/wiki/Dot_product
            (sum * Am Bm)
            ## The A matrix is flat, the B matrix is not so we loop the
subs of the B and multiply with A.
            (Mrows Am)
            ## B is flat, A is not, doesn't work.
            (prog
               (println "Shape mismatch")
               (bye) )
            ## They are both multidimensional so we loop both.
            (make
               (for Ar Am
                  (link
                     (Mrows Ar) ) ) ) )
         (ifn (rest)
            Rm
            (nM Rm (next)) ) ) ) )

I didn't see much benefit in using the Rosetta version for matrix
multiplication but nice touch there with the sum */ and (1.0 .), that was
definitely needed.

Will try and translate this one from python to PL now:
https://medium.com/technology-invention-and-more/how-to-build-a-multi-layered-neural-network-in-python-53ec3d1d326a




On Wed, Feb 21, 2018 at 7:55 PM, Henrik Sarvell <hsarv...@gmail.com> wrote:

> Thanks Alex,
>
> I'll try them out, and modify multiply to handle an arbitrary amount of
> matrices.
>
> On Wed, Feb 21, 2018 at 11:36 AM, Alexander Burger <a...@software-lab.de>
> wrote:
>
>> Hi Henrik,
>>
>> > For reference, the article / tutorial:
>> > https://medium.com/technology-invention-and-more/how-to-buil
>> d-a-simple-neural-network-in-9-lines-of-python-code-cc8f23647ca1
>>
>> Nice!
>>
>>
>> > (de colM (M Cn)
>> >    (make
>> >       (for Col M
>> >          (link (car (nth Col Cn)))) ) )
>> >
>> > ## Transpose matrix: https://en.wikipedia.org/wiki/Transpose
>> > (de trM (M)
>> >    (make
>> >       (for N (length (car M))    # Number of columns.
>> >          (link (colM M N)) ) ) )
>>
>> Note that there are some matrix manipulation tasks in RosettaCode.
>>
>> For example, the matrix transposition is simply:
>>
>>    (de matTrans (Mat)
>>       (apply mapcar Mat list) )
>>
>>
>> Matrix multiplication (for 2 matrices) in RosettaCode is
>>
>>    (de matMul (Mat1 Mat2)
>>       (mapcar
>>          '((Row)
>>             (apply mapcar Mat2
>>                '(@ (sum */ Row (rest) (1.0 .))) ) )
>>          Mat1 ) )
>>
>> Not sure if this fits your needs ...
>>
>> ♪♫ Alex
>>
>> --
>> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>>
>
>

Reply via email to