Discussion: Introducing new operators for matrix computation

Bjorn Pettersen bjorn at roguewave.com
Fri Jul 14 13:38:51 EDT 2000


Huaiyu Zhu wrote:
> 
> On Thu, 13 Jul 2000 16:35:57 -0600, Bjorn Pettersen <bjorn at roguewave.com>
> wrote:
> 
> >I fully understand your desire to make the syntax for your domain
> >specific notation as intuitive as possible.  For people who don't know
> >matlab however, .+ et. al. have no pre-defined meaning. Since + has a
> >predefined meaning of addition, most people when overloading it will do
> >"additive" things with it, e.g. concatenating to sequences. Since .+
> >doesn't have a pre-defined meaning I'm afraid people will override it
> >with random semantics...
> >
> >If you want to do matlab syntax, perhaps a better approach would be to
> >write your own expression parser?
> >
> 
> Hmm, I see what you mean:  If a certain syntax is not meaningful to all the
> users, don't expose it to all of them lest they came up with random
> incompatible usages.  Sounds reasonable, but ...
> 
> Point 1.  We _can_ assign a universal meaning to dot operators, as
> "componentwise operators".  For example
> 
> [1, 2] + [3, 4]             # [1, 2, 3, 4]
> [1, 2] .+ [3, 4]            # [4, 6]
> ['a','b'] * 2               # ['a','b','a','b']
> ["a","b"] .* 2              # ['aa', 'bb']
> {'a':1, 'b':1} .+ {'a':1, 'c':1}   # {'a':2, 'b':1, 'c': 1}
> 'abc' .+ 'def'              # exception: you must define string.__dot_add__

Well, this could actually be generally useful, but not with the .+
syntax (since the dot would interfer with methods on the sequence
object). If I had the ability to do elementwise computations, I would
want to do something like:

  myList @foo()  == map(lambda x:x.foo(), myList) == [x.foo(); for x in
myList]
  [1,2] @+ 2     == map(lambda x:x+2,     [1,2])  == [x+2; for x in
[1,2]]
  [1,2] @+ [3,4] == ??

Reading "x@<something>" as distribute the <somthing> operation over the
elements in x. I think something like this has the potential to make
code more declarative (ie. shorter and easier to understand)...

-- bjorn




More information about the Python-list mailing list