vector^2 without a warning?

Messages in english

Modérateur : xcasadmin

niccolo
Messages : 26
Inscription : dim. mars 02, 2014 2:13 pm

vector^2 without a warning?

Message par niccolo » mar. mai 28, 2019 10:30 am

I often need to "dot product" a vector with itself. E.g., "(pointB - pointA)^2". But when I do this I see the warning:
Warning, ^ is ambiguous on non square matrices. Use .^ to apply ^ element by element.
Is there a way to get rid of this warning? I know I can do "dot(expr, expr)" instead, but then I have to type the expression twice.

parisse
Messages : 5734
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: vector^2 without a warning?

Message par parisse » mar. mai 28, 2019 10:40 am

Just follow the hint: replace ^ by .^ like (pointB - pointA).^2

niccolo
Messages : 26
Inscription : dim. mars 02, 2014 2:13 pm

Re: vector^2 without a warning?

Message par niccolo » mar. mai 28, 2019 12:48 pm

parisse a écrit :
mar. mai 28, 2019 10:40 am
Just follow the hint: replace ^ by .^ like (pointB - pointA).^2
Given the vector [a,b,c], I want to arrive at a^2 + b^2 + c^2.

[a,b,c]^2 does this (but gives a warning).

[a,b,c].^2 gives [a^2, b^2, c^2]. Not good.

I know I can do "[a,b,c]*[a,b,c]" or "sum([a,b,c].^2)", but I was wondering if there's a shorter syntax.

parisse
Messages : 5734
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: vector^2 without a warning?

Message par parisse » mar. mai 28, 2019 6:43 pm

Indeed, sorry. l2norm(v)^2 will do.

Répondre