Forum archive 2000-2006

Michal Charemza - Evaluating a multi-variable function at a point

Michal Charemza - Evaluating a multi-variable function at a point

by Arnold Pizer -
Number of replies: 0
inactiveTopicEvaluating a multi-variable function at a point topic started 8/26/2006; 10:59:21 AM
last post 8/29/2006; 2:41:23 PM
userMichal Charemza - Evaluating a multi-variable function at a point  blueArrow
8/26/2006; 10:59:21 AM (reads: 261, responses: 8)
Hi,

I am trying to use a student's answer, that is a function, as part of of the answer to a later part of the question.

In order to do this, I need to evaluate the function at a point, and use that numerical value. The situation is complicated further by the fact that the function is a function of two variables.

To try to work out how, I have tried:

$point = Formula('2x')->eval(x=>5);

This results in (10) (including the brackets. How can I get the value just as a number without the brackets - (so that it can be used in a num_cmp)?

Also, I have no idea how to extend this to accept functions of more than one variable (in fact - any function that is not purly a function of just "x").

A point to some relavant documentation would be appreciated also, as I can't find anything on "Formula"

Thanks,

Michal Charemza.

<| Post or View Comments |>


userMichal Charemza - Re: Evaluating a multi-variable function at a point  blueArrow
8/26/2006; 11:01:16 AM (reads: 296, responses: 0)
Sorry... I have just realised I was a bit unclear

"In order to do this, I need to evaluate the function at a point, and use that numerical value."

I shouldn't have written "In order to do this". It just happens that in the question I am writing I need to evaluate the entered function at a point.

Michal.

<| Post or View Comments |>


userMichael Gage - Re: Evaluating a multi-variable function at a point  blueArrow
8/26/2006; 11:18:35 AM (reads: 303, responses: 0)
Hi Michael,

I'm not seeing the same result

$point = Formula('2x')->eval(x=>5);

and TEXT($point)

prints out 10 not (10).

I suspect that you can use

ANS($point->cmp)

to check that the student's answer is 10, although there might be a more efficient way (Davide Cervone will know.)

Here is a pointer to what documentation there is for the Parser. If you have access to your webwork server its under ...../webwork2/doc/parser

http://cvs.webwork.rochester.edu/viewcvs.cgi/webwork2/doc/parser/

-- Mike

<| Post or View Comments |>


userMichal Charemza - Re: Evaluating a multi-variable function at a point  blueArrow
8/26/2006; 12:16:41 PM (reads: 303, responses: 0)
> and TEXT($point) > prints out 10 not (10). If I do it this way, then I do get just 10 as well.

However, if I use ( $point ) or just $point after BEGIN_TEXT, then I get the extra brackets as well. If I set a variable $x=5 before BEGIN_TEXT, and display it with $x or ( $x ) after BEGIN_TEXT, then I don't get the extra brackets (as I would expect).

> ...../webwork2/doc/parser Thanks, this looks very helpful.

Michal.

<| Post or View Comments |>


userDavide P. Cervone - Re: Evaluating a multi-variable function at a point  blueArrow
8/26/2006; 1:45:11 PM (reads: 293, responses: 0)
I have tried:

$point = Formula('2x')->eval(x=>5);

This results in (10) (including the brackets. How can I get the value just as a number without the brackets - (so that it can be used in a num_cmp)?

You need to update your copy of the Parser. This behavior was changed about 10 months ago. You can update just the Parser and Value packages that are in pg/lib without updating anything else if you wish.

The reason that the Parser objects put parentheses around themselves during string concatenation operations is so that you can build more complicated formulas from simpler parts. For example:

   $f = Formula("2x+1");
$g = Formula("x/$f");
This will make $g equal to the expected x/(2x+1) rather than (x/2)*x+1, which is what the result would be if objects were concatenated without parentheses (insertion into a string is handle by Perl using string concatenation). When $f is being inserted into a string, there is no way for it to know if that string is going to be used as a Formula or other computation, or just for display purposes, like in BEGIN_TEXT/END_TEXT. Since you usually want the TeX version of $f rather than its string version for BEGIN_TEXT/END_TEXT anyway, the default of using parentheses seemed best, since it is much more obvious that you have parentheses when you don't want them than that they are missing when you do.

It turned out, however, that often the parentheses were not needed, and people were confused about the parens in situations like yours, and so 10 months ago, I changed the defaults for most objects so that they would not insert parentheses unless it was likely that they would be needed. So, for example, Reals don't insert parentheses any more, but complexes do, since the plus in 1+3i has such low precedence. (The one bad effect of this is that when -5 is inserted, the lack of parens can be a problem. For example, $a = Real(-5); $f = Formula("$a^2"); would produce -10 not 10. But this is true of $a = -5; as well)

In terms of using parser objects within BEGIN_TEXT/END_TEXT, I mentioned that you usually want the objects to produce their TeX form rather than their string form (for something like a Real, that is not much different, but if the real were something like 2.4E10, then it would make a difference). To make this easier, the Parser has a Context flag that says how Parser objects should stringify themselves. Using Context()->texStrings; just before BEGIN_TEXT and Context()->normalStrings; just after will make it easier to obtain the desired output.

Given a Parser object, $f (not necessarily a formula, but any Parser object), you can get its TeX form from $f->TeX, and its string form as $f->string. These can be used within BEGIN_TEXT/END_TEXT by using the command escapes \{...\}. For example:

    $f=Formula("sin(x/(x+1))");
BEGIN_TEXT
\(f(x) = \{$f->TeX\}\)
END_TEXT
would get you properly formatted TeX output. It may be easier to use
    $f=Formula("sin(x/(x+1))");
Context()->texStrings;
BEGIN_TEXT
\(f(x) = $f\)
END_TEXT
Context()->normalStrings;
however, especially if there are a number of objects to be inserted.

In any case, updating your copy of the Parser and Value packages should make this work more like what you would expect.

Davide

<| Post or View Comments |>


userDavide P. Cervone - Re: Evaluating a multi-variable function at a point  blueArrow
8/26/2006; 1:55:22 PM (reads: 289, responses: 0)
How can I get the value just as a number without the brackets - (so that it can be used in a num_cmp)?

Mike is right, you don't need to use num_cmp. With Parser objects, you don't need to use the older answer checkers at all, but can just use the object's ->cmp method instead.

If you really want to use num_cmp(), however, you can, simply by passing it $point itself: num_cmp($point) should work fine. (It might require the update from 10 months ago, however.)

Finally, if you really want to turn $point into a perl real number rather than a Parser Real object, you can use $point->value. (For Reals, this gets you the perl real; for complexes, an array of 2 Reals; for points and vectors, an array of coordinates, and so on.)

Davide

<| Post or View Comments |>


userDavide P. Cervone - Re: Evaluating a multi-variable function at a point  blueArrow
8/26/2006; 2:02:02 PM (reads: 284, responses: 0)
The situation is complicated further by the fact that the function is a function of two variables.

These should not be much harder than single-variable functions. For example:

    Context("Numeric")->variables->are(x=>'Real',y=>'Real');
$f = Formula("2x+3y^2");
$fxy = $f->eval(x=>-1, y=>3);
If you want to substitute just for x, use
    $fy = $f->substitute(x=>-1);
so $fy is now a function of just y, namely 2y^2-2.

Davide

<| Post or View Comments |>


userMichal Charemza - Re: Evaluating a multi-variable function at a point  blueArrow
8/29/2006; 12:39:39 PM (reads: 278, responses: 0)
> These should not be much harder than single-variable functions.

I didn't know you had to add the variables to the context.

Thanks Davide, you have been very helpful,

Michal.

<| Post or View Comments |>


userDavide P. Cervone - Re: Evaluating a multi-variable function at a point  blueArrow
8/29/2006; 2:41:23 PM (reads: 272, responses: 0)
I didn't know you had to add the variables to the context.

The Numeric context comes with 'x' defined, but nothing else. (Other contexts have different defaults; e.g. Complex context has z, and Vector context x, y and z). If you are planning to use more or different variables, you need to tell the parser about that somehow. Context()->variables->add(...) and Context()->variables->are(...) are the two ways to do that.

I know that the documentation is pretty sparce, and there are lots of useful things that aren't documented at all, but I've not been able to put the time into it that it really deserves (that's what comes from adding features and fixing bugs rather than writing docs). It's one reason I try to be responsive here in the discussion group, and to provide examples when I can.

Davide

<| Post or View Comments |>