I would like to declare the multivariate Ring $R[x_{1},x_{2},x_{3}]/(x_{2}^2-1, x_{1}^{3}x_{3}-2)$. It would be very helpful if there is some method to do this for an arbitrary number of variables.
Thank you so much,
I think AbstractAlgebra.jl is a very good fit for this. You can call it like:
julia> using AbstractAlgebra
julia> ring = ZZ
Integers
julia> S, (x, y, z) = PolynomialRing(ring, ["x", "y", "z"])
(Multivariate Polynomial Ring in x, y, z over Integers, AbstractAlgebra.Generic.MPoly{BigInt}[x, y, z])
julia> p1 = y^2 - 1
xy^2 - 1
julia> p2 = x^3 * z - 2
x^3*z - 2
julia> div(3 * (x^2 + y^3) * p1 + p2, p1)
3*x^2 + 3*y^3
julia> div(3 * (x^2 + y^3) * p1 + p2, p2)
1
And sorry if this doesn't answer your question, my mathematics is not that good. For more information, please visit the documentation.
According to my colleagues this is available in Oscar.jl [1] (which is built on Singular and AbstractAlgebra.jl, mentioned by #aahlback and myself in another answer).
The documentation is in the process of being written right now, so keep an eye on the package to see how this can be done in the very near future.
The implementation is in [2] if you want to take a look.
Edit: there looks to be a relevant example here [3]. Note the ring R in that example is a polynomial ring and does not correspond to your R.
For the coefficient ring (your R) you can currently use any field, Z or Z/nZ, according to the maintainer of the Singular project (which Oscar.jl uses for these computations). It's very, very hard to extend this to other rings.
[1] https://github.com/oscar-system/Oscar.jl/
[2] https://github.com/oscar-system/Oscar.jl/blob/master/src/Rings/MPolyQuo.jl
[3] https://oscar-system.github.io/Oscar.jl/dev/CommutativeAlgebra/ca/#Example-24
Related
I have a simple quadratic equation, but I need to find a way for R to solve for X or Y depending on the value I input for either. For example, my equation is
y = 232352x^2+2468776x+381622
I need to find a code that solves for y when x = 8000 and solve for x when y = 4000. Does such a code/function exist in R or do I have to do it manually?
The first part (solving for y when x=8000) is pretty straightforward.
You just type:
232352 * 8000^2 + 2468776 * 8000 + 381622
and R gives:
[1] 1.489028e+13
The second problem involves roots. The polyroot() function is what you're after. It takes in the coefficients of the equation as a vector, and returns the roots. So for your case:
polyroot(c(381622-4000,2468776,232352))
gives:
[1] -0.155227+0i -10.469928-0i
And then it's up to you to figure out which solution you want.
Remember in general if you want to solve y = Ax^2 + Bx + C for a particular value of y, you have to rearrange the equation to Ax^2 + Bx + (C-y) = 0.
Translated to R code this is:
coeff <- c(C-y,B,A)
polyroot(coeff)
Where you substitute A,B,C,y with the relevant numbers.
Let 𝐾=ℚ(√2) and 𝐿=𝐾(𝜔) where 𝑤^2+𝑤+1=0 is one of the cubic roots of unity. Let a be a primitive element of L, O_K the ring of algebraic integers. How do I then compute quotients like O_K/Z[a]? I tried:
K.<sqrt2> = NumberField(x^2-2)
L.<w> = K.extension(x^2+x+1)
a = L.primitive_element()
print L.ring_of_integers().quotient(ZZ[a])
However I always get errors like: "unable to convert Relative Order in Number Field in w0 with defining polynomial x^2 + (2*sqrt2 + 1)*x + sqrt2 + 3 over its base field to Number Field in w with defining polynomial x^2 + x + 1 over its base field".
What is the correct syntax here?
As you have noted, the problem is that we have w and w0 and they don't seem to agree. At this documentation link, we have an interesting example.
sage: K.<a> = NumberField(x^3 - 2)
sage: ZZ[a]
Order in Number Field in a0 with defining polynomial x^3 - 2 with a0 = a
This leads me to try
sage: K.<a> = NumberField(x^3 - 2)
sage: Za = ZZ[a]
sage: OK = K.maximal_order()
sage: Za.is_suborder(OK)
False
sage: OK.is_suborder(Za)
False
sage: OK
Maximal Order in Number Field in a with defining polynomial x^3 - 2
sage: Za
Order in Number Field in a0 with defining polynomial x^3 - 2 with a0 = a
sage: OK.number_field()
Number Field in a with defining polynomial x^3 - 2
sage: Za.number_field()
Number Field in a0 with defining polynomial x^3 - 2 with a0 = a
sage: OK.number_field() == Za.number_field()
False
And as we can see, even though a0=a there is apparently no way to directly compare even the underlying number fields. I'm not at all an expert in this part of the code, but I think it deserves a ticket for clarification at the least. I've opened Trac 28706.
In the meantime, if you can find a way to get the order you want using the usual syntax for orders, I'd do that. For the example I made from the documentation, I think Za and OK are the same, but for yours I tried this.
sage: Za.gens()
(1, w0, (-2*sqrt2 - 1)*w0 - sqrt2 - 3, (3*sqrt2 + 6)*w0 + 7*sqrt2 + 7)
sage: O1 = L.order([1,w,(-2*sqrt2 - 1)*w - sqrt2 - 3, (3*sqrt2 + 6)*w + 7*sqrt2 + 7])
sage: O1.is_suborder(OK)
True
which is certainly an improvement. Alas,
sage: OK.quotient(O1)
TypeError: unable to convert Maximal Relative Order in Number Field in w with defining polynomial x^2 + x + 1 over its base field to Number Field in w with defining polynomial x^2 + x + 1 over its base field
so now I am out of my depth. Are such quotients allowed per se? You may have to create an ideal instead to perform this action. Good luck!
I am trying to help a friend in finance research. I wish to solve, for x, the equation that looks like :
g,h,c,p,a,b are all constants.
I guess the first step would be to find it's derivative. This I did, using an online derivative calculator at http://www.derivative-calculator.net/. I got this :
Further, I am trying to solve for x, assuming this is equal to zero. None of the online tools for 'solve for x' are able to do it. I have tried, Wolfram Alpha's online tool for 'solve-for-x', QuickMath, CynMath etc. All of them, 'cannot be solved'. I am looking for a solution like : x = blah-blah-blah. I have also tried the online Mathlab/Octave tools at CompileOnline/TutorialPoint. What can I do to solve for x, (preferably not having to install MathLab etc). Is there anything about these equations that render them incapable to be solved by Wolfram Alpha or such online tools for 'solve for x' ?
You could just write a function in R to solve your equation for a given value of x:
solvex <- function(x) {
g = 1
h = 1
c = 1
p = 1
b = 1
a = 1
g * (1 - exp(h*x + c)) + p * (1 - exp(b*x + a))
}
Then, to plot the solutions over some range, do something like:
x <- seq(-100,100,1)
yseq <- lapply(x, solvex)
plot(x, yseq, type = "l")
Finding a solution using Mathematica:
I know this isn't exactly programming related per se, but programmers are the most
probable of all people who will recognize this maybe.
I have the following (X and Y are arrays, both with 3 elements), and I cannot recognize (although it reminds me of a few things, but none quite!) what is being done here. Does it ring any bells for anyone else ?
I gather you can disregard the lower part; the upper should probably give it away ... but I still cannot see it.
At first it reminded me of linear interpolation in 3d space ...
SUBROUTINE TRII(X,Y,XR,YR)
DIMENSION X(3),Y(3)
D=X(1)*(X(2)**2-X(3)**2)+
> X(2)*(X(3)**2-X(1)**2)+
> X(3)*(X(1)**2-X(2)**2)
D1=Y(1)*(X(2)*X(3)**2-X(3)*X(2)**2)+
> Y(2)*(X(3)*X(1)**2-X(1)*X(3)**2)+
> Y(3)*(X(1)*X(2)**2-X(2)*X(1)**2)
D2=Y(1)*(X(2)**2-X(3)**2)+
> Y(2)*(X(3)**2-X(1)**2)+
> Y(3)*(X(1)**2-X(2)**2)
D3=X(2)*(Y(3)-Y(1))+
> X(1)*(Y(2)-Y(3))+
> X(3)*(Y(1)-Y(2))
A=D1/D
B=D2/D
C=D3/D
YR=A+B*XR+C*XR**2
RETURN
END
SUBROUTINE TRIM(X,Y,XR,YR,XM,YM)
DIMENSION X(3),Y(3)
D=X(1)*(X(2)**2-X(3)**2)+
> X(2)*(X(3)**2-X(1)**2)+
> X(3)*(X(1)**2-X(2)**2)
D1=Y(1)*(X(2)*X(3)**2-X(3)*X(2)**2)+
> Y(2)*(X(3)*X(1)**2-X(1)*X(3)**2)+
> Y(3)*(X(1)*X(2)**2-X(2)*X(1)**2)
D2=Y(1)*(X(2)**2-X(3)**2)+
> Y(2)*(X(3)**2-X(1)**2)+
> Y(3)*(X(1)**2-X(2)**2)
D3=X(2)*(Y(3)-Y(1))+
> X(1)*(Y(2)-Y(3))+
> X(3)*(Y(1)-Y(2))
A=D1/D
B=D2/D
C=D3/D
XR=-B/(2.*C)
YR=A+B*XR+C*XR**2
XM=XR
IF(XR.GT.X(1).OR.XR.LT.X(3))XM=X(1)
YM=A+B*XM+C*XM**2
IF(YM.LT.Y(1))XM=X(1)
IF(YM.LT.Y(1))YM=Y(1)
RETURN
END
">" is a continuation sign.
The code run as follows
Routine TRII takes as input the coordinates of three points (x,y) and interpolates a parabola using Lagrange interpolation. Also takes as input the coordinate XR. Returns in YR the value at XR for the interpolating parabola.
I guess the name of the routine comes from "TRI" (Croatian for "three" (points)) and "I" for Interpolation.
Routine TRIM also calculates the same parabola, and returns the minimun value of the function in the interval {X(1),X(3)}.The name comes from "TRI" and "M" (minimum)
(I "really" executed the program) >)
Note that this is FORTRAN code and the parameters are passed by reference, so the results are returned back in the same parameters (very odd!)
Edit
Just for fun, let's run TRII
TRII[X_, Y_, XR_] :=
Module[{D0, D1, D2, D3, A, B, C},
D0 = X[[1]]*(X[[2]]^2 - X[[3]]^2) +
X[[2]]*(X[[3]]^2 - X[[1]]^2) +
X[[3]]*(X[[1]]^2 - X[[2]]^2);
D1 = Y[[1]]*(X[[2]]*X[[3]]^2 - X[[3]]*X[[2]]^2) +
Y[[2]]*(X[[3]]*X[[1]]^2 - X[[1]]*X[[3]]^2) +
Y[[3]]*(X[[1]]*X[[2]]^2 - X[[2]]*X[[1]]^2);
D2 = Y[[1]]*(X[[2]]^2 - X[[3]]^2) +
Y[[2]]*(X[[3]]^2 - X[[1]]^2) +
Y[[3]]*(X[[1]]^2 - X[[2]]^2);
D3 = X[[2]]*(Y[[3]] - Y[[1]]) +
X[[1]]*(Y[[2]] - Y[[3]]) +
X[[3]]*(Y[[1]] - Y[[2]]);
A = D1/D0;
B = D2/D0;
C = D3/D0;
Return[A + B*XR + C*XR^2];];
X = RandomReal[1, 3];
Y = RandomReal[1, 3];
Show[Plot[TRII[X, Y, x], {x, 0, 1}],
ListPlot[Transpose[{X, Y}], PlotMarkers -> Automatic]]
D is the determinant of the matrix:
| x(1) x(1)² 1 |
D = det | x(2) x(2)² 1 |
| x(3) x(3)² 1 |
In D1, the rightmost column has been replaced with Y:
| x(1) x(1)² Y(1) |
D1 = det | x(2) x(2)² Y(2) |
| x(3) x(3)² Y(3) |
In D2, and D3 it's the first and second columns, respectively. Is it easier to recognize now? Looks a lot like using Cramer's rule to solve a linear equation to me.
Edit: To be more precise: (A, B, C) is the solution to the system:
A + x(1)*B + x(1)²*C = Y(1)
A + x(2)*B + x(2)²*C = Y(2)
A + x(3)*B + x(3)²*C = Y(3)
YR is the square of the solution to the quadratic equation (nb, different x!):
C*x² + B*x + A = 0
I feel like this should be obvious now, but I can't quite grasp it...
This code represents a kind of interpolation/quadratic curve fitting on three 2d points together with a way to compute the minimum or maximum value of such a fitted quadratic within the interval itself. I guess that TRII stands for triple (point)-interpolation and TRIM stands for triple (point) minimum or maximum.
To be more precised TRII solves the problem :- find a quadratic curve that passes through the points (x1,y1),(x2,y2) and (x3,y3) in the form Y=A+BX+CX^2 and compute the Y value of the quadratic at the point XR and return as YR. This is basically a way to interpolate smoothly between three 2d points. It is often used to find a better approximation for the max or min value of a set of discrete data points.
All the D, D1, D2, D3 stuff is to solve the matrix equation:
(1 X1 X1^2) *(A) = (Y1)
(1 X2 X2^2) *(B) = (Y2)
(1 X3 X3^2) *(C) = (Y3)
using Cramers rule as mentioned in one of the other comments, D is the matrix determinant and D1, D2, D3 are co-factors.
TRIM again computes the quadratic Y=A+BX+CX^2 and then finds a max/min of this quadratic (XM, YM). This is done by initially finding the point where the quadratic has a turning point: if F(X)=A+BX+CX^2, F'(XR)=B+2*C*XR=0, or XR=-B/2*C, YR=A+BXR+CXR^2. There is then some logic to force the returned XM, YM min or max values to lie within certain bounds.
The code:
XM=XR
.
.
.
IF(YM.LT.Y(1))YM=Y(1)
Is a little weird since if we assume that GT and LT mean greater than and less than respectively then we need to assume that X3'<'X1 otherwise the condition (XR.GT.X(1).OR.XR.LT.X(3)) is trivial and XM,YM are set to X1, Y1.
So X3'<'X1 and the condition says that if the quadratics max/min value is outside the interval (X1,X3) then set (XM,YM) to (X1, Y1) as before. If not then if Y1 is above the min/max value in Y then again set (XM,YM) to (X1, Y1).
It is hard to understand what this means and I suspect the code may be wrong! Any thoughts?
Ivan
I'm not sure what language this is, but it's clear that this is some sort of solver for quadratic equations. The XR and YR expressions are a dead giveaway:
XR = -B / (2.*C)
YR = A + B*XR + C*XR**2
Without knowing what the X(1..3) and Y(1..3) expressions are, however, it's not going to be possible to infer too much more about what the A/B/C coefficients represent, however. Lots of things use quadratic equations -- area of a circle given the radius, intensity of light at a given distance, et cetera. More contextual data is required.
Update: The OP indicated that he can't be too much more specific for secrecy reasons. Here are some hints, though:
What does the subroutine return? How are those results used later on? That may lead to better insights.
It appears that Y(1) is some sort of magic lower bound for the result of this computation. Notice that if YM is less than Y(1), then both XM and YM are set to X(1) and Y(1), respectively.
The "D" expressions look like this, in more natural syntax:
d = x1 * [x2^2 - x3^2] + x2 * [x3^2 - x1^2] + x3 * [x1^1 - x2^2]
d1 = y1 * [x2*x3^2 - x3*x2^2] + y2 * [x3*x1^2 - x1*x3^2] + y3 * [x1*x2^2 - x1*x2^2]
d2 = y1 * [x2^2 - x3^2] + y2 * [x3^2 - x1^2] + y3 * [x1^2 - x2^2]
d3 = x2 * [y3 - y1] + x1 * [y2 - y3] * x3 * [y1 - y2]
This looks very much like some sort of matrix operation; D is almost certainly for "determinant". But there are other things that have the same mathematical relationship.
This is a way to solve linear equation systems, specifically cramers rule. Also have a look at the rule of sarrus. After that, you seem to construct a quadratic equation out of it.
Does anyone know how to minimize a function containing an integral in MATLAB? The function looks like this:
L = Int(t=0,t=T)[(AR-x)dt], A is a system parameter and R and x are related through:
dR/dt = axRY - bR, where a and b are constants.
dY/dt = -xRY
I read somewhere that I can use fminbnd and quad in combination but I am not able to make it work. Any suggestions?
Perhaps you could give more details of your integral, e.g. where is the missing bracket in [AR-x)dt]? Is there any dependence of x on t, or can we integrate dR/dt = axR - bR to give R=C*exp((a*x-b)*t)? In any case, to answer your question on fminbnd and quad, you could set A,C,T,a,b,xmin and xmax (the last two are the range you want to look for the min over) and use:
[x fval] = fminbnd(#(x) quad(#(t)A*C*exp((a*x-b)*t)-x,0,T),xmin,xmax)
This finds x that minimizes the integral.
If i didn't get it wrong you are trying to minimize respect to t:
\int_0^t{(AR-x) dt}
well then you just need to find the zeros of:
AR-x
This is just math, not matlab ;)
Here's some manipulation of your equations that might help.
Combining the second and third equations you gave gives
dR/dt = -a*(dY/dt)-bR
Now if we solve for R on the righthand side and plug it into the first equation you gave we get
L = Int(t=0,t=T)[(-A/b*(dR/dt + a*dY/dt) - x)dt]
Now we can integrate the first term to get:
L = -A/b*[R(T) - R(0) + Y(T) - Y(0)] - Int(t=0,t=T)[(x)dt]
So now all that matters with regards to R and Y are the endpoints. In fact, you may as well define a new function, Z which equals Y + R. Then you get
L = -A/b*[Z(T) - Z(0)] - Int(t=0,t=T)[(x)dt]
This next part I'm not as confident in. The integral of x with respect to t will give some function which is evaluated at t = 0 and t = T. This function we will call X to give:
L = -A/b*[Z(T) - Z(0)] - X(T) + X(0)
This equation holds true for all T, so we can set T to t if we want to.
L = -A/b*[Z(t) - Z(0)] - X(t) + X(0)
Also, we can group a lot of the constants together and call them C to give
X(t) = -A/b*Z(t) + C
where
C = A/b*Z(0) + X(0) - L
So I'm not sure what else to do with this, but I've shown that the integral of x(t) is linearly related to Z(t) = R(t) + Y(t). It seems to me that there are many equations that solve this. Anyone else see where to go from here? Any problems with my math?