Prev: Special Characters Palette
Next: FindFit
From: Daniel on 18 Dec 2009 06:22 Hello, I am working with functions that have vector and matrix valued arguments. I'd like to postpone going to a component representation as late as possible, but that is not easy because some vectors are already in the component form and this gives me error messages from cross and times. One example: I have several vector valued quantities x,y,z (let's say 100 dimensional). I'd like to write all expressions in the form f[x_,y_]:=x.y instead of f[x1_,x2_,...,_x100,y1_,...]:=... The problem is, that in the first case the system does not know it is dealing with 100 dimensional vectors. g[x_] :=5 x Cross[g[a] , {1, 0, 0}] should give me something like g[a] x {1,0,0}, e.g. stay unevaluated or alternatively be evaluated in component form {0, 5 a3, - 5 a2}. Instead I get an error message: Cross::nonn1: The arguments are expected to be vectors of equal length, and the number of arguments is expected to be 1 less than their length In addition, I'd like to write the gradient of a function with one or several vector arguments, i.e. something like gradient_y f, which should be a vector with the components df/dy_i, i.e. D[f[x,y], {{y1,y2,y3,...y100}}]. This does not work either, because the system does not now that y is in reality {y1,..,y100} How can I deal naturally with vectors, e.g. have vector valued variables as function arguments and return values, use the usual vector functions (cross, dot, grad, div, rot), and diff with respect to a vector. Many thanks Dan
From: dh on 21 Dec 2009 06:54 Hi Daniel, Cross[a,{1,2,3}] Cross gives an error message but leaves the expression unevaluated as you require. Well, you may eliminate the message by e.g. using Quiet or you may define your own Cross that only evaluates for vector arguments: Cross[x1_?VectorQ, x2_?VectorQ] := Cross[x1, x2] Note that if you have n dimensions, Cross needs n-1 arguments! You may also define your own symbolic gradient that only evaluates if you give an explicite vector: myGrad[f_[x_?VectorQ]] := Table[Derivative[i] f[x], {i, Length[x]}] Daniel (Huber) Daniel wrote: > Hello, > > I am working with functions that have vector and matrix valued > arguments. I'd like to postpone going to a component representation as > late as possible, but that is not easy because some vectors are > already in the component form and this gives me error messages from > cross and times. > > One example: I have several vector valued quantities x,y,z (let's say > 100 dimensional). > I'd like to write all expressions in the form > f[x_,y_]:=x.y > instead of > f[x1_,x2_,...,_x100,y1_,...]:=... > > The problem is, that in the first case the system does not know it is > dealing with 100 dimensional vectors. > > g[x_] :=5 x > Cross[g[a] , {1, 0, 0}] > should give me something like g[a] x {1,0,0}, e.g. stay unevaluated > or alternatively be evaluated in component form {0, 5 a3, - 5 a2}. > > Instead I get an error message: > Cross::nonn1: The arguments are expected to be vectors of equal > length, and the number of arguments is expected to be 1 less than > their length > > In addition, I'd like to write the gradient of a function with one or > several vector arguments, i.e. something like > gradient_y f, > which should be a vector with the components df/dy_i, i.e. D[f[x,y], > {{y1,y2,y3,...y100}}]. This does not work either, because the system > does not now that y is in reality {y1,..,y100} > > > How can I deal naturally with vectors, e.g. have vector valued > variables as function arguments and return values, use the usual > vector functions (cross, dot, grad, div, rot), and diff with respect > to a vector. > > Many thanks > > > Dan >
From: David Bailey on 21 Dec 2009 06:57 Daniel wrote: > Hello, > > I am working with functions that have vector and matrix valued > arguments. I'd like to postpone going to a component representation as > late as possible, but that is not easy because some vectors are > already in the component form and this gives me error messages from > cross and times. > > One example: I have several vector valued quantities x,y,z (let's say > 100 dimensional). > I'd like to write all expressions in the form > f[x_,y_]:=x.y > instead of > f[x1_,x2_,...,_x100,y1_,...]:=... > > The problem is, that in the first case the system does not know it is > dealing with 100 dimensional vectors. > > g[x_] :=5 x > Cross[g[a] , {1, 0, 0}] > should give me something like g[a] x {1,0,0}, e.g. stay unevaluated > or alternatively be evaluated in component form {0, 5 a3, - 5 a2}. > > Instead I get an error message: > Cross::nonn1: The arguments are expected to be vectors of equal > length, and the number of arguments is expected to be 1 less than > their length > > In addition, I'd like to write the gradient of a function with one or > several vector arguments, i.e. something like > gradient_y f, > which should be a vector with the components df/dy_i, i.e. D[f[x,y], > {{y1,y2,y3,...y100}}]. This does not work either, because the system > does not now that y is in reality {y1,..,y100} > > > How can I deal naturally with vectors, e.g. have vector valued > variables as function arguments and return values, use the usual > vector functions (cross, dot, grad, div, rot), and diff with respect > to a vector. > > Many thanks > > > Dan > Really, you are asking how to do abstract algebra using Mathematica. The best way to do work like that, is to use some of the unassigned operators - such as \[CircleTimes] to represent operators such as Cross. These are inert, but you can give them the attributes that you require (SetAttributes) - say to make them associative - and you can write definitions that describe what they should do. You can also define (usually recursive) functions that act on expressions of this sort. When the time comes that you want to go to a fully component representation and evaluate the result, you can simply replace \[CicleTimes] by Cross (say). Likewise, I would define your own differentiation operator - say DD - so you can define exactly what it does, and then switch to standard Mathematica differentiation when everything has been reduced to components. Of course, there may well be packages out there that do all this for the sorts of problems you are interested in. David Bailey http://www.dbaileyconsultancy.co.uk
|
Pages: 1 Prev: Special Characters Palette Next: FindFit |