let maxima display an exponentiation as a function instead of a caret - cas

maxima accepts both a^b and a**b as input for exponentiation, and will always output the exponent with caret ^.
Is it also possible to get the output as a function, like pow(a,b)?

OK, as you said, you want to output Math.pow(a,b) for Javascript. The approach I'll suggest here is to replace a^b expressions in Maxima with Math.pow(a,b) expressions and output that.
(%i1) e : sqrt(a) + b^(3/2) + 1/c + exp(d^f);
f
d 1 3/2
(%o1) %e + - + b + sqrt(a)
c
(%i2) subst ("^"=lambda([a, b], Math.pow(a, b)), e);
3 1
(%o2) Math . pow(c, - 1) + Math . pow(b, -) + Math . pow(a, -)
2 2
+ Math . pow(%e, Math . pow(d, f))
OK, so that's most of the work there. Some expressions are represented as "^" expressions even if they appear to be something else, for example, sqrt(a) is a^(1/2) and 1/c is c^(-1). If you need for those to be preserved as sqrt(a) and 1/c then we'll have to work on that.
I'm guessing it's best to have floating point values instead of integer ratios. Also, we'll replace %e by its numerical value. If you want %e^x to be rendered as Math.exp(x), we can work on that. Or if you want Math.pow(Math.E, x), that's relatively simple; just evaluate subst(%e = Math.E, <your expression>).
(%i3) float (%);
(%o3) Math . pow(c, - 1.0) + Math . pow(b, 1.5) + Math . pow(a, 0.5)
+ Math . pow(2.718281828459045, Math . pow(d, f))
Maxima considers x . y to mean noncommutative multiplication, but that doesn't come into play here so that's fine. By default it is displayed with a space on either side of the dot, but if you're willing to do a tiny amount of Lisp hacking we can remove the space. (I guess it doesn't matter to Javascript, right? Math . pow is equivalent to Math.pow, isn't it?)
(%i4) :lisp (setf (get 'mnctimes 'dissym) '(#\.))
(.)
(%i4) %o3;
(%o4) Math.pow(c, - 1.0) + Math.pow(b, 1.5) + Math.pow(a, 0.5)
+ Math.pow(2.718281828459045, Math.pow(d, f))
OK, now we can output the expression.
(%i5) grind (%o3);
Math.pow(c,-1.0)+Math.pow(b,1.5)+Math.pow(a,0.5)
+Math.pow(2.718281828459045,Math.pow(d,f))$
(%o5) done
Is that the expected output?

OP asked about converting %e^x to exp(x). That's easy to do, but to make it stick, we have to disable simplification, i.e. the application of identities which Maxima uses to find a general representation of an expression. By default Maxima simplifies exp(x) to %e^x. We can replace %e^x by exp(x) but we need to disable simplification to prevent it from going back again.
(%i1) simp:false $
(%i2) matchdeclare (xx, all) $
(%i3) defrule (to_exp, %e^xx, Math.exp(xx));
xx
(%o3) to_exp : %e -> Math . exp(xx)
(%i4) apply1 (1 + %e^(x + %e^y), to_exp);
(%o4) 1 + Math . exp(x + Math . exp(y))
Probably you only want to disable simplification (i.e. simp:false) when you are ready to output the expression. But I can imagine situations in which you would have it disabled, e.g. if it is important to output the expression exactly the way it was entered, e.g. x + x instead of 2*x.
I've used a different mechanism to do the replacement here, namely defrule which defines a pattern matching rule. Pattern matching is very useful, and I encourage you to take a look at defrule and matchdeclare in the Maxima documentation.

Related

Terms of an equation in different order

I'm using wxMaxima 15.08.1 (win 10) and when I input this equation
/* [wxMaxima: input start ] */a*x+b*y+c*z=0;
I get this:
/* [wxMaxima: input end ] */cz+by+ax=0
Why does it change the term's position of the expression? It seems like in descending order somehow.
Then, if I type another equation giving all coefficients the same unknown, maxima outputs it just right.
/* [wxMaxima: input start ] */a*x^2+b*x+c=0;
/* [wxMaxima: input end ] */ax^2+bx+x=0
Maxima has its own idea of the canonical ordering of terms in "+" and "*" expressions. The canonical ordering is expressed by the function ordergreatp (equivalently orderlessp) which tells if one term comes after (respectively, before) another term. If you apply sort to a list of terms, they are sorted, by default, according to the canonical order.
By default, "+" terms are displayed in reverse order (reverse of the canonical order). When the global variable powerdisp is true, "+" terms are displayed in the canonical order. You can decide whether one order or the other works better for you.
(%i2) powerdisp;
(%o2) false
(%i3) a*x + b*y + c*z;
(%o3) c z + b y + a x
(%i4) a*x^2 + b*x + c;
2
(%o4) a x + b x + c
(%i7) powerdisp : true $
(%i8) a*x + b*y + c*z;
(%o8) a x + b y + c z
(%i9) a*x^2 + b*x + c;
2
(%o9) c + b x + a x

Operator overloading in Isabelle

I want to use the nat type in Isabelle but I want to overload some existing definitions like for example addition. I wrote the following code:
theory Prueba
imports Main HOL
begin
primrec suma::"nat ⇒ nat ⇒ nat" where
"suma 0 n = 0" |
"suma (Suc x) n = 0"
no_notation suma (infix "+" 65)
value "2 + (1 :: nat)"
I tried to overload addition with a new definition that always outputs 0. However when I evaluate 2 + (1 :: nat) I get "Suc (Suc (Suc 0))" :: "nat", which means Isabelle is still using the plus definition from Nat. How can I get it to use my new definition of +?
Thank you
Your must use no_notation to remove the default plus-syntax which comes from the plus type class of the Groups theory.
no_notation Groups.plus_class.plus (infixl "+" 65)
Then you can use
notation suma (infixl "+" 65)
to add your own syntax.
(I have never tried to override such basic parts of the definitions. I guess it might lead to strange situations – especially for other people trying to work with your theory afterwards.)

Comparing two functions based on Asymptotic notations

f(n)= 1 + 2 + 3 + · · + n
g(n) = 3(n^2) + nlogn
Determining f = O(g) or
f = Ω(g) or f = Θ(g)
.As per my effort and understanding one guess It might be f=O(g) as g(n) has a n^2 power which grows faster than n .
Another way : if divided both by n , f(n) will have a constant 1 and g(n) : nlogn which grows faster than constant 1 . so , f=O(g) .
Is that a correct answer?
What actually is scaling property of Big-O ?
How to prove : For any constant c > 0, cf(n) is O(f(n)).
Understanding so far :
cf(n) < (c + k)f(n) holds for all n > 0 and k > 0.
i. Constant factors are ignored.
ii. Only the powers and functions of n should be exploited
It is this ignoring of constant factors that motivates for such a
notation. Which proves f is O(f).
Is this explanation enough to prove that scaling property of Big-O ?
f(n)=O(g(n)) if there is a positive constant c such as.
|f(n)| <= c*|g(n)| for all n>=n(initial)
and since f(n)=(n(n-1))/2 ----> (n^2)
n^2<= n^2 + nlogn (ignore the constants), for all n>=1 then yes your answer is right.

Any way to "visualize" a thunk/function? Or how to view a function for a general argument

I'm not totally sure how to ask this, but is there a way to show the structure of a thunk?
For example
f x = x + 2
g x = 3 x
compo x = f (g x)
ans = compo 5
-- result: (3 * 5) + 2 = 17
Is there any way I could "see" the thunk for ans? As in, I could see the process of the beta reduction for compo or like the "general" form.
I would like to see, for example:
compo n
--> (3 * n) + 2
As in, if I had a function compo x, I would like to view that it is decomposed to (3*n)+2.
For example, in Mathematica:
f[x_] := x+2;
g[x_] := 3*x;
compo[x_] := f[g[x]];
compo[n]
(%
--> (3 * n) + 2
%)
There is the ghc-vis package on hackage which show a visualization of your heap and of unevaluated thunks.
See the package on hackage or the Homepage (which contains rather impressive examples).
If you just want to see the sequence of reductions, you could try using the GHCi interactive debugger. (It's in the GHC manual somewhere.) It's not nearly as easy as your typical IDE debugger, but it more or less works...
In general (we are talking about Haskell code) I think it has no sense, the final thunk stream will be different for different input data and, on the other hand, functions are expanded partially (functions are not only simple expressions).
Anyway, you can simulate it (but ugly)
Prelude> :set -XQuasiQuotes
Prelude> :set -XTemplateHaskell
Prelude> import Language.Haskell.TH
Prelude> import Language.Haskell.TH.Quote
Prelude> runQ [| $([|\x -> 3 * x|]) . $([|\y -> y + 2|]) |]
InfixE (Just (LamE [VarP x_0] (InfixE (Just (LitE (IntegerL 3))) (VarE GHC.Num.*) (Just (VarE x_0))))) (VarE GHC.Base..) (Just (LamE [VarP y_1] (InfixE (Just (VarE y_1)) (VarE GHC.Num.+) (Just (LitE (IntegerL 2))))))

Mixing addition and subtraction with logical NOT

I found some exercises where you combine n-bit 2's complement values in different ways and simplify the output where possible. (Their practice exercises use 16-bit, but that's irrelevant).
Eg:
!(!x&!y) == x|y
0 & y, negate the output == -1
I'm having no problem applying De Morgan's laws with the examples using AND, OR, and NOT but I am having difficulty using NOT with + and -
Eg:
!(!x+y) == x-y
!(y-1) == -y
How does NOT distribute?
Edit: responding to comments: I realize this is a bitwise NOT. My question is: in algebraic terms, how does it distribute as per algebra? Example on Wikipedia
With 2's complement numbers when you bitwise NOT them it is the same as saying the negative of the number minus 1, so !x is equivalent to -x - 1 where x can be a single variable or an expression.
Starting with !(!x+y), well !x is going to be -x - 1 so then it is !(-x - 1 + y) which becomes -(-x - 1 + y) - 1 which simplifies to x - y.
And for !(y-1), that becomes -(y - 1) - 1 = -y + 1 - 1 = -y.