ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 17 Dec 2021 09:10:58 +0100How to use the coppersmith method in sagemath?https://ask.sagemath.org/question/60319/how-to-use-the-coppersmith-method-in-sagemath/How to use the coppersmith method in sagemath?
Z^2 + 665748*Z + 1494997090560 = 1501297920*W
to be fair, I am writing to you what it is.
It is an exercise but it is understood well.
https://www.linkedin.com/feed/update/urn:li:activity:6876128768512131072/Periodic_1_6Fri, 17 Dec 2021 09:10:58 +0100https://ask.sagemath.org/question/60319/Bivariate Coppersmith methodhttps://ask.sagemath.org/question/56885/bivariate-coppersmith-method/Given the bivariate polynomial
p(m,n) = 675 * m * n + 297 * m + 25 * n + 11
The bivariate Coppersmith method can be used to find m0 and n0 such that
(675 * m0 * n0 + 297 * m0 + 25 * n0 + 11) mod (1763) = 0
where is it
m0 = 3 ; n0 = 3
Unfortunately I cannot understand the hypotheses
Kindly someone could only explain the hypotheses to me
EDIT1:
OTHER INFORMATION
You can choose some coefficients and their size order of this type of polynomial in O (16).
(N-3)/8-q*(p-A)/8-[4-(A-7)*(A-5)/8]=A*(q+A-4-8)/8
->
N=p*q
if we choose A such that p-A mod 8 = 0
we can write it this way
(N-3)/8-Q-[4-(A-7)*(A-5)/8]=A*X
so there are 4 chances to find A and they are
8*h+1 ; 8*h+3 ; 8*h+5 ; 8*h+7
(N-3)/8-p*(q-B)/8-[4-(B-7)*(A-5)/8]=B*(p+B-4-8)/8
->
N=p*q
if we choose B such that q-B mod 8 = 0
we can write it this way
(N-3)/8-P-[4-(B-7)*(B-5)/8]=B*Y
so there are 4 chances to find B and they are
8*k+1 ; 8*k+3 ; 8*k+5 ; 8*k+7
f(N,A,B) is O(16)
Example
1763=41*43
220-q*(p-25)/8-[4-(25-7)*(25-5)/8]=25*(q+25-4-8)/8
220-p*(q-27)/8-[4-(27-7)*(27-5)/8]=27*(p+B-4-8)/8
220-Q-[4-(25-7)*(25-5)/8]=25*X
220-P-[4-(27-7)*(27-5)/8]=27*X
Q=25*a+11 X=10-a
P=27*b+1 X=10-b
220-Q-[4-(17-7)*(17-5)/8]=17*X
Q=17*c+10 X=13-c 9-a=13-c -> c=a+4
220-P-[4-(19-7)*(19-5)/8]=19*X
P=19*d+9 X=12-d 9-b=12-d ->d=b+3
p=(19*(b+3)+9)-(27*b+1)=65-8*b
q=(17*(a+4)+10)-(25*a+11)=67-8*a
(65-8*b)*(67-8*a)=1763
,
(10-a)-(10-b)=((67-8*a)-(65-8*b)-2)/8
(65-8*b)*(27*1763)/(-8*(27*b+1)+1763)=1763 TRUE
So I thought about using the bivariate Coppersmith method
Q=25*a+11
P=27*b+1
p(m,n)=p(b,a)=P*Q=(27*b+1)*(25*a+11)=Z*1763
EDIT2:
At least see if I understand:
in my case given two polynomials
for example
(25*a+11)*(27*b+1) mod 1763 ==0
and
(17*(a+4)+10)*(19*(b+3)+9) mod 1763 == 0
the first question is can they be both modulo 1763?
|a|<IntegerPart[sqrt(1763/2)]
|b|<2*IntegerPart[sqrt(1763/2)]
if this method can be applied, it can be solved in polynomial time
Did I get it right ?P_1_6Fri, 30 Apr 2021 19:21:39 +0200https://ask.sagemath.org/question/56885/Can attributes of sage classes be changed?https://ask.sagemath.org/question/53892/can-attributes-of-sage-classes-be-changed/ I want to change the way vectors are rendered in sagetex. For example, consider `v = vector([1, 2, 3])`. In sagetex, the command `$v=\sage{v}$` renders as
$$
v=(1, 2, 3)
$$
Instead, I want my vectors to take the form
$$
v=\left[\begin{array}{rrr}1&2&3\end{array}\right]^\intercal
$$
I thought I could do this by resetting the `_latex_` method of the `sage.modules.free_module_element.FreeModuleElement` class as follows.
def my_vector_latex(self):
return matrix(v)._latex_() + r'^\intercal'
setattr(sage.modules.free_module_element.FreeModuleElement, '_latex_', my_vector_latex)
However, this code throws the following error:
TypeError: can't set attributes of built-in/extension type 'sage.modules.free_module_element.FreeModuleElement'
So, it looks like I can't modify the methods of `FreeModuleElement` in this way. Is there anything else I could do?more_weightTue, 13 Oct 2020 19:37:51 +0200https://ask.sagemath.org/question/53892/plot3d method does not properly work with threejshttps://ask.sagemath.org/question/50051/plot3d-method-does-not-properly-work-with-threejs/ The `plot3d` method allows to embed 2D objects in 3D graphics. It seems to work properly with the jmol viewer. However, it fails when the viewer is threejs. For example, the code
p = sum(plot((5-k)*sin(x), 0, 10, thickness=10,
color=colormaps.jet(50*k)).plot3d(z=k) for k in range(6))
show(p, viewer="jmol")
show(p, viewer="threejs")
yields this result:
![image description](/upfiles/1582626986673771.png)
The first graphic, corresponding to jmol, is correct. The second graphic, produced by threejs, is completely wrong.JuanjoTue, 25 Feb 2020 11:37:40 +0100https://ask.sagemath.org/question/50051/Redefine Method in Sage classhttps://ask.sagemath.org/question/45102/redefine-method-in-sage-class/I am using the a class to manipulate vector fields of $\mathbb{R}^n$.
I am using an other convention on the lie brackets : to compute $XY=[X,Y]$ I have to enter $XY=-X.bracket(Y)$.
**I would like to redefine the method "bracket()"** so that I include this minus sign in the redefinition.
Of course I can each time put a minus sign in front all my definition or call Y.bracket(X) to compute [X,Y] but I don't want to use them (the display is then not adapted ...).
Here is my code
from sage import *
reset()
def mybracket(A,B):
return B.bracket(A)
M.<x,y> = EuclideanSpace()
X = M.vector_field(0,1-y*x^2, name='X')
Y = M.vector_field(-(y-1),x, name='Y')
YX = Y.mybracket(X)
*AttributeError: 'VectorFieldFreeModule_with_category.element_class' object has no attribute 'mybracket'*
ThankssagenotdeadSat, 19 Jan 2019 14:42:20 +0100https://ask.sagemath.org/question/45102/How to use the divergence of a finite function as the source term of another problemhttps://ask.sagemath.org/question/44492/how-to-use-the-divergence-of-a-finite-function-as-the-source-term-of-another-problem/ I use RT element to solve problem A and denote the finite element solution by sigma, sigma is a matrix-valued finite element function. I need to use the divergence of sigma to be the source term of another problem, the trial function space is vector-valued functions. But it doesn't work. The code is
from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt
mesh = UnitSquareMesh(8,8)
RT = VectorElement("RT",mesh.ufl_cell(), 1,2)
Q = FunctionSpace(mesh, RT)
V = VectorFunctionSpace(mesh, 'CG',1)
f = Expression((('x[0]*x[1]','x[0]'),('x[0]','x[0]*x[1]')),degree=2)
sigma = TrialFunction(Q)
tau = TestFunction(Q)
a = inner(sigma, tau)*dx
L = inner(f,tau)*dx
sigma = Function(Q)
solve(a == L, sigma)
def boundary(x, on_boundary):
return on_boundary
bc = DirichletBC(V, Constant((0.0, 0.0)), boundary)
u = TrialFunction(V)
v = TestFunction(V)
au = inner(grad(u), grad(v))*dx
Lu = inner(sigma,grad(v))*dx
u = Function(V)
solve(au == Lu, u, bc)adaWed, 28 Nov 2018 19:41:43 +0100https://ask.sagemath.org/question/44492/may an object in sage lose its type in a method within a class ?https://ask.sagemath.org/question/44242/may-an-object-in-sage-lose-its-type-in-a-method-within-a-class/Many thanks to @slelievre for his response and to @tmonteil for his comments.
Here is the original Sage code. It and a little bit long but perhaps all these are
useful to see what goes wrong:
class CCODEPARAM:
def __init__(self, liste_ent, prem, em):
self.liste_ent = liste_ent
self.prem = prem
self.em = em
def qu(CyclicCodeParam):
q = (CyclicCodeParam.prem)^(CyclicCodeParam.em)
return q
def long(CyclicCodeParam):
NN = CyclicCodeParam.liste_ent
r = len(NN)
n = 1
for i in range(r):
n = n * NN[i]
return n
def epsilon(CyclicCodeParam):
NN = CyclicCodeParam.liste_ent
from sage.arith.functions import LCM_list
eps = LCM_list(NN)
return eps
def t(CyclicCodeParam):
NN = CyclicCodeParam.liste_ent
p = CyclicCodeParam.prem
r = len(NN)
m = CyclicCodeParam.em
q = p^m
eps = CyclicCodeParam.epsilon()
k = 1
while not(mod(q^k-1, eps) == 0):
k += 1
return k
def cardn(CyclicCodeParam):
return (CyclicCodeParam.qu())^(CyclicCodeParam.long())
def Gqu(CyclicCodeParam):
Q = (CyclicCodeParam.qu())^(CyclicCodeParam.t())
return Q
def liste_groupes(CyclicCodeParam):
NN = CyclicCodeParam.liste_ent
r = len(NN)
Groupes = []
for i in range(r):
Groupes.append(Set(IntegerModRing(NN[i])))
return Groupes
def prod_groupes(CyclicCodeParam):
GR = CyclicCodeParam.liste_groupes()
GG = list(cartesian_product(tuple(GR)))
return GG
def corps(CyclicCodeParam):
F = GF((CyclicCodeParam.qu()))
FF = GF(CyclicCodeParam.Gqu())
return [F, FF]
def prim(CyclicCodeParam):
K = CyclicCodeParam.corps()
L = K[1]
a = L.multiplicative_generator()
return a
def primorderb(CyclicCodeParam):
Q = CyclicCodeParam.Gqu()
eps = CyclicCodeParam.epsilon()
a = CyclicCodeParam.prim()
b = a^((Q - 1) / eps)
return b
def primuroots(CyclicCodeParam):
NN = CyclicCodeParam.liste_ent
r = len(NN)
eps = CyclicCodeParam.epsilon()
b = CyclicCodeParam.primorderb()
e = []
for i in range(r):
e.append(b^(eps/NN[i]))
return e
def galois(CyclicCodeParam):
t = CyclicCodeParam.t()
q = CyclicCodeParam.qu()
MM = list(Set({q^nu for nu in range(t)}))
return MM
def orbites(CyclicCodeParam):
GG = CyclicCodeParam.prod_groupes()
MM = CyclicCodeParam.galois()
NN = CyclicCodeParam.liste_ent
O = orb(GG, MM, NN)
return O
def ambspace(CyclicCodeParam):
F = CyclicCodeParam.corps()[0]
n = CyclicCodeParam.long()
K = list(cartesian_product([F]*n))
return K
def zeros(PAR, A):
O = PAR.orbites()
Z = Set({O[i][0] for i in A})
return Z
def CCODE(PAR, A, Vars):
# order = 'lex'
CORPS = PAR.ambspace()
print type(CORPS)
Z = PAR.zeros(A)
# print Z
Exp = PAR.orbites()
# print Exp
e = PAR.epsilon()
print e
CODE = {Zvalpol2(c, Vars, Exp, e, Z) for c in CORPS}
return CODE
def oneorb(g, MM, NN):
O = Set({})
r = len(NN)
for u in MM:
B = []
for i in range(r):
B.append(mod((g[i])*u,NN[i]))
O = O.union(Set({tuple(B)}))
return O
def orb(GG, MM, NN):
Orb = Set({})
GG = Set(GG)
for g in GG:
t = oneorb(g,MM,NN)
Orb = Orb.union(Set({t}))
GG = GG.difference(Orb)
return Orb
def pol(c, Vars, Exp):
n = len(c)
m = len(Vars)
P = 0
M = []
for i in range(n):
M.append(0)
M = list(M)
for i in range(n):
M[i] = 1
for j in range(m):
M[i] *= Vars[j]**Exp[i][j]
for i in range(n):
P += c[i] * M[i]
return P
def Zvalpol2(c, Vars, Exp, e, Z):
P = pol(c, Vars, Exp)
print('poly')
r = len(Vars)
V = Set({})
for h in Z:
E = []
for i in range(r):
E.append(e[i]**h[i])
V = V + Set({P(tuple(E))})
if V == Set({0}):
mes2 = 'mot de code'
print mes2
In the fonction "CCODE", when constructing the objet "CORPS" by `CORPS =PAR.ambspace()`, Sage return "none" for its type, even though the fonction "ambspace" return the object "K", which is of type "list". As a consequence, when executing the function CCODE, Sage returns an error by saying that objet of nonetype (which is CORPS) is not iterable. This happens on the instruction above the "return CODE" in the function CCODE.andriamSun, 11 Nov 2018 20:24:27 +0100https://ask.sagemath.org/question/44242/Why is partial_fraction a method but not a function?https://ask.sagemath.org/question/37589/why-is-partial_fraction-a-method-but-not-a-function/ Why is partial_fraction a method but not a function? non-function names don't show up in a Tab search so they're hard to recall. Is there a pattern to whether something is a method, a function, or both so I'd know how to search?cybervigilanteSun, 14 May 2017 23:34:01 +0200https://ask.sagemath.org/question/37589/Newton method for one variablehttps://ask.sagemath.org/question/34554/newton-method-for-one-variable/ I try to use 'solve' for nonlinear eauqtions in one variable, but the answer is tautological or "cannot evaluate symbolic expression numerically" if I add "explicit_solutions=True". Is Newton method (or any other, like secant method) implemented in Sage?
Ex:
sage: x=var('x')
sage: (x-cos(x)).solve(x)
[x == cos(x)]
while I would expect x= 0.739085logomathMon, 22 Aug 2016 09:53:18 +0200https://ask.sagemath.org/question/34554/Finding all shortest paths between given (specific) pair of verticeshttps://ask.sagemath.org/question/10332/finding-all-shortest-paths-between-given-specific-pair-of-vertices/I am working with graphs in sage and need a method of finding all shortest paths between some pair (or all pairs) of vertices.
Note that it is important to have all shortest paths registred, not just one, as seen in many Bellman-Ford/Dijkstra implementations (for instance Graph.shortest_path_all_pairs or networkx.algorithms.shortest_paths.all_pairs_shortest_path), and not just a number of those paths.
I am also satisfied with only a list of "optimal" predecessors... as long as the list is complete.
Thank you for answers!MorgothTue, 09 Jul 2013 07:45:53 +0200https://ask.sagemath.org/question/10332/Normalize vector to euclidean unit lengthhttps://ask.sagemath.org/question/9410/normalize-vector-to-euclidean-unit-length/I know I can compute a vector of unit length in a given direction using something like
v = some_vector_computation()
v = v/v.norm()
some_other_computation(v)
On the other hand, the most intuitive way to write this in a single line won't work:
some_other_computation(some_vector_computation().normalize())
This is because of the way `normalize` is defined, which is well documented and has been discussed in [this question](http://ask.sagemath.org/question/726/vectornormalize-function).
So I wonder, is there some other way to write the above in a single line? I'm not interested in solutions which call `some_vector_computation` twice, or have to name the vector, or use some lambda function, or similar hacks. I very much have a method in mind, but anything with similar complexibility and readability would be fine.
I know I could probably add my own method to the vector class at runtime, but modifying sage classes in this way doesn't exactly feel right. And I fear that using a non-standard method might make my code much harder to read, as others would expect being able to reproduce it, but won't be able to if they missed my custom method.
If there is no such method, do you agree that there should be, and that I should file a feature request on the Sage Trac?MvGWed, 10 Oct 2012 09:20:15 +0200https://ask.sagemath.org/question/9410/method overloading error?https://ask.sagemath.org/question/8645/method-overloading-error/Hi all:
I made a class called RationalExpression that encodes a rational expression (ratio of polynomials). It contains a class method called 'numerator' that returns the numerator of a RationalExpression instance, but the method doesn't work when i work with a multivariate polynomial ring. The 'numerator' method appears to clash with another 'numerator' method. Do you know how i can fix my bug?
Here's the relevant code snippet from /Users/arai021/Dropbox/sage_projects/pfd.py:
class RationalExpression(object):
r"""
Represents a rational expression `P/(Q_1^e_1 \cdots Q_m^e_m)` by storing the
parts `P` and `[[Q_1,e_1], \ldots, [Q_m,e_m]]`, where
`P, Q_1, \ldots, Q_m` are elements of a common polynomial ring, and
`e_1, \ldots, e_m` are positive integers.
"""
def __init__(self, num, *denoms):
self.numerator = num
self.base_ring = num.parent()
self.denominators = sorted(denoms)
def __str__(self):
return str([self.numerator, self.denominators])
def numerator(self):
return self.numerator
And here's a failing example:
sage: attach("/Users/arai021/Dropbox/sage_projects/pfd.py")
sage: R.<x,y>= PolynomialRing(QQ)
sage: P = x*y**2
sage: Qs = [x*y,3], [x,1]
sage: f = RationalExpression(P, *Qs)
sage: print f
[x*y^2, [[x, 1], [x*y, 3]]]
sage: type(f)
<class '__main__.RationalExpression'>
sage: f.numerator()
Traceback (most recent call last): print f
File "", line 1, in <module>
File "/private/var/folders/bc/bc2aJOjCE20MqpWiFIh58++++TQ/-Tmp-/tmpcbitNd/___code___.py", line 10, in <module>
exec compile(u'print f.numerator()' + '\n', '', 'single')
File "", line 1, in <module>
File "multi_polynomial_libsingular.pyx", line 2025, in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular.__call__ (sage/rings/polynomial/multi_polynomial_libsingular.cpp:14808)
TypeError: number of arguments does not match number of variables in parentaraichevWed, 18 Jan 2012 20:51:49 +0100https://ask.sagemath.org/question/8645/How can I re-code a function into a method?https://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/I have the following simple function 'add4' stored in a file 'foo.sage':
def add4(x):
"""Adds 4 to x"""
return x + 4
I can use this in sage by:
sage: attach 'foo.sage'
sage: add4?
Type: function
Base Class: <type 'function'>
String Form: <function add4 at 0x4f332a8>
Namespace: Interactive
File: Dynamically generated function. No source code available.
Definition: add4(x)
Docstring:
Adds 4 to x
sage: var('x')
x
sage: add4(x)
x + 4
How can I change my function to a method so that I can call it using the x.add4() syntax? I still want the method to reside in an external file.
rtrwalkerWed, 16 Nov 2011 18:57:28 +0100https://ask.sagemath.org/question/8482/Why does almost everything return "<built-in method METHOD of sage.symbolic.expression.Expression object at SOME_ADDRESS>"?https://ask.sagemath.org/question/7781/why-does-almost-everything-return-built-in-method-method-of-sagesymbolicexpressionexpression-object-at-some_address/Maybe the answer is obvious, but I couldn't find anything.
Let's say I have a variable that contains a symbolic expression. I can type a "." (dot) at the end and press TAB and I get a huge list of methods I can choose from. So I choose simplify_full or trig_reduce or something similar (I tried about 8 different methods). Now comes the thing I don't understand: Almost anything returns:
> built-in method METHODNAME of
> sage.symbolic.expression.Expression
> object at SOME_ADDRESS
For example:
> built-in method reduce_trig of
> sage.symbolic.expression.Expression
> object at 0x73b5ab8
Is there something wrong with my sage configuration? I'm running Ubuntu 10.10 64-bit.
Taylor SeriesWed, 01 Dec 2010 07:47:42 +0100https://ask.sagemath.org/question/7781/