Finding any one solution to two inequalities in 2 variables (Python) - sage

I have two inequalities as below and while I can find the appropriate regions for which the inequalities hold true, I am looking for any one particular value rather than solving the inequality by hand. So one of the solution in this case would be when d=-4.5 and g=3. And I want any one correct solution. I am using sage.
Input: solve([-1/2*d + 1/2 < g, g < -d - 1],[d,g])
Output: [[-2*g + 1 < d, d < -g - 1, 2 < g]]
The closest I have got is by using sympy modules but I was not able to solve 2 equations, only 1 works:
Input: solve_univariate_inequality(x**2 >= 4, x, relational=False)
Output:(−∞,−2]∪[2,∞)
Is anyone aware of a technique that would lead me to a solution?

I'm confused as to why the Sage answer isn't acceptable. It says any g>2 so pick g=4, maybe, and then any d between -2*g+1 and -g-1, so that would be between -7 and -5, so -6 works. The point is to give you all answers.
You could do something like this, but you'd still have to look at the first solution to see what was appropriate for d, or find a way to programmatically extract that information - and that might be challenging, since in general your inequality may not have a nice solution like this, and the order of the inequalities in the solution might have a more random order.
var('d,g')
S = solve([-1/2*d + 1/2 < g, g < -d - 1],[d,g])[0]
[s.subs(g=4) for s in S]
I hope this at least helps you frame your question, if not perhaps even answer your actual query; good luck.

Related

Sagemath: Is there a simple way to factor a polynomial over C & have the roots appear in radical instead of decimal form?

In Mathematica, if I do the following
Roots[x^3 - 2 == 0, x]
I get
x=(-1)^(2/3) 2^(1/3) || x=(-2)^(1/3) || x = 2^(1/3)
I want to do something similar in Sagemath
sage: F1.<x> = PolynomialRing(CC)
sage: f=x^3 - 2
sage: f.roots()
[(1.25992104989487, 1),
(-0.629960524947437 - 1.09112363597172*I, 1),
(-0.629960524947437 + 1.09112363597172*I, 1)]
Is there a way in sagemath to see it either as radicals or as ^(1/n) or something similar?
Is there a reason you need this computation to take place within a complex polynomial ring? I'm not an expert in computer algebra and I'm sure I'm oversimplifying or something, but I believe that is the root of this behavior; Sage treats the complex numbers as an inexact field, meaning that it stores the coefficients a and b in a+b*I as (default 53-bit) floats rather than as symbolic constants. Basically, what you are asking for is a type error, any object defined over the ComplexField (or ComplexDoubleField, or presumably any inexact field) will have floats as its coefficients. On the other hand, the corresponding behavior in the symbolic ring (where the token x lives by default) seems to be exactly what you are looking for; more specifically, evaluating var("t"); solve(t^3-2==0,t) returns [t == 1/2*I*sqrt(3)*2^(1/3) - 1/2*2^(1/3), t == -1/2*I*sqrt(3)*2^(1/3) - 1/2*2^(1/3), t == 2^(1/3)].

Using python to find the limit of a recusive function

Assume I had the following iterative fuction:
f(z) = z^2 + c
z initally equal to 0
and each answer of the function becomes z for the next iteration. i.e. if c is 1 then the fist iteration gives 1, the second gives 2 and so fourth.
Now assuming I already set a value for c, I would like to be able to use Python to find the limit as this function approaches an infinite number of iterations. How would I best be able to do that? Would Sympy be a good tool?
editied to clearify what I man by iterative function.

modelling an infinite series in R

I'm trying to write a code to approximate the following infinite Taylor series from the Theis hydrogeological equation in R.
I'm pretty new to functional programming, so this was a challenge! This is my attempt:
Wu <- function(u, repeats = 100) {
result <- numeric(repeats)
for (i in seq_along(result)){
result[i] <- -((-u)^i)/(i * factorial(i))
}
return(sum(result) - log(u)-0.5772)
}
I've compared the results with values from a data table available here: https://pubs.usgs.gov/wsp/wsp1536-E/pdf/wsp_1536-E_b.pdf - see below (excuse verbose code - should have made a csv, with hindsight):
Wu_QC <- data.frame(u = c(1.0*10^-15, 4.1*10^-14,9.9*10^-13, 7.0*10^-12, 3.7*10^-11,
2.3*10^-10, 6.8*10^-9, 5.7*10^-8, 8.4*10^-7, 6.3*10^-6,
3.1*10^-5, 7.4*10^-4, 5.1*10^-3, 2.9*10^-2,8.7*10^-1,
4.6,9.90),
Wu_table = c(33.9616, 30.2480, 27.0639, 25.1079, 23.4429,
21.6157, 18.2291, 16.1030, 13.4126, 11.3978,
9.8043,6.6324, 4.7064,2.9920,0.2742,
0.001841,0.000004637))
Wu_QC$rep_100 <- Wu(Wu_QC$u,100)
The good news is the formula gives identical results for repeats = 50, 100, 150 and 170 (so I've just given you the 100 version above). The bad news is that, while the function performs well for u < ~10^-3, it goes off the rails and gives negative outputs for numbers within an order of magnitude or so of 1. This doesn't happen when I just call the function on an individual number. i.e:
> Wu(4.6)
[1] 0.001856671
Which is the correct answer to 2sf.
Can anyone spot what I've done wrong and/or suggest a better way to code this equation? I think the problem is something to do with my for loop and/or an issue with the factorials generating infinite numbers as u gets larger, but I'm not at all certain.
Thanks!
As it says on page 93 of your reference, W is also known as the exponential integral. See also here.
Then, e.g., the package expint provides a function to compute W(u):
library(expint)
expint(10^(-8))
# [1] 17.84347
expint(4.6)
# [1] 0.001841006
where the results are exactly as in your referred table.
You can write a function that takes in a value together with the repetition times and outputs the required value:
w=function(u,l){
a=2:l
-0.5772-log(u)+u+sum(u^(a)*rep(c(-1,1),length=l-1)/(a)/factorial(a))
}
transform(Wu_QC,new=Vectorize(w)(u,170))
u Wu_table new
1 1.0e-15 3.39616e+01 3.396158e+01
2 4.1e-14 3.02480e+01 3.024800e+01
3 9.9e-13 2.70639e+01 2.706387e+01
4 7.0e-12 2.51079e+01 2.510791e+01
5 3.7e-11 2.34429e+01 2.344290e+01
6 2.3e-10 2.16157e+01 2.161574e+01
7 6.8e-09 1.82291e+01 1.822914e+01
8 5.7e-08 1.61030e+01 1.610301e+01
9 8.4e-07 1.34126e+01 1.341266e+01
10 6.3e-06 1.13978e+01 1.139777e+01
11 3.1e-05 9.80430e+00 9.804354e+00
12 7.4e-04 6.63240e+00 6.632400e+00
13 5.1e-03 4.70640e+00 4.706408e+00
14 2.9e-02 2.99200e+00 2.992051e+00
15 8.7e-01 2.74200e-01 2.741930e-01
16 4.6e+00 1.84100e-03 1.856671e-03
17 9.9e+00 4.63700e-06 2.030179e-05
As the numbers become large the estimation is not quite good, so we should have to go further than 170! but R cannot do that. Maybe you can try other platforms. ie Python
I think I may have solved this myself (though borrowing heavily from Onyambo's answer!) Here's my code:
well_func2 <- function (u, l = 100) {
result <- numeric(length(u))
a <- 2:l
for(i in seq_along(u)){
result[i] <- -0.5772-log(u[i])+u[i]+sum(u[i]^(a)*rep(c(-1,1),length=l-1)/(a)/factorial(a))
}
return(result)
}
As far as I can tell so far, this matches the tabulated results well for u <5 (as did Onyambo's code), and it also gives the same result for vector vs single-value inputs.
Still needs a bit more testing, and there's probably a tidier way to code it using map() or similar instead of the for loop, but I'm happy enough for now. Thought I'd share in case anyone else has the same problem.

How to add if(A and B) then C constraints in Gurobi

Now I have three 5 by 3 matrix, X, Y, Z. All elements in them are binary variables. I want to add the following constraints:
I know I should introduce an auxiliary binary variable here. But I really stuck in how to write these simple conditional equalities as linear constraints. Any advice, tricks, suggestions?
Many thanks in advance!
Actually you don't need extra binary variables for this.
x(i,j)=1 and x(i+1,j)=0 => z(i+1,j)=1
can be interpreted as:
z(i+1,j) >= x(i,j)*(1-x(i+1,j))
This can be written as a linear inequality:
z(i+1,j) >= x(i,j) - x(i+1,j)
Similarly,
x(i,j)=0 and x(i+1,j)=1 => y(i+1,j)=1
can be formulated as:
y(i+1,j) >= x(i+1,j) - x(i,j)

Set Theory & Geometry: Two arcs on the same circle overlap with wrapping values

As a background, I'm a computer programmer and I'm working on a software library that allows a computer to quickly search through all dates to find a set of dates that satisfies a criteria. For example:
I want a list of every possible time that has ever occurred that has occurred on a friday or a saturday that is in April or May during the first week of the month.
My library uses numerical sets to efficiently represent ranges of dates that satisfy a criteria.
I've been thinking about ways to improve the performance of some parts of the app and I think that by combining sets and some geometry, I can really improve my results. However, my geometry is a bit rusty and I was hoping you might could help.
Here's my thought:
Certain elements of time can be represented as a circular dial. For example, Minutes can be positioned on a clock with values between 0...59. We could store valid ranges as a list of arcs. For example, If we wanted all times that ended with 05..10, we could store [5,10]. If we wanted all times that end with :45-59 or :00-15, we could store [45, 15]. Notice how this last arc "loops around" the dial. Here's a mockup showing different ranges intersecting on a dial
My question is this:
Given a set of whole numbers between N...M arranged into a circle.
Given Arc1 which is representing by [A, B] and Arc2 which is represented by [C, D] where A, B, C, and D are all within in range N...M
How do I determine:
A. Whether the arcs intersect.
B. If they do, what their intersection is.
C. If they do, what their union is.
Thank you so much for your help. If you're not able to help, if you can point me in the right direction, that would be great.
Thanks!
A simple and safe approach is to split the intervals that straddle 0. Then you perform pairwise interval intersection/union (for instance if A < D and C < B then [max(A,C), min(B,D)] for the intersection), and merge them if they meet at 0.
It seems the primitive operation to implement would be something like 'is the number X contained in the arch [A,B]'. Once you have that, you could implement an [A,B]/[C,D] arch-intersection predicate by something like -
Arch intersection means exactly that at least one of the following conditions is met:
C is contained in [A,B]
D is contained in [A,B]
A is contained in [C,D]
B is contained in [C,D]
One way to implement this contained-in-arch test without any branches is with some trigonometry and vector cross product. Not sure it would be faster (the math/branches performance tradeoff is entirely empiric), but it might be worth a try.
Denote Xa = sin(X/N * 2PI), Ya = cos(X/N * 2PI) and similarly for Xb,Yb etc.
C is contained in [A,B] is equivalent to:
Xa * Yc - Ya * Xc > 0
AND
Xc * Yb - Yc * Xb > 0
You can complete the other 3 conditions in an identical manner.
Hope this turns out useful.

Resources