Message Boards Message Boards

0
|
1382 Views
|
8 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Pattern matching doesn't work as expected?

Posted 7 months ago

Why doesn't this work?

test[x_List;/VectorQ[x, NumberQ]] := 1;
test[{2.1, 1.2, 3.1}]

It doesn't evaluate test, even though the argument is a list of numbers. I have the latest version of Mathematica.

POSTED BY: Iuval Clejan
8 Replies

Sorry I didn't study your code carefully. If you need to prevent evaluation until a certain step, you may consider something like Inactive and Activate.

POSTED BY: Gianluca Gorni
Posted 7 months ago

I tried it, as well as Hold and ReleaseHold, but I am not understanding enough of the under-the-hood details to make it work.

POSTED BY: Iuval Clejan
Posted 7 months ago

OK, I very much appreciate your help so far. I am still having trouble. Please see enclosed file. I need all these cells to evaluate to a number, and the first 2 so far aren't. The reason I need to specify the patterns in ffoutsuper is because I need to give NDSolve a list of equations without evaluating ffoutsuper (this works just fine, though I don't show it here), but after that I want the equations to be solved, which involves not only numerically evaluating ffoutsuper, but also its derivatives with respect to the list variables. And I need to keep the list structures, or things get to unwieldy to code. The real ffoutsuper is much more complicated than in this simplified example (involving a bunch of lists, and not 0). I get an error that the initial (t=0) derivative (with respect to an independent time variable t) is non-numerical, because the derivative with respect to the other variables does not evaluate to a number.

POSTED BY: Iuval Clejan

Very curious. It appears that you cannot mix numeric tests like x_Real or x_?NumberQ with list structures inside Derivative:

Clear[f, g, h, x];
f[{x_Real}] = x^2;
Derivative[{1}][f][{1.}]
g[x_Real, {y_}] = x^2;
Derivative[1, {0}][g][1., {1.}]
h[{x_?NumberQ}] = x^2;
Derivative[{1}][h][{1.}]
POSTED BY: Gianluca Gorni
Posted 7 months ago

Thank you Gianluca for your generous help (and Hans too). I feel like I need to give back to this forum so I am not free riding, but I am not an advanced user of Mathematica, and I also am daunted by the sheer number of questions.

In any case, in the past, when I was not dealing with the list structures, I would pass all the elements of the lists as individual arguments to ffoutsuper, and prevent evaluation in the eqnlist by requiring that the arguments to ffoutsuper be Real numbers (which allowed me to pass it arguments like a[1,1][t] in eqnlist. Now I was trying to do similarly, by requiring certain of the arguments to be vectors of numbers (there is also a scalar that is not always there, or I could use it, but as you saw in the previous example, even when it is there, Derivative doesn't like it when there are other arguments which are lists), which happens once NDSolve starts evaluating.

I am enclosing the file that gives a bit more information about the eqnlist (though not about the intricate details of ffoutsuper, which itself calls another NDSolve, and other functions that need to be passed the scalars, vectors and matrices that I am passing to ffoutsuper. Perhaps there is some other way to create the eqnlist to be passed to the top level NDSolve without evaluating ffoutsuper (as in delatyed evalutation), while having it evaluate later when taking derivatives with respect to the non-time variables?

POSTED BY: Iuval Clejan
Posted 7 months ago

Sorry... I don't know how that happened. I thought I cut and pasted from an example in the documentation, but I must have changed it.

POSTED BY: Iuval Clejan

Also beware that NumberQ[Pi] is False, which is not quite intuitive. You may consider the following version, which is a little more coincise:

test[x : {___?NumberQ}] := 1
POSTED BY: Gianluca Gorni
Posted 7 months ago

It is not a bug, it is your syntax/typing error. Swap the slash and semicolon:

test[x_List /; VectorQ[x, NumberQ]] := 1
POSTED BY: Hans Milton
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