Evaluate polynomials with imaginary numbers - sage

I'm trying to calculate 19v^2 + 49v + 8 to the 67th power over the finite field Z/67Z using Sage where v = sqrt(-2).
Here's what I have so far (using t instead of v):
R.<t> = PolynomialRing(GF(67))
poly = (19 * (t^2) + 49*t + 8)
poly^67
This works fine.
But now, I want to evaluate the resulting polynomial with the value sqrt(-2).
I'm not sure how to proceed. Any ideas?

Computing with a square root of -2 amounts to working modulo
the polynomial t^2 + 2.
The function power_mod can be used for that.
Instead of first powering and then reducing modulo t^2 + 2,
which would be wasteful, it performs the whole powering
process modulo t^2 + 2, which is a lot more efficient.
Here are two ways to write the (same) computation.
sage: t = polygen(GF(67))
sage: p = 19 * t^2 + 49 * t + 8
sage: power_mod(p, 67, t^2 + 2)
sage: R.<t> = GF(67)[]
sage: p = R([8, 49, 19])
sage: power_mod(p, 67, R([2, 0, 1]))

Related

Complex numbers in fomula in R

I have this formula: A(k) + iB(k) + C(k)e^(5 * pi() * i * k/12500)
where (for example) B(k) = sin(k * pi()/20) and k = 1, 2, 3,..., 2500
How should I write this formula in R?
How
Thanks in advance
If a and b are numbers, you can write a + bi exactly like that in R, e.g. 1 + 2i is legal in math or R. To write i, use 1i and iB(k) is 1i * B(k).

Solving the recurrence equation T(n) = 3 + m * T(n - m)

I have a Computer Science Midterm tomorrow and I need help determining the complexity of a particular recursive function as below, which is much complicated than the stuffs I've already worked on: it has two variables
T(n) = 3 + mT(n-m)
In simpler cases where m is a constant, the formula can be easily obtained by writing unpacking the relation; however, in this case, unpacking doesn't make the life easier as follows (let's say T(0) = c):
T(n) = 3 + mT(n-m)
T(n-1) = 3 + mT(n-m-1)
T(n-2) = 3 + mT(n-m-2)
...
Obviously, there's no straightforward elimination according to these inequalities. So, I'm wondering whether or not I should use another technique for such cases.
Don't worry about m - this is just a constant parameter. However you're unrolling your recursion incorrectly. Each step of unrolling involves three operations:
Taking value of T with argument value, which is m less
Multiplying it by m
Adding constant 3
So, it will look like this:
T(n) = m * T(n - m) + 3 = (Step 1)
= m * (m * T(n - 2*m) + 3) + 3 = (Step 2)
= m * (m * (m * T(n - 3*m) + 3) + 3) + 3 = ... (Step 3)
and so on. Unrolling T(n) up to step k will be given by following formula:
T(n) = m^k * T(n - k*m) + 3 * (1 + m + m^2 + m^3 + ... + m^(k-1))
Now you set n - k*m = 0 to use the initial condition T(0) and get:
k = n / m
Now you need to use a formula for the sum of geometric progression - and finally you'll get a closed formula for the T(n) (I'm leaving that final step to you).

How do you simplify the difference between functions of x in R with respect to a Calculus context?

First of all, this looks like a fair amount of Calculus, so I predict that it would get forwarded to Cross-Validated by someone who thinks that this is TL;DR. But I think this is a programming question, so here me out.
Imagine that I have the following functions in terms of x: f(x), g(x), h(x) ...
f(x) = 2x^2 + 4x - 30
g(x) = x^2 - x + 12
h(x) = f(x) - g(x) = (2x^2 + 4x - 30) - (x^2 - x + 12) = x^2 + 5*x - 42
Note: If I were to compute g(x) - f(x) here I would get a different polynomial, but I would get the same roots so it doesn't really matter because if I took the coefficients from g(x) - f(x), then polyroot() would return the same x-intercept intersection points as f(x) = g(x).
I am able to resolve h(x) = (2x^2 + 4x - 30) - (x^2 - x + 12), but I can't resolve it to x^2 + 5*x - 42 which is just a more simplified version of the same function of h(x). But I need it in this form to compute the intersections of these functions where I need the coefficients of the difference function. Then I would use the points of intersection to compute the difference integral over the greater function minus the smaller function over the range where the functions intersect, and this difference integral is simply the area between the functions.
So my goal is to compute the area between two intersecting functions.
My problem is that I want to automate the whole process, and I want to simply the h(x) difference function to 1*x^2 + 5*(x) - 42, where the coefficients of this polynomial function in increasing order are -42, 5, 1 in that order.
So let's just write the code:
fx <- function(x){2*x^2 + 4*x - 30}
gx <- function(x){1*x^2 - 1*x + 12}
hx <- function(x){fx - gx} # doesn't work because I can't pass it to curve(hx)
hx <- function(x){(2*x^2 + 4*x - 30) - (1*x^2 - 1*x + 12)} # works
but it is not in the form that I want.
> hx
function(x){(2*x^2 + 4*x - 30) - (1*x^2 - 1*x + 12)}
<bytecode: 0x000000001c0bfc10>
Errors:
> curve(hx)
Error in expression(fx) - expression(gx) :
non-numeric argument to binary operator
See this is why I need the coefficients.
> z <- polyroot(c(-42, 5, 1)) # polyroot functions give you the x-intercepts of a polynomial function.
> z
[1] 4.446222-0i -9.446222+0i
Of course I could just compute "x^2 + 5*x - 42" on pen and paper, but they say that programmers always want to find the most efficient algorithmic process with the least amount of work.
Now I need to see which function is greater than the other, over the given range. Two ways visually or incrementally. (This is for the Calculus II part.)
x = seq(from = -9.4, to = 4.4, by = 0.2)
fx_range = 2*x^2 + 4*x - 30
> table(fx_range >= gx_range)
FALSE
70
> table(gx_range >= fx_range)
TRUE
70
It looks like the g(x) function is greater than or equal to the f(x) function over the range of the intersection points. So should evalulate the integral of g(x) - f(x) according to calculus. I was just doing f(x) - g(x) earlier for the polyroot function.
Areabetween curves = (from -9.446222 to 4.446222) ∫[g(x) - f(x)]dx
= (from -9.446222 to 4.446222) ∫[(x^2 - x + 12) - (2*x^2 + 4*x - 30)]
gx_minus_fx = function(x){(x^2 - x + 12) - (2*x^2 + 4*x - 30)}
Area = integrate(gx_minus_fx, lower = -9.446222, upper = 4.446222)
Area
446.8736 with absolute error < 5e-12 # This is exactly what I wanted to compute!
Now let's graphically check if I was supposed to subtract g(x) - f(x):
> curve(fx, main = "Functions with their Intersection Points", xlab = "x", ylab = "Functions of x", from = -9.446222, to = 4.446222)
> curve(gx, col = "red", add = TRUE)
> legend("topright", c("f(x) = 2x^2 + 4x - 30", "g(x) = x^2 - x + 12"), fill = c("black", "red"))
Yeah, I did it right!
So again, what I would like help with is figuring out how I could simplify
h(x) = f(x) - g(x) to x^2 + 5*x - 42.
This appears to be an algebraic problem. I showed that I could do high-level Calculus 2 in R, and I would just like to know if there is a way that I can automate this whole process for the h(x) function.
Thank you!!!

Formula of n-th member of combined arithmetic and geometric progression

I have a progression of format (b + 1) * 2.
1, 4, 10, 22, 46, 94, 190...
In code I can do it like:
k = 1
for i in range(n):
k += 1
k *= 2
But doing it with loop is a bit inefficient. Is there math formula to find it's n-th member?
If the starting number is b1, then
2-nd term is 2*b1 + 2
3-rd term is 4*b1 + 6
4-th term is 8*b1 + 14
5-nd term is 16*b1 + 30 ..
n-th term is
b1 * 2^(n-1) + 2^n - 2
for your example b0 = 1
b(n) = 3*2^(n-1) - 2

how can i calculate the polynomial that has the following asymptotes

how can i calculate the polynomial that has the tangent lines (1) y = x where x = 1, and (2) y = 1 where x = 365
I realize this may not be the proper forum but I figured somebody here could answer this in jiffy.
Also, I am not looking for an algorithm to answer this. I'd just like like to see the process.
Thanks.
I guess I should have mentioned that i'm writing an algorithm for scaling the y-axis of flotr graph
The specification of the curve can be expressed as four constraints:
y(1) = 1, y'(1) = 1 => tangent is (y=x) when x=1
y(365) = 1, y'(365) = 0 => tangent is (y=1) when x=365
We therefore need a family of curves with at least four degrees of freedom to match these constraints; the simplest type of polynomial is a cubic,
y = a*x^3 + b*x^2 + c*x + d
y' = 3*a*x^2 + 2*b*x + c
and the constraints give the following equations for the parameters:
a + b + c + d = 1
3*a + 2*b + c = 1
48627125*a + 133225*b + 365*c + d = 1
399675*a + 730*b + c = 0
I'm too old and too lazy to solve these myself, so I googled a linear equation solver to give the answer:
a = 1/132496, b = -731/132496, c = 133955/132496, d = -729/132496
I will post this type of question in mathoverflow.net next time. thanks
my solution in javascript was to adapt the equation of a circle:
var radius = Math.pow((2*Math.pow(365, 2)), 1/2);
var t = 365; //offset
this.tMax = (Math.pow(Math.pow(r, 2) - Math.pow(x, 2), 1/2) - t) * (t / (r - t)) + 1;
the above equation has the above specified asymptotes. it is part of a step polynomial for scaling an axis for a flotr graph.
well, you are missing data (you need another point to determine the polynomial)
a*(x-1)^2+b*(x-1)+c=y-1
a*(x-365)^2+b*(x-365)+c=y-1
you can solve the exact answer for b
but A depends on C (or vv)
and your question is off topic anyways, and you need to revise your algebra

Resources