Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 57c0ee7

Browse files
committed
add giac algo in the doc
1 parent 5b04bfa commit 57c0ee7

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

src/sage/symbolic/relation.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -489,27 +489,29 @@ def test_relation_maxima(relation):
489489
"""
490490
m = relation._maxima_()
491491

492-
#Handle some basic cases first
492+
# Handle some basic cases first
493493
if repr(m) in ['0=0']:
494494
return True
495495
elif repr(m) in ['0#0', '1#1']:
496496
return False
497497

498-
if relation.operator() == operator.eq: # operator is equality
498+
if relation.operator() == operator.eq: # operator is equality
499499
try:
500-
s = m.parent()._eval_line('is (equal(%s,%s))'%(repr(m.lhs()),repr(m.rhs())))
500+
s = m.parent()._eval_line('is (equal(%s,%s))' % (repr(m.lhs()),
501+
repr(m.rhs())))
501502
except TypeError:
502503
raise ValueError("unable to evaluate the predicate '%s'" % repr(relation))
503504

504505
elif relation.operator() == operator.ne: # operator is not equal
505506
try:
506-
s = m.parent()._eval_line('is (notequal(%s,%s))'%(repr(m.lhs()),repr(m.rhs())))
507+
s = m.parent()._eval_line('is (notequal(%s,%s))' % (repr(m.lhs()),
508+
repr(m.rhs())))
507509
except TypeError:
508510
raise ValueError("unable to evaluate the predicate '%s'" % repr(relation))
509511

510-
else: # operator is < or > or <= or >=, which Maxima handles fine
512+
else: # operator is < or > or <= or >=, which Maxima handles fine
511513
try:
512-
s = m.parent()._eval_line('is (%s)'%repr(m))
514+
s = m.parent()._eval_line('is (%s)' % repr(m))
513515
except TypeError:
514516
raise ValueError("unable to evaluate the predicate '%s'" % repr(relation))
515517

@@ -580,6 +582,7 @@ def string_to_list_of_solutions(s):
580582
# Solving #
581583
###########
582584

585+
583586
def solve(f, *args, **kwds):
584587
r"""
585588
Algebraically solve an equation or system of equations (over the
@@ -621,7 +624,7 @@ def solve(f, *args, **kwds):
621624
622625
- ``algorithm`` - string (default: 'maxima'); to use SymPy's
623626
solvers set this to 'sympy'. Note that SymPy is always used
624-
for diophantine equations.
627+
for diophantine equations. Another choice is 'giac'.
625628
626629
- ``domain`` - string (default: 'complex'); setting this to 'real'
627630
changes the way SymPy solves single equations; inequalities
@@ -907,7 +910,6 @@ def solve(f, *args, **kwds):
907910
sage: solve(abs(x + 3) - 2*abs(x - 3),x,algorithm='sympy',domain='real')
908911
[x == 1, x == 9]
909912
910-
911913
We cannot translate all results from SymPy but we can at least
912914
print them::
913915
@@ -1080,12 +1082,12 @@ def solve(f, *args, **kwds):
10801082

10811083
for v in variables:
10821084
if not is_SymbolicVariable(v):
1083-
raise TypeError("%s is not a valid variable."%repr(v))
1085+
raise TypeError("%s is not a valid variable." % repr(v))
10841086

10851087
try:
10861088
f = [s for s in f if s is not True]
10871089
except TypeError:
1088-
raise ValueError("Unable to solve %s for %s"%(f, args))
1090+
raise ValueError("Unable to solve %s for %s" % (f, args))
10891091

10901092
if any(s is False for s in f):
10911093
return []
@@ -1113,8 +1115,9 @@ def solve(f, *args, **kwds):
11131115
l.append(r)
11141116
return l
11151117
else:
1116-
return [[v._sage_() == ex._sage_() for v,ex in d.iteritems()]
1117-
for d in ret]
1118+
return [[v._sage_() == ex._sage_()
1119+
for v, ex in d.iteritems()]
1120+
for d in ret]
11181121
elif isinstance(ret, list):
11191122
l = []
11201123
for sol in ret:
@@ -1292,6 +1295,7 @@ def _solve_expression(f, x, explicit_solutions, multiplicities,
12921295
raise NotImplementedError("to_poly_solve does not return multiplicities")
12931296
# check if all variables are assumed integer;
12941297
# if so, we have a Diophantine
1298+
12951299
def has_integer_assumption(v):
12961300
from sage.symbolic.assumptions import assumptions, GenericDeclaration
12971301
alist = assumptions()
@@ -1336,15 +1340,15 @@ def has_integer_assumption(v):
13361340
else:
13371341
raise
13381342
if explicit_solutions:
1339-
P.eval('solveexplicit: false') # switches Maxima back to default
1343+
P.eval('solveexplicit: false') # switches Maxima back to default
13401344

13411345
if s == 'all':
13421346
if solution_dict:
1343-
ans = [ {x: f.parent().var('r1')} ]
1347+
ans = [{x: f.parent().var('r1')}]
13441348
else:
13451349
ans = [x == f.parent().var('r1')]
13461350
if multiplicities:
1347-
return ans,[]
1351+
return ans, []
13481352
else:
13491353
return ans
13501354

@@ -1538,7 +1542,6 @@ def solve_mod(eqns, modulus, solution_dict=False):
15381542
techniques, etc. But for a lot of toy problems this function as
15391543
is might be useful. At least it establishes an interface.
15401544
1541-
15421545
TESTS:
15431546
15441547
Make sure that we short-circuit in at least some cases::
@@ -1573,7 +1576,7 @@ def solve_mod(eqns, modulus, solution_dict=False):
15731576

15741577
if not isinstance(eqns, (list, tuple)):
15751578
eqns = [eqns]
1576-
eqns = [eq if is_Expression(eq) else (eq.lhs()-eq.rhs()) for eq in eqns]
1579+
eqns = [eq if is_Expression(eq) else (eq.lhs() - eq.rhs()) for eq in eqns]
15771580
modulus = Integer(modulus)
15781581
if modulus < 1:
15791582
raise ValueError("the modulus must be a positive integer")
@@ -1590,14 +1593,13 @@ def solve_mod(eqns, modulus, solution_dict=False):
15901593

15911594
has_solution = True
15921595
for p,i in factors:
1593-
solution =_solve_mod_prime_power(eqns, p, i, vars)
1596+
solution = _solve_mod_prime_power(eqns, p, i, vars)
15941597
if len(solution) > 0:
15951598
solutions.append(solution)
15961599
else:
15971600
has_solution = False
15981601
break
15991602

1600-
16011603
ans = []
16021604
if has_solution:
16031605
for solution in cartesian_product_iterator(solutions):
@@ -1612,6 +1614,7 @@ def solve_mod(eqns, modulus, solution_dict=False):
16121614
else:
16131615
return ans
16141616

1617+
16151618
def _solve_mod_prime_power(eqns, p, m, vars):
16161619
r"""
16171620
Internal help function for solve_mod, does little checking since it expects
@@ -1663,7 +1666,7 @@ def _solve_mod_prime_power(eqns, p, m, vars):
16631666
16641667
Currently this constructs possible solutions by building up
16651668
from the smallest prime factor of the modulus. The interface
1666-
is good, but the algorithm is horrible if the modulus isn't the
1669+
is good, but the algorithm is horrible if the modulus is not the
16671670
product of many small primes! Sage *does* have the ability to
16681671
do something much faster in certain cases at least by using the
16691672
Chinese Remainder Theorem, Groebner basis, linear algebra
@@ -1697,7 +1700,8 @@ def _solve_mod_prime_power(eqns, p, m, vars):
16971700
else:
16981701
shifts = cartesian_product_iterator([range(p) for _ in range(len(vars))])
16991702
pairs = cartesian_product_iterator([shifts, ans])
1700-
possibles = (tuple(vector(t)+vector(shift)*(mrunning//p)) for shift, t in pairs)
1703+
possibles = (tuple(vector(t) + vector(shift) * (mrunning // p))
1704+
for shift, t in pairs)
17011705
ans = list(t for t in possibles if all(e(*t) == 0 for e in eqns_mod))
17021706
if not ans:
17031707
return ans
@@ -1734,7 +1738,7 @@ def solve_ineq_univar(ineq):
17341738
17351739
ALGORITHM:
17361740
1737-
Calls Maxima command solve_rat_ineq
1741+
Calls Maxima command ``solve_rat_ineq``
17381742
17391743
AUTHORS:
17401744
@@ -1746,12 +1750,13 @@ def solve_ineq_univar(ineq):
17461750
ineq0 = ineq._maxima_()
17471751
ineq0.parent().eval("if solve_rat_ineq_loaded#true then (solve_rat_ineq_loaded:true,load(\"solve_rat_ineq.mac\")) ")
17481752
sol = ineq0.solve_rat_ineq().sage()
1749-
if repr(sol)=="all":
1753+
if repr(sol) == "all":
17501754
from sage.rings.infinity import Infinity
1751-
sol = [ineqvar[0]<Infinity]
1755+
sol = [ineqvar[0] < Infinity]
17521756
return sol
17531757

1754-
def solve_ineq_fourier(ineq,vars=None):
1758+
1759+
def solve_ineq_fourier(ineq, vars=None):
17551760
"""
17561761
Solves system of inequalities using Maxima and Fourier elimination
17571762
@@ -1799,7 +1804,7 @@ def solve_ineq_fourier(ineq,vars=None):
17991804
18001805
ALGORITHM:
18011806
1802-
Calls Maxima command fourier_elim
1807+
Calls Maxima command ``fourier_elim``
18031808
18041809
AUTHORS:
18051810
@@ -1809,7 +1814,7 @@ def solve_ineq_fourier(ineq,vars=None):
18091814
setvars = set([])
18101815
for i in (ineq):
18111816
setvars = setvars.union(set(i.variables()))
1812-
vars =[i for i in setvars]
1817+
vars = [i for i in setvars]
18131818
ineq0 = [i._maxima_() for i in ineq]
18141819
ineq0[0].parent().eval("if fourier_elim_loaded#true then (fourier_elim_loaded:true,load(\"fourier_elim\"))")
18151820
sol = ineq0[0].parent().fourier_elim(ineq0,vars)
@@ -1821,9 +1826,10 @@ def solve_ineq_fourier(ineq,vars=None):
18211826
sol = []
18221827
if repr(sol) == "[universalset]":
18231828
from sage.rings.infinity import Infinity
1824-
sol = [[i<Infinity for i in vars]]
1829+
sol = [[i < Infinity for i in vars]]
18251830
return sol
18261831

1832+
18271833
def solve_ineq(ineq, vars=None):
18281834
"""
18291835
Solves inequalities and systems of inequalities using Maxima.
@@ -1888,8 +1894,8 @@ def solve_ineq(ineq, vars=None):
18881894
18891895
ALGORITHM:
18901896
1891-
Calls solve_ineq_fourier if inequalities are list and
1892-
solve_ineq_univar of the inequality is symbolic expression. See
1897+
Calls ``solve_ineq_fourier`` if inequalities are list and
1898+
``solve_ineq_univar`` of the inequality is symbolic expression. See
18931899
the description of these commands for more details related to the
18941900
set of inequalities which can be solved. The list is empty if
18951901
there is no solution.

0 commit comments

Comments
 (0)