Message Boards Message Boards

0
|
11738 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How do I teach Mathematica that a object is a vector or a matrix?

Posted 10 years ago

I'm migrating some SMP code. In SMP odd[Vector]:1 and diffsn[Matrix]:1 worked fine. I've tried SetAttributes[odd,Vector]. But VectorQ[odd] returns False. What do I need to do to get Mathematica to recognize that 'odd' is a vector and 'diffsn' is a matrix?

POSTED BY: Anthony DeGance
4 Replies
Posted 10 years ago

I'm not sure if this will help with your final goal, but you can use the concept of UpValues. UpValues allow you to define transformation rules for symbols. In short notation, this is done as follows.

odd /: VectorQ[odd] := True

Then,

VectorQ[odd]

returns

True

But, David Reiss suggestion of using patterns like _?VectorQ might be what you're looking for.

POSTED BY: David G

I only used SMP a bit way back when... I was using Macsyma while SMP was being developed... long story... ;-)

With regard to da'question....

Some functions in Mathematica have as an option the option Assumptions (without the dollar sign). For those functions the default value of that option is usually

Assumptions->$Assumptions

That is, the adjustable system parameter $Assumptions will be used in those functions if no alternative value of that option is set explicitly, thereby giving a means to set a list of assumptions for all such functions in a Mathematica session. Not all functions have an Assumptions option (in fact most do not). In particular the functions VectorQ and MatrixQ do not. But functions like Simplify, FullSimplify, and in our case TensorDimensions do have the Assumptions option. Thus if you set

$Assumptions = {v1 \[Element] Vectors[3], v2 \[Element] Vectors[3]}

thus telling, for example, TensorDimensions that v1 and v2 are both R3 vectors, one will find that

TensorDimensions[v1\[TensorProduct]v2]

returns

{3, 3}

but

TensorDimensions[a\[TensorProduct]b] 

returns unevaluated since there is no information about the nature of a and b.

So, the question still on the table (if it is easy enough to summarize the answer to) is how are you wanting to make use of the vectorness or matrixness of your parameters? Can your needs be met via patterns like _?VectorQ ?

POSTED BY: David Reiss

Yes, really. It was Dale Prouty and the folks at Inference who taught me SMP.

I did research in the area of dispersion approximations to the vector convective diffusion equation, Appl. Sci. Res., vol. 43, no. 4, p. 239. I used SMP to solve a system of perturbation like equations in which the matrices did not commute. So I'm trying learn Mathematica by migrating that SMP code.

Okay, back to da'question. So if I enter (the dollar sign is there) $ $Assumptions = odd \[Element] Vectors[3], $Assumptions = half \[Element] Vectors[3],
odd.half, should I not expect the response odd.half? Or at least if I enter VectorQ[odd] to return True and not False? SMP allowed me to carry on a charade, so to speak, which I was happy to let it do since I wanted the results in symbolic terms.

POSTED BY: Anthony DeGance

Really?? SMP?? ;-)

http://en.wikipedia.org/wiki/Symbolic_Manipulation_Program

But to get back to your question... there isn't a global way to specify that odd is a vector, though using, for example,

$Assumptions = odd \[Element] Vectors[3]

will cause tensor manipulation recognition of the fact that odd is an Rn vector, for example (see http://reference.wolfram.com/language/ref/Vectors.html )

So the question to ask in response is where will you use VectorQ in your code? Certainly in defining a function you can use a pattern to specify that the function only acts on vectors as in

f[v_?VectorQ] := Plus@@v

So the more detailed answer to your question may depend on how you want to use the restriction of your parameter to being a vector.

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract