How do you factor over a Z field? - matlab

I have to factorize a polynomial e.g.
over the field of Z5 using Matlab or Mupad.
And i tried everything read a lot of Matlab and Mupad documentation and still can't find it, so i am guessing it is the math i don't know that's going to help me factor it.

Don't kill a mosquito with a cannon!
You only need to find a root between 0, 1, 2, -2, -1.
Also, given that x5 = x, the problem reduces to finding x such that
2x + 2x^4 + x^3 + 2x^2 - 3 = 0
and since x ≠ 0, x^4 = 1 hence
2x + x^3 + 2x^2 - 1 = 0
Well, let's try!
1: 2 + 1 + 2 - 1 -> -1
2: -1 + 3 - 2 - 1 -> -1
-2: 1 - 3 + 3 - 1 -> 0 -> root!
Then the polynomial is divisible by (x - 3), and you can repeat the procedure with the quotient until there are no roots left.
Addendum
After dividing by (x - 3) we get
x4 + x2 + 1
which we can expressed as
(x2 + 1)2 - x2
or
((x2 + 1) - x)((x2 + 1) + x)
To find the factors of degree 2 programmatically, just try with x2 + ax + b for a and b between 0 and 4.

I found a mupad command to do what i needed.
Still thanks for exaplaining the math behind it.

Related

The roots of the characteristic polynomial and the eigenvalues are not the same

This is matrix B
B = [1 2 0 ; 2 4 6 ; 0 6 5]
The result of eig(B) is:
{-2.2240, 1.5109, 10.7131}
and the characteristic polynomial of B by this link is
syms x
polyB = charpoly(B,x)
x^3 - 10*x^2 - 11*x + 36
but the answer of solve(polyB) is
133/(9*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) + ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3) + 10/3
(3^(1/2)*(133/(9*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) - ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3))*i)/2 - 133/(18*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) - ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3)/2 + 10/3
10/3 - 133/(18*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) - ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3)/2 - (3^(1/2)*(133/(9*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) - ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3))*i)/2
which I don't know what it is while I expect it to be the eigenvalues of B. What is the problem?
I do not understand why you add x and symbolic maths, they are not required for your task.
B = [1 2 0 ; 2 4 6 ; 0 6 5]
cp=charpoly(B)
eig2=roots(cp)
returns:
eig2 =
10.7131
-2.2240
1.5109
However, if for some reason you insist in using symbolic (which you should not for a numerical task), you can do
double(solve(polyB))
ans =
10.7131 + 0.0000i
-2.2240 - 0.0000i
1.5109 - 0.0000i
(note imaginary parts is zero)
Since I do not have MATLAB in this machine, I will use SymPy instead:
>>> from sympy import *
>>> B = Matrix([[1, 2, 0],
[2, 4, 6],
[0, 6, 5]])
Computing the characteristic polynomial and its roots:
>>> s = Symbol('s')
>>> p = (s*eye(3) - B).det()
>>> p
s**3 - 10*s**2 - 11*s + 36
>>> roots = solve(p,s)
Computing floating-point approximations of the three roots:
>>> [ r.evalf() for r in roots ]
[1.51092975992931 - 0.e-22*I, -2.22404024437578 + 0.e-22*I, 10.7131104844465 - 0.e-20*I]
Since B is symmetric, its eigenvalues must be real. Note that the imaginary parts of the floating-point approximations of the roots are indeed equal to zero.
Printing in LaTeX, the exact values of the roots are:
Note that some roots are "longer" than others, i.e., they require more symbols. However, they are exact. Using floating-point arithmetic, all roots have the same "size", but they are approximations.

Dividing two polynomials in MATLAB [duplicate]

This question already has answers here:
Divide two polynomials using MATLAB
(2 answers)
Closed 6 years ago.
I'm currently working on a Cyclic Code program for a class in MATLAB and I'm trying to figure out the best way to divide two polynomials, the generator P(X) and the user input, shifted by 3 (x^3 * D(X)) in order to get the Quotient Q(X) and Remainder C(X), which would allow me to get the transmitted data T(X) = X^3*D(X) + C(X)
The code I have for my program so far takes in the users 4-bit input in binary, i.e.
Insert 4-bit input: 1001
Then it checks it to make sure its valid, and shifts it giving:
0 0 0 1 0 0 1
which stands for the polynomial
X^3 + X^6
I then need to divide that by the generator polynomial
P(X) = 1 + X + X^3
Working it out on paper,
x^6 + X^3
___________
x^3 + x + 1
Gives: Q(X) = X^3 + X
R(X) = X^2 + X
So, T(X) = X^6 + X^3 + X^2 + X, which is 0111001 for the Codeword
What would be the best way to do this?
I have tried the following:
% Prompt for User input
b4_in = input('Insert 4-bit input: ' ,'s'); %Input 1001
%% CHECK FOR VAILD INPUT %%
dec_in = bin2dec(b4_in)
bin_in = fliplr(de2bi(dec_in)) %User input in Binary
d = [0000000]; %Calculating X^3 * D(X)
d = bin_in;
d(7)=0;
d = fliplr(d); %Gives 0 0 0 1 0 0 1
d
gen_pol = [1 1 0 1] %P(X) = 1 + X + X^3
[q, c] = deconv(bin_in, gen_pol)
When I go this, I get:
q =
1
c =
0 -1 0 0
What do I need to do differently to get the following?
q = 0 1 0 1
c = 0 1 1
Thank you!
In MATLAB, polynomials read in a binary vector from left to right. For example, x^3+x is [1 0 1 0], x^2+x is [1 1 0]. The Quotient Q(X) should be x^3-x instead of x^3+x. Make sure your inputs are in the right format, and you should get the following result as expected,
q =
1 0 -1 0
c =
0 0 0 0 1 1 0

Solving a linear system of equation with two variables in MATLAB

It might seem a simple question. I need it, though. Let's assume we have two equations:
2 * y + x + 1 = 0
and
y - 2 * x = 0
I would like to find their bisection which can be calculated from this equation:
|x + 2 * y + 1| |-2 *x + y |
------------------- = -----------------
(sqrt(2^2 + 1^2)) (sqrt(1^2 + 2^2))
To make the long story short, we only need to solve this below system of equation:
2 * y + x + 1 = -2 *x + y
and
2 * y + x + 1 = 2 *x - y
However, using solve function of MATLAB:
syms x y
eqn1 = 2 * y + x + 1 == -2 *x + y ;
eqn2 = 2 * y + x + 1 == 2 *x - y ;
[x, y] = solve (eqn1 , eqn2, x, y) ;
Will give me:
x = -1/5 and y = -2/5
But, I am looking for the result equations, which is:
y = -3 * x - 1 and 3 * y = 2 * x - 1
So, does anyone know how I can get the above line equation instead of the result point? Thanks,
The following should solve both equations with y on the left-hand-side:
y1 = solve(eqn1,y)
y2 = solve(eqn2,y)
Result:
y1 =
- 3*x - 1
y2 =
x/3 - 1/3
As an aside, it would be much faster to solve this system by thinking of it it as a matrix inversion problem Ax=b rather than using MATLAB's symbolic tools:
A = [1 2; -2 1];
b = [-1; 0];
x = A\b
Result:
x =
-0.2000
-0.4000

Numerical solutions of a nonlinear equation with different independent values in matlab

For example, if I have this function: g = t^3 - 5*t^2 + 2
And g = [3 4 6 2 9 10 17 1]
I would like to solve the equation for each g[i] and obtain the resulting t vector.
This might guide you:
>> syms t g %// define symbolic variables
>> y = t^3 - 5*t^2 + 2 - g; %// define y so that equation is: y=0
>> g_data = [3 4 6 2 9 10 17 1]; %// define g values
>> n = 1; %// choose first value. Or use a loop: for n = 1:numel(g_data)
>> s = solve(subs(y, g, g_data(n))) %// substitute g value and solve equation y=0
s =
25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) + ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3) + 5/3
5/3 - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)/2 - 25/(18*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - (3^(1/2)*(25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3))*i)/2
5/3 - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)/2 - 25/(18*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) + (3^(1/2)*(25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3))*i)/2
>> double(s) %// show solutions as floating point values
ans =
5.039377328113847
-0.019688664056924 + 0.445027607060817i
-0.019688664056924 - 0.445027607060817i

Have a trouble with the function roots

Hey guys I have multiple problems with using function 'roots'.
I Have to find zeros of 's^1000 + 1'.
I made Y = zeros(1,1000) then manually changed the 1000th matrice to '1'. but then 'root' function does not work with it !
Another problem is that I am having trouble with matrix multiplication. The question is finding zeros(roots) of (s^6 + 6*s^5 + 15*s^4 + 20*s^3 + 15*s^2 + 6*s +1)*(s^6 + 6s^5 + 15*s^4 +15*s^2 +6*s +1) so i did:
a = [1 6 15 20 15 6 1]
b = [1 6 15 0 15 6 1]
y = a.*b;
roots(y)
but this gives me
-27.9355 + 0.0000i
-8.2158 + 0.0000i
0.1544 + 0.9880i
0.1544 - 0.9880i
-0.1217 + 0.0000i
-0.0358 + 0.0000i
where I calculate the original equation with wolfram then I have made matrix as :
p = [1 12 66 200 375 492 524 492 375 200 66 12 1]
roots(p)
and this gives me :
-3.1629 + 2.5046i
-3.1629 - 2.5046i
0.3572 + 0.9340i
0.3572 - 0.9340i
-1.0051 + 0.0000i
-1.0025 + 0.0044i
-1.0025 - 0.0044i
-0.9975 + 0.0044i
-0.9975 - 0.0044i
-0.9949 + 0.0000i
-0.1943 + 0.1539i
-0.1943 - 0.1539i
and I think the second solution is right (that is what wolfram alpha gave me)
How would you answer these two questions through matlab guys?
To multiply polynomials, you convolve their coefficients:
>> roots(conv(a,b))
ans =
-3.1629 + 2.5046i
-3.1629 - 2.5046i
0.3572 + 0.9340i
0.3572 - 0.9340i
-1.0051
-1.0025 + 0.0044i
-1.0025 - 0.0044i
-0.9974 + 0.0044i
-0.9974 - 0.0044i
-0.9950
-0.1943 + 0.1539i
-0.1943 - 0.1539i
Q1
Using roots to find the roots of s1000 + 1 is a bit of an overkill. The solution is given by this code snippet (corrected the first version; may be deduced using De Moivre's formula):
n = 1000;
k = 0:n-1
u = (2*k + 1)*pi / n;
s = cos(u) + 1i*sin(u)
Also, this method is approx. 100000 times faster.
Q2
Multiplying two polynomials to find the roots of their product is a bit of an overkill. :-) The union of the two polynomials' root sets is the root set of the product polynomial:
s = [roots(a);roots(b)]
Also, this method is more accurate.
1) The vector describing s^1000 + 1 should end with a 1 as well.
2)
a = [1 6 15 20 15 6 1]
b = [1 6 15 0 15 6 1]
y = a.*b;
This is a DOT product, multiplication of polynomials do not multiply element-wise.
Question 1
You need to include the coefficient of x^0 in the vector of coefficients, so there are 1001 entries with the first and last being 1
coeffs=zeros(1001,1);
coeffs([1,1001])=1;
roots(coeffs)
Question 2
To multiply the coefficients of polynomials you need to use convolution:
roots(conv(a,b))