# PVector methods versus mathematical operators?

**URL:** <https://discourse.processing.org/t/pvector-methods-versus-mathematical-operators/20029>\
**Category:** Processing.py\
**Created:** [April 22, 2020, 11:19am UTC](https://discourse.processing.org/t/pvector-methods-versus-mathematical-operators/20029 "2020-04-22T11:19:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [April 22, 2020, 11:19am UTC](https://discourse.processing.org/t/pvector-methods-versus-mathematical-operators/20029/1 "2020-04-22T11:19:34Z")

</div>

Looking at the [PVector reference entry](https://py.processing.org/reference/PVector.html), it states –

“Since vectors represent groupings of values, we cannot simply use traditional addition/multiplication/etc. Instead, we’ll need to do some “vector” math, which is made easy by the methods inside the PVector class.”

But arithmetic operators appear to work fine:

```auto
# methods
a = PVector(1, 1)
b = PVector(1, 1)
a.add(b)
print(a) # (2, 2)
print(a.div(2)) # (1, 1)

# arithmetic operators
a = PVector(1, 1)
b = PVector(1, 1)
a += b
print(a) # (2, 2)
print(a/2) # (1, 1)

```

What are the advantages (if any) of using the methods? Is this some Python vs Java Mode thing?

---

<div class="post-metadata">

**Author:** ![behreajj](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@behreajj](https://discourse.processing.org/u/behreajj)\
**Post date:** [April 22, 2020, 12:38pm UTC](https://discourse.processing.org/t/pvector-methods-versus-mathematical-operators/20029/2 "2020-04-22T12:38:17Z")

</div>

Hi @tablereturn,

I’m not as familiar with the Python implementation of Processing, but if [this](https://github.com/jdf/processing.py/blob/57f6e4d0402687306b0fb43701659d5001364b26/runtime/src/jycessing/core.py#L180) is the right spot in the source code, then the Python operator definition calls either the static or the instance method.

For advantages vs. disadvantages, if you search for debates about whether languages should allow operator overloading or not, you’ll find a variety of responses. I’m not sure exactly what the documentation is referencing… Maybe to how, for example, the `*` operator could refer ambiugously to

- component-wise multiplication betweeen two vectors
- multiplication between a vector and a scalar
- the cross product
- the dot product?

The ambiguity between the first two is not as great in languages where the variable data type is clearer (i.e., `PVector#mult(PVector, float)` vs. `PVector#mult(PVector, PVector)`).

Best,  
Jeremy

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 22, 2020, 1:04pm UTC](https://discourse.processing.org/t/pvector-methods-versus-mathematical-operators/20029/3 "2020-04-22T13:04:44Z")

</div>

> [@tabreturn](#):
>
> Looking at the [PVector reference entry](https://py.processing.org/reference/PVector.html), it states –

Processing flavors many times rely on Processing’s Java Mode reference as base.

> [@tabreturn](#):
>
> But arithmetic operators appear to work fine:

Python Mode’s dev subclassed the original Processing’s PVector in order to add overloaded operators:

> <https://github.com/jdf/processing.py/blob/3056/runtime/src/jycessing/core.py#L180-L241>

We can still access the original PVector w/o overloaded operators via ` __pvector__ ` though:

> <https://github.com/jdf/processing.py/blob/3056/runtime/src/jycessing/core.py#L26-L27>

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [April 22, 2020, 7:25pm UTC](https://discourse.processing.org/t/pvector-methods-versus-mathematical-operators/20029/4 "2020-04-22T19:25:37Z")

</div>

Of course languages should allow operator overloading 😉

Great answer! Thanks, @behreajj

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [April 25, 2020, 4:13pm UTC](https://discourse.processing.org/t/pvector-methods-versus-mathematical-operators/20029/5 "2020-04-25T16:13:00Z")

</div>

This should have been fixed by a PR from me on the documentation, but for some reason it is not ‘live’ at he site ☹

> <https://github.com/jdf/Processing.py-Bugs/issues/284>
>
> https://processing.org/reference/PVector.html has lots of good documentation for PVector methods.
> https://py.processing.org/reference/PVector.html doesn't have any documentation of PVector methods.

> <https://github.com/jdf/processing-py-site/pull/153/commits/54f7fbff12531029dfef28d0d8893b2fe60b6f08>
