# Maxima vector magnitude and cross product

I'm new to maxima. And I have two questions:

1. Question I have a little problem with computing vector's magnitude. Now I'm using such form as

sqrt(v1.v1)


This code looks very ugly.

2. Question I thought vector cross product is expressed like a~b. but Maxima says ~ is not an infix operator

As far as I know, there is no built-in function to calculate a vector's magnitude, but you can easily define your own:

(%i1) norm(x) := sqrt(x . x)$(%i2) norm([1, 1]); (%o2) sqrt(2)  The cross product operator ~ is only available after loading 'vect': (%i1) load("vect")$
(%i2) [1, 2, 3] ~ [2, 3, 4];
(%o2)                        [1, 2, 3] ~ [2, 3, 4]
(%i3) express(%);
(%o3)                            [- 1, 2, - 1]


well, I didn't like the vec library, so I decided to write my own functions. here it goes:

/*this function gets 3 values as elements of a 3D vector and gives a (3,1) column matrix as the representation of the vector */
vec(vec_tempvar_a1,vec_tempvar_b1,vec_tempvar_c1):=block(
matrix([vec_tempvar_a1],[vec_tempvar_b1],[vec_tempvar_c1]))$/*this function gets a (3,1) column matrix as representation of a 3D vector and delivers a scalar as the magnitude of the vector*/ vecmag(vecmag_tempvar_V1):= block( sqrt(transpose(vecmag_tempvar_V1).vecmag_tempvar_V1))$

dotprod(dotprod_tempvar_V1,dotprod_tempvar_V2):=block(
transpose(dotprod_tempvar_V1).dotprod_tempvar_V2)$/*this function calculates the vector/cross product of the vector on the left on the vector on the right*/ crosprod(crosprod_temvar_V1,crosprod_temvar_V2):= block( matrix([(crosprod_temvar_V1[2,1]*crosprod_temvar_V2[3,1]-crosprod_temvar_V1[3,1]*crosprod_temvar_V2[2,1])], [(crosprod_temvar_V1[3,1]*crosprod_temvar_V2[1,1]-crosprod_temvar_V1[1,1]*crosprod_temvar_V2[3,1])], [(crosprod_temvar_V1[1,1]*crosprod_temvar_V2[2,1]-crosprod_temvar_V1[2,1]*crosprod_temvar_V2[1,1])]) )$


I have also been working on a maxima library for rigitd body dynamics, specially robotic applications using screw theory in particular. you may find it at github.com/Foadfs/RMaxima.

If you don't mind using lists as vectors a very simple solution would be functions::

cross(u, v) := [u[2] * v[3] - v[2] * u[3], v[1] * u[3] - u[1] * v[3], u[1] * v[2] - v[1] * u[2]]$$dot(u, v) := u[1] * v[1] + u[2] * v[2] + u[3] * v[3]$$
norm(u) := sqrt(dot(u, u))\$


this is not an ideal nor safe solution but a concise one.