Mathematica Defining function of a vector and symbolic symmetric matrices- mathematica

AI Thread Summary
The discussion focuses on defining a vector-valued function in Mathematica that represents the exponential of a quadratic form using a symbolic matrix. The user seeks to take derivatives with respect to vector components and evaluate the results when the components vanish, while also looking for a simpler way to create a symmetric symbolic matrix. They initially struggled with the correct syntax for defining functions that accept vectors, discovering that using patterns like f[{x_,y_,z_}] works better than f[{x,y,z}_]. A solution was found that allows for arbitrary length vectors with minimal effort, and further suggestions were made to refine function definitions based on list patterns. Overall, the conversation emphasizes the importance of correct syntax and function definitions in Mathematica for handling symbolic computations efficiently.
muppet
Messages
602
Reaction score
1
Hi all,
I'd like to define a vector valued function in mathematica 7 as the exponential of a quadratic form, defined with respect to a purely symbolic matrix. What I want to do with it is to take derivatives with respect to the components of my vector, and evaluate the result when all components of the vector vanish (i.e. the result will just be a sum of products of matrix components). I'm struggling to define a function that takes a vector as an argument- can anyone tell me a good way of doing this?

It would also be helpful to me if I could construct a symmetric symbolic matrix (in a slightly less messy way than adding a symbolic matrix to its transpose), so that it presents an answer in the most simple form, although that's not so essential as I can simplify the result myself if needs be.

Many thanks in advance.
 
Physics news on Phys.org
Would you consider, to help you get the answer you are looking for, using Mathematica to manually show the steps you would like it to calculate for you for the simplest, but non-trivial, example you can come up with? Perhaps a 3x3 matrix with simple symbolic entries somewhat like you might expect to have?
 
Thanks for your reply. As it happens I already know the answer I'm looking for; it's a field theory calculation that's in plenty of books and can be worked out using a few diagrams. It's for a homework in which the derivation of the result is the important thing and it's been suggested that we get a computer to take our derviatives for us, as the procedure generates a very large number of terms only to simplify enormously at the end. The problem I have is getting Mathematica to understand what I want it to do!
 
Well I've now solved my problem, albeit in a very crude way- at the simplest level, it was a question of getting the syntax right (in spite of, rather than aided by, the documentation- f[{x,y,z}_] doesn't work, wheras f[{x_,y_,z_}] does- this despite the command ?f automatically translating f[{x,y,z}_] into f[{x_,y_,z_}]...).

It would still be nice to know, however, if there's a slicker way of defining functions of a vector- preferably one that didn't involve writing the list out explicitly, and that could allow me to readily generalize my approach to an input of arbitrary length with minimal effort.
 
If you don't need to name the individual components or if you can access the components with your own code then you can just name the vector, like this:

In[3]:= f[v_]:=Norm[v];
f[{3,4}]

Out[4]= 5

That works with arbitrary length vectors and minimal effort.
 
Actually, when you define f[{x,y,z}_] it did not get turned into f[{x_,y_,z_}], it actually gets turned into f[{x _, y _, z _}]

What happens, is
f[{x,y,z}_]
is actually
f[Times[{x,y,z},_]]
and since Times has the Attribute Listable, it becomes
f[{Times[x, _], Times[y, _],Times[z, _]}]
which in StandardForm is
f[{x _, y _, z _}]

When you did ?f, it showed the UpValue that you had defined, but if you look closely, there are spaces between the Symbols and the Blanks which means Times, as opposed to the shorthand for a named pattern.
 
And what Bill says is completely correct. Although you could go further by requiring the definition only to fire if, e.g., it is given a List

f[lst_List] := Norm[lst]

or a list of length three

f[lst:{_,_,_}] := Norm[lst]

etc...
 

Similar threads

Replies
3
Views
27K
Replies
6
Views
5K
Replies
4
Views
3K
Replies
1
Views
2K
Replies
6
Views
4K
Replies
18
Views
2K
Replies
4
Views
4K
Back
Top