Force re-evaluation of maxima variable - maxima

This may be super simple but I cannot find a way to get Maxima to tell me that c is now 8, not 3? Can anyone help?
(%i1) a:1;
(%o1) 1
(%i2) b:2;
(%o2) 2
(%i3) c:a+b;
(%o3) 3
(%i4) ''c;
(%o4) 3
(%i5) a:6;
(%o5) 6
(%i6) ''c;
(%o6) 3
Many thanks
Tom

The easiest way is to define include a prevent evaluation (') operator in the definition ofc. For example:
(%i1) c : '(a+b);
(%o1) b + a
(%i2) a:1;
(%o2) 1
(%i3) b:2;
(%o3) 2
(%i4) ''c;
(%o4) 3
(%i5) a:6;
(%o5) 6
(%i6) ''c;
(%o6) 8
Note that you can also post-fix the values of a and b:
(%i7) c, a:11, b:5;
(%o7) 16

Related

Interacting in agda-mode with agda?

It feels super awkward to interact with agda.
Consider the proof state:
_ = begin
5 ∸ 3
≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ { 2 <cursor-goes-here> }0
When I type C-c C-l (type-check), it says
?0 : 2 ∸ 0 ≡ _y_131
_y_131 : ℕ [ at /home/bollu/work/plfa/src/plfa/part1/Naturals.lagda.md:586,5-10 ]
which doesn't seem like a great error? Nor does a refine (C-c C-r) give me a good error message: It only tells me:
cannot refine
How do I get adga to tell me:
You've finished the proof, except for a missing \qed
In general, what is the "preferred mode of interaction" when building proofs?
The overall issue
Your post starts by the following assumption:
It feels super awkward to interact with agda.
The reason that could explain your feeling is that you seem to assume that Agda can both infer a term and its type, in other words, both the property you wish to prove and a proof of it. Agda can often do one of these, but asking for both does not make much sense. As a comparison, imagine being on a bench in a park, when a complete strangers comes and sits next to you, saying nothing. You can see he would very much enjoy to ask you something, but, despite your efforts at making him speak, he remains silent. After a few minutes, the stranger yells at you that, despite him being thirsty, you did not bring the drink he was expected. In this metaphor, the stranger is you, and you are Agda. There is no way you could have known he was thirsty, and even less bring him his drink.
Concretely
You gave the following piece of code:
_ = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ { 2 <cursor-goes-here> }0
This piece of code lacks a type signature which will allow Agda to help you more. Agda tells you so when you type check by providing you with the inferred type of the goal:
?0 : 2 ∸ 0 ≡ _y_131
_y_131 : ℕ [ at /home/bollu/work/plfa/src/plfa/part1/Naturals.lagda.md:586,5-10 ]
Here Agda says that your proof goal is that 2 ∸ 0 is equal to some unknown natural number y. This number being unknown there is very little chance Agda can help you go further in your proof effort because it does not even know what you wish to prove. As far as it knows, your goal could turn out to be 5 ∸ 3 ≡ 3 for wish there exists no proof term.
Getting back to our metaphor, you lacks the statement "I am thirsty". Should the stranger provide this piece of information, you could - possibly - react, which means Agda can try and help.
The solution
I'm assuming you wish to prove that the result of your subtraction is two, in which case the code is as follows:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ {!!}
In this case, you can interact with Agda in various ways, which all lead to Agda providing you with a sound proof term:
You can call Agsy to solve the problem for you (CTRL-c CTRL-a), which leads to:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ refl
You can try and refine the goal directly (CTRL-c CTRL-r), asking Agda if there exists any unique constructor which has the right type, which leads to the same:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ refl
If you wish to wrap up your proof using \qed you can try and input _∎ into the hole after which refining (CTRL-c CTRL-r) gives:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ {!!} ∎
Calling Agsy in the resulting goal naturally gives:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ 2 ∎

wxMaxima ezunits funny business

Is the handling of the units broken or what am I missing?
load(ezunits);
σ_N: 10000`N/(50`mm*10`mm);
newts: 123`kg*m/s^3; newts `` N; newts + 321 `kg*m/s^2;
produces not what one would have hoped for:
(%i1) load(ezunits);
(%o1) "C:/maxima-5.43.2/share/maxima/5.43.2/share/ezunits/ezunits.mac"
(%i2) σ_N: 10000`N/(50`mm*10`mm);
(σ_N) 10000 ` (N/500 ` 1/mm^2)
(%i5) newts: 123`kg*m/s^3; newts `` N; newts + 321 `kg*m/s^2;
(newts) 123 ` (kg*m)/s^3
(%o4) 123/s ` N
(%o5) 321 ` (kg*m)/s^2+123 ` (kg*m)/s^3
Should be:
σ_N= 20 N/mm^2
newts= 123 N/s
For the first part, you have to use parentheses to indicate the grouping you want. When you write a ` b/c, it is interpreted as a ` (b/c), but in this case you want (a ` b)/c. (Grouping works that way because it's assumed that stuff like x ` m/s is more common than (x ` m)/s.)
(%i2) σ_N: (10000`N)/(50`mm*10`mm);
N
(%o2) 20 ` ---
2
mm
Just for fun, let's check the dimensions of this quantity. I guess it should be force/area.
(%i3) dimensions (%);
mass
(%o3) ------------
2
length time
(%i4) dimensions (N);
length mass
(%o4) -----------
2
time
(%i5) dimensions (mm);
(%o5) length
Looks right to me.
For the second part, I don't understand what you're trying to so. The variable newts has units equivalent to N/s, so I don't understand why you're trying to convert it to N, and I don't understand why you're trying to add N/s to N. Anyway here's what I can make of it.
(%i6) newts: 123`kg*m/s^3;
kg m
(%o6) 123 ` ----
3
s
(%i7) newts `` N/s;
N
(%o7) 123 ` -
s
When quantities with different dimensions are added, ezunits just lets it stand; it doesn't produce an error or anything.
(%i8) newts + 321 ` kg*m/s^2;
kg m kg m
(%o8) 321 ` ---- + 123 ` ----
2 3
s s
The motivation for that is that it allows for stuff like 3`sheep + 2`horse or x`hour + y`dollar-- the conversion rate can be determined after the fact. In general, allowing for expressions to be reinterpreted after the fact is, I believe, the mathematical attitude.

Not able to apply guard expression in query list comprehension

I am trying a query list comprehension:
> (set xs '(1 2 3 4 5 6 7 8))
> (lc ((<- x xs) (when (> x 5))) x)
But I am getting the error exception error: undefined function when/1.
Is it possible to apply guard statements to lc?
According to the LFE User Guide, within a list comprehension qualifier, the guard must precede the list expression:
(<- pat {{guard}} list-expr)
This means your example should be written as follows:
lfe> (set xs '(1 2 3 4 5 6 7 8))
(1 2 3 4 5 6 7 8)
lfe> (lc ((<- x (when (> x 5)) xs)) x)
(6 7 8)
You could alternatively treat the greater-than expression as a normal boolean expression qualifier:
lfe> (lc ((<- x xs) (> x 5)) x)
(6 7 8)

Element Wise Operations in ojAlgo

How does one perform element wise operations (specifically multiplication) on a matrix in ojAlgo ?
Supposed that I have a storeA and storeB and would like to perform a binary operation f on each of the elements of A and B as the input respectively and obtain the output storeC.
Example
[1 2 3]
[4 5 6] -- A
[1 3 9]
[2 4 6] -- B
f - multiply
[1 6 27]
[8 20 36] -- C
What are the approaches that I can take ?

Infix to postfix for negative numbers

How do I convert negative numbers from infix to postfix ?
Suppose I have a expression
a = - b - (-c-d)
In some places I read that you can parathesize the negative numbers like
a = (-b) - (-c-d)
But here if I do that, I will get a term like "ab-" at the beginning of the postfix expression which means a-b and is incorrect.
How do I convert this ?
In infix notation, you must distinguish between the binary subtraction operator sub and the unary negation operator neg. Both are represented by a minus sign, but the context tells you which is which.
You've got a negation, when the minus as at the beginning of the expression, or after an opening parenthesis or after a binary operator:
    − (x + y)   →   x y add neg
    4 × − x   →   4 x neg mult
    2 × (− x + y)   →   2 x neg y add mult
You've got a subtraction when the minus is after a closing parenthesis or after a symbol, i.e. after a variable or number:
    1 − x   →   1 x sub
    (4 ∗ x) − 1   →   4 x mult 1 sub
Take care that the unary operator neg just takes one argument off the stack. If you want to stick with binary operators, you can push a zero before the second operand and use binary sub:
    − (x + y)   →   0 x y add sub
    4 x neg mult   →  4 0 x sub mult
    2 x neg y add mult   →   2 0 x sub y add mult
Finally, you can apply a similar logic to unary plus, which you can just ignore:
    + x   →   x
    + (x + y)   →   x y add

Resources