How to modify arithmetic operators (+,-,x)

lfcmaniac

I am currently writing a Linear Algebra module for Python 3.x wherein I deal with self-defined matrix objects.

Is there any way I can make the basic arithmetic operators like +, -, * adhere to my matrix objects? For example -

>>> A = matrix("1 2;3 4")
>>> B = matrix("1 0; 0 1")
>>> A + B
[2 2]
[3 5]
>>> A * A
[7 10]
[15 22]

Right now I have written separate functions for addition, multiplication, etc. but typing A.multiply(A) is much more cumbersome than simply A*A.

Vladimir

You are looking for special methods. Particularly at emulating numerical types section.

Also, as you're trying to implement matrices and matrices are containers, you may find useful to define custom container methods for your type.

UPDATE: Here is an example of custom objects using special methods to implement arithmetical operators:

class Value(object):
    def __init__(self, x):
        self.x = x

    def __add__(self, other):
        if not isinstance(other, Value):
            raise TypeError
        return Value(self.x + other.x)

    def __mul__(self, other):
        if not isinstance(other, Value):
            raise TypeError
        return Value(self.x * other.x)

assert (Value(2) + Value(3)).x == 5
assert (Value(2) * Value(3)).x == 6

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to avoid NullPointerException from arithmetic operators in Java?

From Dev

How to pass arithmetic operators as function parameters in LISP?

From Dev

How to use arithmetic operators to get cell location

From Dev

Where/how are the arithmetic operators for atomic< T > defined?

From Dev

How to Properly Use Arithmetic Operators Inside Comprehensions?

From Dev

How to perform arithmetic on values and operators expressed as strings?

From Dev

Where/how are the arithmetic operators for atomic< T > defined?

From Dev

How to use arithmetic operators with SAS macro variables

From Dev

how can i save values with arithmetic operators in mysql with php?

From Dev

Understanding arithmetic operators in Python

From Dev

Arithmetic on two CGPoints with + or - operators

From Dev

Treat arithmetic operators as functions

From Dev

Array of Arithmetic Operators in Swift

From Dev

Calculation program with arithmetic operators

From Dev

Storing and using arithmetic operators

From Dev

Javascript:Inconsistency with arithmetic operators?

From Dev

Trouble with arithmetic operators in AngularJS

From Dev

Bitwise arithmetic, How can x*x<0?

From Dev

Are arithmetic operators ever preferable to arithmetic functions?

From Dev

Is there an equivalent to optional chaining with arithmetic operators?

From Dev

"Simple" Expression Language - arithmetic operators?

From Dev

Chain arithmetic operators in dplyr with %>% pipe

From Dev

"Simple" Expression Language - arithmetic operators?

From Dev

Action in controller not accepting arithmetic operators

From Dev

overloading arithmetic operators c++

From Dev

Copying a variable with only arithmetic operators

From Dev

How can I express the term ( a < b ? 0 : 1 ) using only bitwise or arithmetic operators?

From Dev

Perform arithmetic operations from operators defined as strings

From Dev

Java operators performance arithmetic vs bitwise

Related Related

  1. 1

    How to avoid NullPointerException from arithmetic operators in Java?

  2. 2

    How to pass arithmetic operators as function parameters in LISP?

  3. 3

    How to use arithmetic operators to get cell location

  4. 4

    Where/how are the arithmetic operators for atomic< T > defined?

  5. 5

    How to Properly Use Arithmetic Operators Inside Comprehensions?

  6. 6

    How to perform arithmetic on values and operators expressed as strings?

  7. 7

    Where/how are the arithmetic operators for atomic< T > defined?

  8. 8

    How to use arithmetic operators with SAS macro variables

  9. 9

    how can i save values with arithmetic operators in mysql with php?

  10. 10

    Understanding arithmetic operators in Python

  11. 11

    Arithmetic on two CGPoints with + or - operators

  12. 12

    Treat arithmetic operators as functions

  13. 13

    Array of Arithmetic Operators in Swift

  14. 14

    Calculation program with arithmetic operators

  15. 15

    Storing and using arithmetic operators

  16. 16

    Javascript:Inconsistency with arithmetic operators?

  17. 17

    Trouble with arithmetic operators in AngularJS

  18. 18

    Bitwise arithmetic, How can x*x<0?

  19. 19

    Are arithmetic operators ever preferable to arithmetic functions?

  20. 20

    Is there an equivalent to optional chaining with arithmetic operators?

  21. 21

    "Simple" Expression Language - arithmetic operators?

  22. 22

    Chain arithmetic operators in dplyr with %>% pipe

  23. 23

    "Simple" Expression Language - arithmetic operators?

  24. 24

    Action in controller not accepting arithmetic operators

  25. 25

    overloading arithmetic operators c++

  26. 26

    Copying a variable with only arithmetic operators

  27. 27

    How can I express the term ( a < b ? 0 : 1 ) using only bitwise or arithmetic operators?

  28. 28

    Perform arithmetic operations from operators defined as strings

  29. 29

    Java operators performance arithmetic vs bitwise

HotTag

Archive