From: Stavros M. <mac...@al...> - 2022-07-01 20:36:30
|
Let's look at a simpler example. Consider: f(x):= block([n],if n=0 then 1 else x); In Maxima, if a variable *n* is not assigned a value, it evaluates to itself (the symbol *n*). The "=" operation checks for structural equality, not mathematical equality. The symbol *n* is not equal to the number *0*, so the predicate is false, and thus your function returns *x*. So you should use *equal*: f(x):= block([n],if equal(n,0) then 1 else x); Until *n* is defined, the test *equal(n,0) *is undefined, so an unevaluated conditional is returned. -s On Fri, Jul 1, 2022 at 3:39 PM Me Self <vla...@gm...> wrote: > Hello everyone > > Is it possible to return a function with an if, from a block()? In a > very simple example: > > (%i1) f(x) := block([y, r, n], r:map(rhs,allroots(x)), y:if n=0 then > 1.618 else x*n)$ > (%i2) f(x+1); > (%o2) -1.0*n > (%i3) g(n) := ''%$ > (%i4) g([0, 1, 2]); > (%o4) [0, -1.0, -2.0] > > f(x) takes a function (polynomial here, for simplicity) as an argument > (see %i2) which is used, internally, to generate some numbers (a bogus > allroots() here), and I need it to return a function of n, where n > will take discrete values. As it is now, it simply evaluates n to be > false (n is just n, not 0) so it spits out the rest of the > conditional. What I want it to return is this (based off on the rest > of the example code above): > > (%i2) f(x+1); > (%o2) if n=0 then 1.618 else -1*n > > I don't want to use makelist() as that requires a numerical 4th > argument, which would mean f(x) will need an extra argument for f(x) > -- but only one is needed. > > I have tried declaring y like this inside the block(): y:'(if n=0 then > 1.618 else n*r), which returns: > > (%o2) if n=0 then 1.618 else n*r > > but when evaluated: > > (%o4) [0, r, 2*r] > > So, how can I make the block() return this: > > (%o2) f(x+1); > (%o2) if n=0 then 1.618 else n*r > > but which, when evaluated it returns: > > (%i3) g(n) := ''%$ > (%o4) g([0, 1, 2]); > (%o4) [1.618, -1.0, -2.0] > > > Regards, > Vlad > > > _______________________________________________ > Maxima-discuss mailing list > Max...@li... > https://lists.sourceforge.net/lists/listinfo/maxima-discuss > |