Domanda

With Maxima, it is possible to replace an unknown by a value using at() statement. But this use a list, for the substitution, and the solve() statement don't return a list.

Code:

(%i1) g(x):=x^2+a;
                                         2
(%o1)                           g(x) := x  + a
(%i2) g(x),solve(x=3),a=2;
(%o2)                                11

I managed to compute a result using commas, but I can't create a function to do so:

(%i3) f(y) := g(x),solve(x=3),a=y;
(%o3)                          f(y) := g(x)
(%i4) f(2);
                                2
(%o4)                          x  + a

Is there a statement for which the commas acts like it acts directly in the line?


Edit:
Actually, it is possible to use at() with solve() to create the function f(), as solve() just return a list of lists. So the code would be:

(%i5) f(y) := at(at(g(x), solve(x=3)[1]), a=y);
(%o5)             f(y) := at(at(g(x), solve(x = 3) ), a = y)
(%i6) f(2);
(%o6)                                 11

Notice the [1] after solve(x=3) in the (%i5). It select the the first item (solution) of list.

È stato utile?

Soluzione 2

Perhaps I'm answering the wrong question. Maybe what you want is ev(foo, bar, baz) -- ev is the function that is actually called when you write foo, bar, baz at the console input prompt. So the function would be written f(y) := ev (g(x), solve(x=3), a=y).

However, bear in mind that there are several different kinds of functionality built into ev, so it is hard to understand (see the documentation for ev). Instead, consider using subst which is much simpler.

Altri suggerimenti

I'm not sure what you are trying to accomplish -- probably it would be best if you would back up a couple of steps and describe the larger problem you are trying to solve here.

My best guess as to what you want is that you are trying to use the result of 'solve' to find a value to substitute into some expression. If so you can achieve it like this: f(eq, u) := map (lambda ([e], subst (e, g(u))), solve (eq, x)); where eq is an equation to solve for x and then substitute into g(u). Note that 'solve' can return multiple solutions so that's why I use 'map' to apply something to each solution. Here is an example output:

(%i7) f(eq) := map (lambda ([e], subst (e, g(x))), solve (eq, x));
(%o7)       f(eq) := map(lambda([e], subst(e, g(x))), solve(eq, x))
(%i8) solve (x^2 + 2*x + 2);
(%o8)                     [x = - %i - 1, x = %i - 1]
(%i9) f (x^2 + 2*x + 2);
(%o9)                      [g(- %i - 1), g(%i - 1)]

Of course you can define 'g' in whatever way is appropriate.

The answer to your specific question (which I believe is not actually very much relevant, but anyway) is to use 'block' to group together expressions to be evaluated. E.g. f(x) := block (...);

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top