Reanimator Ltd

High-performance coding by Eddie Edwards

Operators as Matrices

06 Mar 2012, 13:17 UTC

In The Myth of the "Unrealizable" Filter I had an expression like this:

Y = (X)A.B

Where Y, X, A, B are all vectors, so A.B scales vector X to give vector Y.

This can be expressed in matrix form

Y = G*B

Here, G is the matrix representing the operator (X)A. which operates on B on the right.

An interesting thought is that maybe it would be cool if a language allowed (X)A. to be an object which acts on the right. i.e. instead of parsing say A + B as add(A,B) the language would parse A + B as apply(adder(A),B) where adder(A) = apply(A,+) is a function taking one argument, which adds A to the argument. Thus, the whole language works (modulo operator precedence) as a series of apply operations which can take variables or operators/symbols/functions, and which performs currying. So:

apply(A,fn(a,b))      gives fn2(b) -> fn(A,b)
apply(fn(a),B)        gives fn2()  -> fn(B)
apply(fn1(a),fn2(b))  gives fn3(b) -> fn1(fn2(b))

Then perhaps - assuming * and . operators - I could just write:

G = X * (A.)

This is not simple to compile - the expression is apply(apply(X,*),apply(A,dot)) which I might rewrite apply(mulbyX,dotbyA) giving a function like fn(B) -> mulbyX(dotbyA(B)). Casting this function to a matrix is non-trivial.

Alternatively, perhaps the same compiler (which has to be able to analyze linear functions) could handle a syntax like this:

G = (X * (A.B)) %% B

Which is rather like differentiation with respect to B.

The resulting system would be powerful and conceptually simple, but maybe it has insufficient use cases. Still, it's interesting to think of new ways to write code.

What do you think? :)

Please log in if you wish to edit or delete comments you make.

Formatting help & Commenting policy

Andrei Rotenstein commented:

Eddie, I'm not sure I follow the last expression.

on 06 Mar 2012, 16:56 UTC

Eddie Edwards commented:

Andrei, the idea is that the expression on the LHS of %% is "divided by" the expression on the RHS to give a matrix (or perhaps tensor) G s.t. G*B reproduces the original expression. It's a lexical expression, so B doesn't have to be declared anywhere, while X and A are used to create G.

The output is a matrix where each element is the amount of RHS[n] found in LHS[m]. So in fact it is precisely differentiation with respect to B, but the compiler would complain if the result wasn't a constant in each place (i.e. B is used with other powers than 0 and 1).

Does that help?

on 06 Mar 2012, 19:12 UTC

Eddie Edwards commented:

Thinking about it, you could implement this in a C compiler as follows:

  1. Isolate LHS and RHS expressions
  2. Set RHS variable to (0,0,0), fold into LHS, then constant-fold LHS
  3. Do the same for (1,0,0), (0,1,0), (0,0,1)
  4. Subtract e.g. (0,0,0) from (1,0,0) to get the first column of G

This works assuming the LHS is linear; if the LHS is not linear it gives a rough approximation of the correct answer at B = (0,0,0) :)

on 06 Mar 2012, 21:26 UTC updated 06 Mar 2012, 21:28 UTC

DC commented:

Most recent topmost *really* doesn't work for this sort of blog.

on 06 Mar 2012, 21:48 UTC

Andrei Rotenstein commented:

Much.

Pretty awesome, Eddie!

on 08 Mar 2012, 02:30 UTC

thank you sirthese links were heuplfl i have all the basic knowledge of working with AVRs given in your tutorials..and had bought usb avr programmer, avr development board and atmega 16 microcontrolleri and one of my friend( pursuing mechanical engineering) are trying to develop a book scanner for which i neededthe programming code for rotating the rod (as mentioned earlier) the whole setup of the book scanner is as follows-we have 2 rods at right angle to each other(say one along the x axis and another one along the y axis) the rod along the y axis rotates 60 degree clockwise(cw) and anticlockwise(acw) direction with time delay of 10 seconds as the rod rotates cw and acw the glass panel(which is moulded in the shape of an open book) attached to the rod along the x axis moves up and down .now moving the rod in 60 degree cw for 10 seconds then moving it acw is the 1st mechanism we need to build nextly we need some automatic setup to turn the pages of the books which are to be scanned (since as the glass panel moves up there is a provision to turn the pages manually or through some automatic setup so that the further pages can be scanned)this automatic setup for turning the pages is the 2nd mechanism we need to build..third we need an speed regulator to make the rod move slow and fast as and when needed..i request you if you could help us out in this..thank you once again and sorry for replying so late.. commented:

thank you sirthese links were heuplfl i have all the basic knowledge of working with AVRs given in your tutorials..and had bought usb avr programmer, avr development board and atmega 16 microcontrolleri and one of my friend( pursuing mechanical engineering) are trying to develop a book scanner for which i neededthe programming code for rotating the rod (as mentioned earlier) the whole setup of the book scanner is as follows-we have 2 rods at right angle to each other(say one along the x axis and another one along the y axis) the rod along the y axis rotates 60 degree clockwise(cw) and anticlockwise(acw) direction with time delay of 10 seconds as the rod rotates cw and acw the glass panel(which is moulded in the shape of an open book) attached to the rod along the x axis moves up and down .now moving the rod in 60 degree cw for 10 seconds then moving it acw is the 1st mechanism we need to build nextly we need some automatic setup to turn the pages of the books which are to be scanned (since as the glass panel moves up there is a provision to turn the pages manually or through some automatic setup so that the further pages can be scanned)this automatic setup for turning the pages is the 2nd mechanism we need to build..third we need an speed regulator to make the rod move slow and fast as and when needed..i request you if you could help us out in this..thank you once again and sorry for replying so late..

on 05 Sep 2015, 21:55 UTC

anyway no matter how nicely u write your blog or whether you did or did not blog about it, u cant make everyone hap.pylife is short, just do whatever make us happy commented:

anyway no matter how nicely u write your blog or whether you did or did not blog about it, u cant make everyone hap.pylife is short, just do whatever make us happy

on 18 Oct 2016, 12:24 UTC

Slam dunkin like Shaquille O'Neal, if he wrote <a href="http://xjvufuohlr.com">inmarfotive</a> articles. commented:

Slam dunkin like Shaquille O'Neal, if he wrote <a href="http://xjvufuohlr.com">inmarfotive</a> articles.

on 19 Oct 2016, 17:41 UTC

Got it! Thanks a lot again for helpnig me out! http://trtheax.com [url=http://puefihzqhm.com]puefihzqhm[/url] [link=http://uijhpttxga.com]uijhpttxga[/link] commented:

Got it! Thanks a lot again for helpnig me out! http:trtheax.com [url=http:puefihzqhm.com]puefihzqhm[/url] [link=http://uijhpttxga.com]uijhpttxga[/link]

on 19 Oct 2016, 23:57 UTC

Gee <a href="http://dyohcrc.com">wirllkeis,</a> that's such a great post! commented:

Gee <a href="http://dyohcrc.com">wirllkeis,</a> that's such a great post!

on 22 Oct 2016, 05:45 UTC

Action requires kngeewdol, and now I can act! http://oyvrijlrqm.com [url=http://ukdzwhujnac.com]ukdzwhujnac[/url] [link=http://ecyvqcnr.com]ecyvqcnr[/link] commented:

Action requires kngeewdol, and now I can act! http:oyvrijlrqm.com [url=http:ukdzwhujnac.com]ukdzwhujnac[/url] [link=http://ecyvqcnr.com]ecyvqcnr[/link]

on 24 Oct 2016, 20:54 UTC

DR44GX http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com commented:

DR44GX http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com

on 02 Jan 2017, 08:45 UTC

pJihDf <a href="http://foenwnxvhofp.com/">foenwnxvhofp</a>, [url=http://anmpehfhlqsb.com/]anmpehfhlqsb[/url], [link=http://jponzvlryibk.com/]jponzvlryibk[/link], http://ouioqlvktqtg.com/ commented:

pJihDf <a href="http:foenwnxvhofp.com/">foenwnxvhofp</a>, [url=http:anmpehfhlqsb.com/]anmpehfhlqsb[/url], [link=http:jponzvlryibk.com/]jponzvlryibk[/link], http:ouioqlvktqtg.com/

on 10 Jan 2017, 12:19 UTC

9KlT2O <a href="http://foihevcpytit.com/">foihevcpytit</a>, [url=http://xooazeafbclr.com/]xooazeafbclr[/url], [link=http://oitmtamobqlh.com/]oitmtamobqlh[/link], http://ajrffemddxcx.com/ commented:

9KlT2O <a href="http:foihevcpytit.com/">foihevcpytit</a>, [url=http:xooazeafbclr.com/]xooazeafbclr[/url], [link=http:oitmtamobqlh.com/]oitmtamobqlh[/link], http:ajrffemddxcx.com/

on 10 Jan 2017, 14:46 UTC

dPTsBa <a href="http://bfecxeyppvzt.com/">bfecxeyppvzt</a>, [url=http://kmvmseafxygg.com/]kmvmseafxygg[/url], [link=http://vghnwzhwjwsa.com/]vghnwzhwjwsa[/link], http://liqcgxcyyajv.com/ commented:

dPTsBa <a href="http:bfecxeyppvzt.com/">bfecxeyppvzt</a>, [url=http:kmvmseafxygg.com/]kmvmseafxygg[/url], [link=http:vghnwzhwjwsa.com/]vghnwzhwjwsa[/link], http:liqcgxcyyajv.com/

on 10 Jan 2017, 17:09 UTC

vRFZh3 <a href="http://udrvmatrfuiz.com/">udrvmatrfuiz</a>, [url=http://fjwitztelnwu.com/]fjwitztelnwu[/url], [link=http://neyujqidbbkj.com/]neyujqidbbkj[/link], http://oaqwfetknucw.com/ commented:

vRFZh3 <a href="http:udrvmatrfuiz.com/">udrvmatrfuiz</a>, [url=http:fjwitztelnwu.com/]fjwitztelnwu[/url], [link=http:neyujqidbbkj.com/]neyujqidbbkj[/link], http:oaqwfetknucw.com/

on 10 Jan 2017, 20:40 UTC

iQvUHH <a href="http://oqmvceaozubw.com/">oqmvceaozubw</a>, [url=http://bpoxulgelvtb.com/]bpoxulgelvtb[/url], [link=http://ymtdknnosmsl.com/]ymtdknnosmsl[/link], http://jsalztdrbkzw.com/ commented:

iQvUHH <a href="http:oqmvceaozubw.com/">oqmvceaozubw</a>, [url=http:bpoxulgelvtb.com/]bpoxulgelvtb[/url], [link=http:ymtdknnosmsl.com/]ymtdknnosmsl[/link], http:jsalztdrbkzw.com/

on 11 Jan 2017, 05:22 UTC

VCUb7e <a href="http://grqgslfdpgho.com/">grqgslfdpgho</a>, [url=http://qvhroirjihmm.com/]qvhroirjihmm[/url], [link=http://vctaehbeynja.com/]vctaehbeynja[/link], http://oyubdmxrdsdc.com/ commented:

VCUb7e <a href="http:grqgslfdpgho.com/">grqgslfdpgho</a>, [url=http:qvhroirjihmm.com/]qvhroirjihmm[/url], [link=http:vctaehbeynja.com/]vctaehbeynja[/link], http:oyubdmxrdsdc.com/

on 11 Jan 2017, 07:42 UTC

py3DiA <a href="http://daojmlgfnwdx.com/">daojmlgfnwdx</a>, [url=http://elmeghkswesl.com/]elmeghkswesl[/url], [link=http://jwqespfptkhn.com/]jwqespfptkhn[/link], http://dzworrowbgru.com/ commented:

py3DiA <a href="http:daojmlgfnwdx.com/">daojmlgfnwdx</a>, [url=http:elmeghkswesl.com/]elmeghkswesl[/url], [link=http:jwqespfptkhn.com/]jwqespfptkhn[/link], http:dzworrowbgru.com/

on 11 Jan 2017, 10:02 UTC

9sCw84 <a href="http://bjwdpkqgqoau.com/">bjwdpkqgqoau</a>, [url=http://akxtrvltpazv.com/]akxtrvltpazv[/url], [link=http://pczikrhpmyzx.com/]pczikrhpmyzx[/link], http://ptstmbdtmgkt.com/ commented:

9sCw84 <a href="http:bjwdpkqgqoau.com/">bjwdpkqgqoau</a>, [url=http:akxtrvltpazv.com/]akxtrvltpazv[/url], [link=http:pczikrhpmyzx.com/]pczikrhpmyzx[/link], http:ptstmbdtmgkt.com/

on 11 Jan 2017, 10:33 UTC

F2LSZe <a href="http://ftpebvddiuoq.com/">ftpebvddiuoq</a>, [url=http://wkpfqlsqtylh.com/]wkpfqlsqtylh[/url], [link=http://ogbxmhfifqeg.com/]ogbxmhfifqeg[/link], http://cceiqkzybtaf.com/ commented:

F2LSZe <a href="http:ftpebvddiuoq.com/">ftpebvddiuoq</a>, [url=http:wkpfqlsqtylh.com/]wkpfqlsqtylh[/url], [link=http:ogbxmhfifqeg.com/]ogbxmhfifqeg[/link], http:cceiqkzybtaf.com/

on 19 Jan 2017, 01:54 UTC

stqtO7 <a href="http://vexzifuzdicd.com/">vexzifuzdicd</a>, [url=http://vkxejpbbldyc.com/]vkxejpbbldyc[/url], [link=http://venvnwnylond.com/]venvnwnylond[/link], http://jmleaeuvwczy.com/ commented:

stqtO7 <a href="http:vexzifuzdicd.com/">vexzifuzdicd</a>, [url=http:vkxejpbbldyc.com/]vkxejpbbldyc[/url], [link=http:venvnwnylond.com/]venvnwnylond[/link], http:jmleaeuvwczy.com/

on 19 Jan 2017, 15:37 UTC

OroRj6 <a href="http://cvlozbyeyjxo.com/">cvlozbyeyjxo</a>, [url=http://zumblcxijxug.com/]zumblcxijxug[/url], [link=http://sehkocziiecx.com/]sehkocziiecx[/link], http://bxlyflqdxgxj.com/ commented:

OroRj6 <a href="http:cvlozbyeyjxo.com/">cvlozbyeyjxo</a>, [url=http:zumblcxijxug.com/]zumblcxijxug[/url], [link=http:sehkocziiecx.com/]sehkocziiecx[/link], http:bxlyflqdxgxj.com/

on 20 Jan 2017, 20:10 UTC

ikkCGs <a href="http://hjnwqbvtribo.com/">hjnwqbvtribo</a>, [url=http://nqfajpikgstw.com/]nqfajpikgstw[/url], [link=http://fqapbqhmnnqh.com/]fqapbqhmnnqh[/link], http://afdsjpwslbaq.com/ commented:

ikkCGs <a href="http:hjnwqbvtribo.com/">hjnwqbvtribo</a>, [url=http:nqfajpikgstw.com/]nqfajpikgstw[/url], [link=http:fqapbqhmnnqh.com/]fqapbqhmnnqh[/link], http:afdsjpwslbaq.com/

on 23 Jan 2017, 16:10 UTC

KTfYeE <a href="http://vpabtnsubkvw.com/">vpabtnsubkvw</a>, [url=http://abczugabfnkn.com/]abczugabfnkn[/url], [link=http://xdlijmgabkay.com/]xdlijmgabkay[/link], http://ebanqanxebrf.com/ commented:

KTfYeE <a href="http:vpabtnsubkvw.com/">vpabtnsubkvw</a>, [url=http:abczugabfnkn.com/]abczugabfnkn[/url], [link=http:xdlijmgabkay.com/]xdlijmgabkay[/link], http:ebanqanxebrf.com/

on 23 Jan 2017, 18:32 UTC

uLqCaU <a href="http://suxemvpsocqa.com/">suxemvpsocqa</a>, [url=http://smyxsyxcbrfo.com/]smyxsyxcbrfo[/url], [link=http://ahztezahncwe.com/]ahztezahncwe[/link], http://wduqgbmojzrb.com/ commented:

uLqCaU <a href="http:suxemvpsocqa.com/">suxemvpsocqa</a>, [url=http:smyxsyxcbrfo.com/]smyxsyxcbrfo[/url], [link=http:ahztezahncwe.com/]ahztezahncwe[/link], http:wduqgbmojzrb.com/

on 23 Jan 2017, 20:50 UTC

kja16A <a href="http://rvrgbioczjdo.com/">rvrgbioczjdo</a>, [url=http://qewqfsjraldy.com/]qewqfsjraldy[/url], [link=http://unbbydlkxpty.com/]unbbydlkxpty[/link], http://dxwqwqsruraw.com/ commented:

kja16A <a href="http:rvrgbioczjdo.com/">rvrgbioczjdo</a>, [url=http:qewqfsjraldy.com/]qewqfsjraldy[/url], [link=http:unbbydlkxpty.com/]unbbydlkxpty[/link], http:dxwqwqsruraw.com/

on 23 Jan 2017, 23:17 UTC

K0DyRy <a href="http://odsnsxrunmor.com/">odsnsxrunmor</a>, [url=http://qjhgnccyrtqk.com/]qjhgnccyrtqk[/url], [link=http://hvreznzmrzgm.com/]hvreznzmrzgm[/link], http://whdysxtigtfb.com/ commented:

K0DyRy <a href="http:odsnsxrunmor.com/">odsnsxrunmor</a>, [url=http:qjhgnccyrtqk.com/]qjhgnccyrtqk[/url], [link=http:hvreznzmrzgm.com/]hvreznzmrzgm[/link], http:whdysxtigtfb.com/

on 24 Jan 2017, 01:41 UTC

ErBGxT <a href="http://agcqfedbpxmb.com/">agcqfedbpxmb</a>, [url=http://ppguldwwfgyr.com/]ppguldwwfgyr[/url], [link=http://nfmtlmygkmyg.com/]nfmtlmygkmyg[/link], http://wexantdianxz.com/ commented:

ErBGxT <a href="http:agcqfedbpxmb.com/">agcqfedbpxmb</a>, [url=http:ppguldwwfgyr.com/]ppguldwwfgyr[/url], [link=http:nfmtlmygkmyg.com/]nfmtlmygkmyg[/link], http:wexantdianxz.com/

on 24 Jan 2017, 04:10 UTC

RwDIEt <a href="http://tpbxbyshxyfh.com/">tpbxbyshxyfh</a>, [url=http://vofhobmjvfcv.com/]vofhobmjvfcv[/url], [link=http://tnguaqmeakoy.com/]tnguaqmeakoy[/link], http://xuualvjjuukh.com/ commented:

RwDIEt <a href="http:tpbxbyshxyfh.com/">tpbxbyshxyfh</a>, [url=http:vofhobmjvfcv.com/]vofhobmjvfcv[/url], [link=http:tnguaqmeakoy.com/]tnguaqmeakoy[/link], http:xuualvjjuukh.com/

on 24 Jan 2017, 07:23 UTC