Isabelle: degree of polynomial multiplied with constant - polynomial-math

I am working with the library HOL/Library/Polynomial.thy.
A simple property didn't work. E.g., the degree of 2x *2 is equal to the degree of 2x-
How can I prove the lemmas (i.e., remove "sorry"):
lemma mylemma:
fixes y :: "('a::comm_ring_1 poly)" and x :: "('a::comm_ring_1)"
shows "1 = 1" (* dummy *)
proof-
have "⋀ x. degree [: x :] = 0" by simp
from this have "⋀ x y. degree (y * [: x :] ) = degree y" sorry
(* different notation: *)
from this have "⋀ x y. degree (y * (CONST pCons x 0)) = degree y" sorry
.
From Manuel's answer, the solution I was looking for:
have 1: "⋀ x. degree [: x :] = 0" by simp
{
fix y :: "('a::comm_ring_1 poly)" and x :: "('a::comm_ring_1)"
from 1 have "degree (y * [: x :]) ≤ degree y"
by (metis Nat.add_0_right degree_mult_le)
}

There are a number of issues here.
First of all, the statement you are trying to show simply does not hold for all x. If x = 0 and y is nonconstant, e.g. y = [:0,1:], you have
degree (y * [: x :]) = degree 0 = 0 ≠ 1 = degree y
The obvious way to fix this is to assume x ≠ 0.
However, this is not sufficient either, since you only assumed 'a to be a commutative ring. However, in a commutative ring, in general, you can have zero divisors. Consider the commutative ring ℤ/4ℤ. Let x = 2 and y = [:0,2:].
Then y * [:x:] = [:0,4:], but 4 = 0 in ℤ/4ℤ. Therefore y * [:x:] = 0, and therefore, again,
degree (y * [: x :]) = degree 0 = 0 ≠ 1 = degree y
So, what you really need is one of the following two:
the assumption x ≠ 0 and 'a::idom instead of 'a::comm_ring. idom stands for “integral domain” and, that is simply a commutative ring with a 1 and without zero divisors
more generally, the assumption that x is not a zero divisor
even more generally, the assumption that x * y ≠ 0 or, equivalently, x times the leading coefficient of y is not 0
Also, the usage of ⋀ in Isar proofs is somewhat problematic at times. The “proper“ Isar way of doing this would be:
fix x :: "'a::idom" and y :: "'a poly"
assume "x ≠ 0"
hence "degree (y * [:x:]) = degree y" by simp
The relevant lemmas are degree_mult_eq and degree_smult_eq, you will see that they require the coefficient type to be an idom. This works for the first case I described above, the other two will require some more manual reasoning, I think.
EDIT: just a small hint: you can find theorems like this by typing
find_theorems "degree (_ * _)"
If you try to apply the degree_mult_eq it shows to your situation (with comm_ring), you will find that it fails, even though the terms seem to match. If that is the case, it is usually a type issue, so you can write something like
from [[show_sorts]] degree_mult_eq
to see what the types and sorts required by the lemma are, and it says idom.

Related

How to resolve an equation in dart (from a string)

I'm trying to resolve equations in dart. Here an example of an equation: y = x * 16. The y variable is a known value and x the value I want to compute. For example, if y = 32, I want to resolve the equation 32 = x * 16 (so 2).
I can't transform the equations manually because they come from an external source. Some examples : y = x * 16, y = (x + 35) / 1500, eventually y = (x + 14) - (x - 5). The left operand is always y (a known value). I can eventually simplify the right operand manually.
I tried to use the math_expressions and equations packages, but without success.

Error: Cannot interpret this number as a value of type nat

My current goal is:
x - 1 + 1 = x
I tried to use rewrite -> (Nat.add_comm (-1) 1). to change the current goal to x + 1 - 1, but it gave me the error Error: Cannot interpret this number as a value of type nat. How can I solve this question?
Assuming x is indeed a natural number, I believe your goal is false. Note that subtraction on the natural numbers is truncated. Thus, if x = 0, what we have is
0 - 1 + 1 = (0 - 1) + 1 = 0 + 1 = 1 != 0
where the parenthesis I added are already there, I just made them explicit (*).
The error you get makes perfect sense. -1 is not a natural number, and hence Coq can't interpret it as a natural number.
(*) You can do this with Set Printing Parentheses.
Edit: If you are able to prove that 1 <= x in your context, you can use
Nat.sub_add: forall n m : nat, n <= m -> m - n + n = m
Nat.add_sub_swap: forall n m p : nat, p <= n -> n + m - p = n - p + m
I found these results by importing Arith and searching like so:
Search (_ - _ + _).

initial point in CORDIC algorithm

I am trying to reduce number of iterations required to calculate multiplication using the CORDIC algorithm because I am using this algorithm in a continuous function to calculate square function. Here is the algorithm assuming -1<x<1'
function z=square(x)
y=x;
z=0;
for i=1:15
if (x > 0)
x = x - 2^(-i);
z = z + y*2^(-i);
else
x = x + 2^(-i);
z = z - y*2^(-i);
end
end
return
end
I already know the close value to multiplication result (from the previous result (call it pr)) and value of x (the value of x is continuous) . Does it help in anyway to decrease number of iterations?
If you are multiplying twice by the same constant, say a.x and a.x', then you can multiply and add with the delta a.(x' - x), which has less significant digits.
In case both factors vary, you can still use
x'.y' = (x'- x).(y' - y) + x.(y' - y) + (x' - x).y + x.y
where maybe the first term is neglectible.
For a square,
x'² = (x'- x)² + 2.x.(x' - x) + x²

Pumping Lemma problems for determining Regular Language and CFL

{a^p b^p; p is a prime number}
{a^p b^p; p is a prime number, m is a fixed number and m≥p≥0}
How do I prove if this is a regular language/context free language (or not)?
1) L = {a^n b^n; n is a prime number} :
So the prove can be done by contradiction. Suppose L is regular, and p is the pumping length.
The test string is w = a^p b^p, w belongs to L, and |w| = 2p >= p
We subdivide w=xyz. There are 3 conditions to prove the pumping lemma:
from the third condition, |xy| < p, so xy contains only a's
from the second condition, |y| > 0, so y has the form y = a^k, where 1 <= k <= p
from the first condition, xy^iz belongs to L for i = 0, 1, 2, ... So if you pump down (i = 0) you got:
w = a^(p - k) b^p , and w does not belongs to L (Because the quantity of a's and b's are different)
So you prove that L is not regular.

How to perform Modulo greatest common divisor?

Suppose that gcd(e,m) = g. Find integer d such that (e x d) = g mod m
Where m and e are greater than or equal to 1.
The following problem seems to be solvable algebraically but I've tried doing it and it give me an integer number. Sometimes, the solution for d is an integer and sometimes it isn't. How can I approach this problem?
d can be computed with the extended euklidean algorithm, see e.g. here:
https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
The a,b on that page are your e,m, and your d will be the x.
Perhaps you are assuming that both e and m are integers, but the problem allows them to be non-integers? There is only one case that gives an integer solution when both e and m are integers.
Why strictly integer output is not a reasonable outcome if e != m:
When you look at a fraction like 3/7 say, and refer to its denominator as the numerator's "divisor", this is a loose sense of the word from a classical math-y perspective. When you talk about the gcd (greatest common divisor), the "d" refers to an integer that divides the numerator (an integer) evenly, resulting in another integer: 4 is a divisor of 8, because 8/4 = 2 and 2 is an integer. A computer science or discrete mathematics perspective might frame a divisor as a number d that for a given number a gives 0 when we take a % d (a mod d for discrete math). Can you see that the absolute value of a divisor can't exceed the absolute value of the numerator? If it did, you would get pieces of pie, instead of whole pies - example:
4 % a = 0 for a in Z (Z being the set of integers) while |a| <= 4 (in math-y notation, that set is: {a ∈ Z : |a| <= 4}), but
4 % a != 0 for a in Z while |a| > 4 (math-y: {a ∈ Z : |a| > 4}
), because when we divide 4 by stuff bigger than it, like 5, we get fractions (i.e. |4/a| < 1 when |a| > 4). Don't worry too much about the absolute value stuff if it throws you off - it is there to account for working with negative numbers since they are integers as well.
So, even the "greatest" of divisors for any given integer will be smaller than the integer. Otherwise it's not a divisor (see above, or Wikipedia on divisors).
Look at gcd(e, m) = g:
By the definition of % (mod for math people), for any two numbers number1 and number2, number1 % number2 never makes number1 bigger: number1 % number2 <= number1.
So substitute: (e * d) = g % m --> (e * d) <= g
By the paragraphs above and definition of gcd being a divisor of both e and m: g <= e, m.
To make (e * d) <= g such that d, g are both integers, knowing that g <= e since g is a divisor of e, we have to make the left side smaller to match g. You can only make an integer smaller with multiplcation if the other multipland is 0 or a fraction. The problem specifies that d is an integer, so we one case that works - the d = 0 case - and infinitely many that give a contradiction - contradiction that e, m, and d all be integers.
If e == m:
This is the d = 0 case:
If e == m, then gcd(e, m) = e = m - example: greatest common divisor of 3 and 3 is 3
Then (e * d) = g % m is (e * d) = m % m and m % m = 0 so (e * d) = 0 implying d = 0
How to code a function that will find d when either of e or m might be NON-integer:
A lot of divisor problems are done iteratively, like "find the gcd" or "find a prime number". That works in part because those problems deal strictly with integers, which are countable. With this problem, we need to allow e or m to be non-integer in order to have a solution for cases other than e = m. The set of rational numbers is NOT countable, however, so an iterative solution would eventually make your program crash. With this problem, you really just want a formula, and possibly some cases. You might set it up like this:
If e == m
return 0 # since (e * d) = m % m -> d = 0
Else
return g / e
Lastly:
Another thing that might be useful depending on what you do with this problem is the fact that the right-hand-side is always either g or 0, because g <= m since g is a divisor of m (see all the stuff above). In the cases where g < m, g % m = g. In the case where g == m, g % m = 0.
The #asp answer with the link to the Wikipedia page on the Euclidean Algorithm is good.
The #aidenhjj comment about trying the math-specific version of StackOverflow is good.
In case this is for a math class and you aren't used to coding: <=, >=, ==, and != are computer speak for ≤, ≥, "are equal", and "not equal" respectively.
Good luck.