Error (in J) Invalid input: diff recieved .5, which is not valid for 2nd arg - maple

was doing a code in maple for non-linear systems using newtons
I struggled a bit with this so my friend showed me his solution. I did exactly the same and I keep getting this error. I apologize for the long code, but I figured I'd add everything needed. If needed I can shorten it. I am just trying to do 5 iterations of this method, and then this error says "(in J)" and I am not sure what they mean by the 2nd argument
restart;
g := (x, y, z) -> 3*x - cos(y*z) - 1/2;
h := (x, y, z) -> x^2 - 81*(y + 0.1)^2 + sin(z) + 1.06;
i := (x, y, z) -> exp(-y*x) + 20*z + 10/3*Pi - 1;
A := (x, y, z) -> Vector[column](3, [g(x, y, z), h(x, y, z), i(x, y, z)]);
B := (x, y, z) -> Vector[column](3, [x, y, z]);
J := Matrix(3, 3, [[diff(g(x, y, z), x), diff(g(x, y, z), y), diff(g(x, y, z), z)], [diff(h(x, y, z), x), diff(h(x, y, z), y), diff(h(x, y, z), z)], [diff(i(x, y, z), x), diff(i(x, y, z), y), diff(i(x, y, z), z)]]);
J := (x, y, z) -> Matrix(3, 3, [[diff(g(x, y, z), x), diff(g(x, y, z), y), diff(g(x, y, z), z)], [diff(h(x, y, z), x), diff(h(x, y, z), y), diff(h(x, y, z), z)], [diff(i(x, y, z), x), diff(i(x, y, z), y), diff(i(x, y, z), z)]]);
C := (x, y, z) -> LinearAlgebra[MatrixInverse](J(x, y, z));
F := (x, y, z) -> evalf(B(x, y, z) - ((C(x, y, z)) . (A(x, y, z))));
x[0] := 0.5;
y[0] := 0.5;
z[0] := -0.5;
x[1] := F(x[0], y[0], z[0]);
Error, (in J) invalid input: diff received .5, which is not valid for its 2nd argument

You could also use the Jacobian command from the VectorCalculus package, along with purely Vector form. But I'll try to make minimal changes to get your original to behave (eg. I'll leave all the unnecessary operator definitions).
The following works in my Maple 2021.0 if I Copy&Paste in as either 1D plaintext in a Worksheet or 2D Math in a Document.
restart;
g := (x,y,z) -> 3*x - cos(y*z) - 1/2:
h := (x,y,z) -> x^2 - 81*(y + 0.1)^2 + sin(z) + 1.06:
i := (x,y,z) -> exp(-y*x) + 20*z + 10/3*Pi - 1:
A := (x,y,z) -> Vector(3,[g(x,y,z),h(x,y,z),i(x,y,z)]):
B := (x,y,z) -> Vector(3,[x,y,z]):
J:=unapply(Matrix(3,3,
[[diff(g(x,y,z),x),diff(g(x,y,z),y),diff(g(x,y,z),z)],
[diff(h(x,y,z),x),diff(h(x,y,z),y),diff(h(x,y,z),z)],
[diff(i(x,y,z),x),diff(i(x,y,z),y),diff(i(x,y,z),z)]]),
[x,y,z]):
C := (x,y,z) -> LinearAlgebra:-MatrixInverse(J(x,y,z)):
F := (x,y,z) -> evalf(B(x,y,z) - ((C(x,y,z)) . (A(x,y,z)))):
x[0] := 0.5: y[0] := 0.5: z[0] := -0.5:
V[0] := Vector([x[0],y[0],z[0]]);
for ii from 1 to 7 do
V[ii] := F(V[ii-1][1], V[ii-1][2], V[ii-1][3]);
g(V[ii][1], V[ii][2], V[ii][3]),
h(V[ii][1], V[ii][2], V[ii][3]),
i(V[ii][1], V[ii][2], V[ii][3]);
end do;

Related

Coq unable to unify -- how to change hypothesis?

Coq beginner here.
I have the following silly theorems:
Theorem plus_same : forall a b c : nat,
a+b=a+c -> b=c.
Proof. Admitted.
Theorem advanced_commutivity:
forall x y z w : nat, x + y + (z+w) = x + z + (y + w).
Proof.
intros x y z w.
apply (plus_same x (y + (z+w)) (z + (y + w))).
However, when I try to run the apply line, I get an error:
Unable to unify "y + (z + w) = z + (y + w)" with
"x + y + (z + w) = x + z + (y + w)".
Do I need to change my hypothesis here? How can I apply plus_same here to the arguments in advanced_commutivity proof?
You are misreading your goal: x + y + (z + w) stands for (x + y) + (z + w), because + is registered as left-associative, which is different from x + (y + (z + w)).
So in order to apply your lemma, you should first reassociate your + by rewriting with another Lemma plus_assoc : forall x y z, x + y + z = x + (y + z).

Solve for a variable in Coq

Is there a way to solve for a variable in Coq? Given:
From Coq Require Import Reals.Reals.
Definition f_of_x (x : R) : R := x + 1.
Definition f_of_y (y : R) : R := y + 2.
I want to express
Definition x_of_y (y : R) : R :=
as something like solve for x in f_of_x = f_of_y. I expect to use the tactic language to then shuffle terms about. I ultimately want to end up with the correct usable definition of y + 1. I think want to use my definiton:
Compute x_of_y 2. (* This would yield 3 if R was tractable or if I was using nat *)
The alternative is to do it by hand with pencil/paper and then only check my work with Coq. Is this the only way?
If I understand correctly, what you want to express is the existence of a solution to the equation
x + 3 = x + 2
If so you can state it in coq as
Lemma solution :
exists x, x + 3 = x + 2.
If it was something solvable like x + 2 = 2 * x then you could solve it as
Lemma solution :
exists x, x + 2 = 2 * x.
Proof.
exists 2. reflexivity.
Qed.
But then of course there are no solutions to x + 3 = x + 2.
If you want instead a solution, with y fixed to
x + 3 = y + 2
you have to quantify over y:
Lemma solution :
forall y, exists x, x + 1 = y + 2.
Proof.
intro y.
eexists. (* Here I'm saying I want to prove the equality and fill in the x later *)
eapply plus_S_inj.
rewrite plus_0.
reflexivity.
Defined.
Print solution. (* You will see the y + 1 here *)
Here I assume some lemmata that help me manipulate numbers:
Lemma plus_S_inj :
forall x y z t,
x + z = y + t ->
x + (S z) = y + (S t).
Admitted.
Lemma plus_0 :
forall x,
x + 0 = x.
Admitted.
You probably have similar lemmata for your notion of R (I don't know which it is so I cannot go any further.)

Notation for reflexive transitive closure in Coq

Consider the reflexive transitive closure of a relation:
Inductive star {A : Type} (r : A -> A -> Prop) : A -> A -> Prop :=
| star_refl x : star r x x
| star_step x y z : r x y -> star r y z -> star r x z.
How can I give notation in Coq so that I can write x ->* y, perhaps adding a subscript to represent the relation ->__r. This is certainly possible in Isabelle. Is there a clean way of doing it in Coq?
You can indeed use the notation system of Coq for this:
Notation "x '[' R ']*' y" := (star R x y) (at level 20).
Goal
forall A (x y z : A) R,
x [R]* y ->
y [R]* z ->
x [R]* z.
There are other notations that you can try, this an example explicitly mentioning the R.
You can only use this generic notation in combination with a special one for reduction.
Section Terms.
Context (term : Type).
Context (red : term -> term -> Prop).
Notation "x → y" := (red x y) (at level 0).
Notation "x →* y" := (x [red]* y) (at level 19).
Goal forall x y, x → y -> x →* y.
Abort.
End Terms.
Also note that you can do something fancy and use the notation already in the definition.
Reserved Notation "x '[' R ']*' y" (at level 20).
Inductive star {A : Type} (r : A -> A -> Prop) : A -> A -> Prop :=
| star_refl x : x [r]* x
| star_step x y z : r x y -> y [r]* z -> x [r]* z
where "x '[' R ']*' y" := (star R x y).
You can do a lot of things with notations. The following also works.
Notation "x '→<' R '>*' y" := (star R x y) (at level 20).
Goal
forall A (x y z : A) R,
x →<R>* y ->
y →<R>* z ->
x →<R>* z.
Abort.

How to prove x + y - z = x + (y - z) in Coq

I want to prove this :
1 subgoals
x : nat
y : nat
z : nat
______________________________________(1/1)
x + y - z = x + (y - z)
It looks trivial, but it confuse me a lot, and I need it for another proof.
Thanks.
What you're trying to prove doesn't hold if y <= z, because with nat a-b is zero if a <= b.
Omega is a useful tactic to use for inequalities and simple arithmetic over nat.
Require Import Omega.
Theorem foo:
forall x y z:nat, (x = 0 \/ z <= y) <-> x + y - z = x + (y - z).
intros; omega.
Qed.
However, your identity of course holds for the integers Z.
Require Import ZArith.
Open Scope Z.
Theorem fooZ:
forall x y z:Z, x + y - z = x + (y - z).
intros; omega.
Qed.

How can I force maple to perform chain differentiation?

When differentiating functions, it is often not clear to me, in which cases maple performs a chain differentiation and when it does not so.
Let's look at an example:
f := (x, y) -> r(x)*M(y);
g := (x, y) -> h(x, f(x,y));
A := D[2](g);
Then A(a,b) gives just
D[2](g)(a,b)
Question: Why does maple not perform the differentiation by going through the definitions applying the chain rule? And how can I get maple to do so?
Even more puzzling, in this simpler example, maple behaves as i wish:
f := 'f';
g := (x, y) -> h(x, f(x,y));
A := D[2](g);
Then A(a,b) returns
D[2](h)(a, f(a, b))*D[2](f)(a, b)
Maybe this helps to tackle the problem...
Is this useful?
restart:
f := (x, y) -> r(x)*M(y):
g := (x, y) -> h(x, f(x,y)):
#diff(g(x,y),y);
#convert(diff(g(x,y),y),D);
unapply(convert(diff(g(x,y),y),D),[x,y]);
(x, y) -> D[2](h)(x, r(x) M(y)) r(x) D(M)(y)