# User defined plus and product.

**URL:** <https://community.wolfram.com/t/user-defined-plus-and-product/5822>\
**Category:** Questions\
**Tags:** computer-science, mathematica\
**Created:** [May 27, 2015, 9:04pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822 "2015-05-27T21:04:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ambassadeurkosh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ambassadeurkosh/32/16368_2.png) [@ambassadeurkosh](https://community.wolfram.com/u/ambassadeurkosh)\
**Post date:** [May 27, 2015, 9:04pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/1 "2015-05-27T21:04:52Z")

</div>

Hello.

I’m a new user of Mathematica. So please be patient with me 🙂

I try to define E = { e1, e2, e3, e4 }, a set, and two operations on E : the plus and the product.  
The idea is to compute product of matrices using those user defined operations.

Could someone give me the syntax and the “how to” to make it work ?

Regards

---

<div class="post-metadata">

**Author:** ![kayh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kayh/32/31501_2.png) [@kayh](https://community.wolfram.com/u/kayh)\
**Post date:** [May 27, 2015, 9:25pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/2 "2015-05-27T21:25:35Z")

</div>

What you are looking for probably already exists:  
[matrix operations](http://reference.wolfram.com/language/guide/MatrixOperations.html) and  
[vector + matrices](http://reference.wolfram.com/language/tutorial/VectorsAndMatrices.html)

but you can define your own as well

---

<div class="post-metadata">

**Author:** ![ambassadeurkosh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ambassadeurkosh/32/16368_2.png) [@ambassadeurkosh](https://community.wolfram.com/u/ambassadeurkosh)\
**Post date:** [May 28, 2015, 12:24pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/3 "2015-05-28T12:24:30Z")

</div>

Thanks helping, but it doesnt help.  
I alrerady know Mathematica provide matrices product. I dont need to change matrix or vector product, but establishing “computing rules” between some elements of a set, and use it.

example :

E = { a,b,c }

plus :  
(a,a) → a,  
(a,b) → b,  
(b,a) → b,  
…

Multiply :  
(a,a) → b,  
(a,b) → a

And now, if I compute  
( {{a, a}, {b, a} } ) ( {{a, b}, {a, a}} )

I expect the result is :  
{{b, a}, {a, b}}

and not only :  
{{a^2, a b}, {a b, a^2}}

I tried to search on google, but I dont have the good “key-words” to collect some interesting results.

I know it’s something about finite groups, algebra or something like that, but i didnt found at now…

Please help.

---

<div class="post-metadata">

**Author:** ![jpd](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jpd/32/34994_2.png) [@jpd](https://community.wolfram.com/u/jpd)\
**Post date:** [May 28, 2015, 1:49pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/4 "2015-05-28T13:49:21Z")

</div>

Write your own add and multiply functions. Combine them using **Inner** to make your matrix product function.

---

<div class="post-metadata">

**Author:** ![ambassadeurkosh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ambassadeurkosh/32/16368_2.png) [@ambassadeurkosh](https://community.wolfram.com/u/ambassadeurkosh)\
**Post date:** [May 28, 2015, 2:52pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/5 "2015-05-28T14:52:28Z")

</div>

Excellent !!!  
Inner[f,list1,list2,g] is exactly what I was searching for.

The last part now is : defining f and g with elegant writing. Let’s consider the informations I have are an enumeration of all tuples X+Y=Z, could I define f and g by using some kind of pattern matching or some similar mechanism ? what is the most “readable” technique for such kind of definitions ?

Regards

Frédéric DIDIER

---

<div class="post-metadata">

**Author:** ![jpd](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jpd/32/34994_2.png) [@jpd](https://community.wolfram.com/u/jpd)\
**Post date:** [May 28, 2015, 3:24pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/6 "2015-05-28T15:24:57Z")

</div>

To use pattern matching to turn an enumeration into a function, define your “plus” function as follows:

```
plus[a,a]=a
plus[a,b]=b
plus[b,a]=b
...

```

When the left hand side matches, the right hand side replaces it. Note that `a` and `b` are not named function arguments (that would be  
`a_` and `b_`), but literal symbols that must match the symbols in those slots for the definition to take effect.

---

<div class="post-metadata">

**Author:** ![ambassadeurkosh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ambassadeurkosh/32/16368_2.png) [@ambassadeurkosh](https://community.wolfram.com/u/ambassadeurkosh)\
**Post date:** [May 28, 2015, 3:41pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/7 "2015-05-28T15:41:13Z")

</div>

Just what I was searching for. It perfectly uses a and b as ‘atoms’.

Thanks for your patience.

---

<div class="post-metadata">

**Author:** ![dlkeith](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dlkeith/32/31494_2.png) [@dlkeith](https://community.wolfram.com/u/dlkeith)\
**Post date:** [May 28, 2015, 3:45pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/8 "2015-05-28T15:45:20Z")

</div>

This might also be interesting: [Operators without Built In Meanings](http://reference.wolfram.com/language/tutorial/OperatorsWithoutBuiltInMeanings.html)

```
In[1]:= CirclePlus[a, b] = b

Out[1]= b

In[2]:= a \[CirclePlus] b

Out[2]= b

```

(Note that [CirclePlus] can be entered as esc c+ esc and looks like a CirclePlus symbol in an actual notebook.)

---

<div class="post-metadata">

**Author:** ![ambassadeurkosh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ambassadeurkosh/32/16368_2.png) [@ambassadeurkosh](https://community.wolfram.com/u/ambassadeurkosh)\
**Post date:** [May 28, 2015, 4:17pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/9 "2015-05-28T16:17:31Z")

</div>

Nice feature.  
I really appreciate the opulence of symbols. It make articles more readable and attractive.

Thanks for the tip.

---

<div class="post-metadata">

**Author:** ![ambassadeurkosh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ambassadeurkosh/32/16368_2.png) [@ambassadeurkosh](https://community.wolfram.com/u/ambassadeurkosh)\
**Post date:** [May 28, 2015, 8:27pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/10 "2015-05-28T20:27:21Z")

</div>

I met a last difficulty I didnt solve.  
I didnt see it 4 hours ago because of the sample limitation at dimension 2.

If I work on :

```
Inner[dwproduct, ( {
   {1, On, 0, 0},
   {On, 1, On, On},
   {0, On, 1, 0},
   {0, On, 0, 1}
  } ), ( { {V},{N},{N},{N} } ), wsum]

```

The given result is

```
{
 {wsum[V, N, 0, 0]},
 {wsum[V, N, N, N]},
 {wsum[0, N, N, 0]},
 {wsum[0, N, 0, N]}
}

```

So, how to define wsum a general/generic way to avoid specialize for each tuple ? (i probably will compute samples with matrices at dimension 600 and more)

Could I imagine something recursive (or not) that will transfer “evaluation capability” to binary operation ?

Let’s say wsum[x1,x2,x3,…,xn]= ((((x1)+x2)+x3)+…)+xn

---

<div class="post-metadata">

**Author:** ![ambassadeurkosh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ambassadeurkosh/32/16368_2.png) [@ambassadeurkosh](https://community.wolfram.com/u/ambassadeurkosh)\
**Post date:** [May 28, 2015, 9:32pm UTC](https://community.wolfram.com/t/user-defined-plus-and-product/5822/11 "2015-05-28T21:32:33Z")

</div>

I finally found a recursive possibility :

f [x\_ ,y\_ , z\_\_] = f [f [x,y] , z ]

hope recursive aspect will be replaced at execution time by iterative computation…
