Ask Your Question

JRHales's profile - activity

2023-10-28 02:48:57 +0200 received badge  Notable Question (source)
2023-10-28 02:48:57 +0200 received badge  Popular Question (source)
2023-01-06 00:08:11 +0200 received badge  Nice Question (source)
2022-05-05 07:00:59 +0200 received badge  Famous Question (source)
2021-06-25 17:46:19 +0200 received badge  Famous Question (source)
2020-09-04 15:05:34 +0200 received badge  Notable Question (source)
2020-09-04 15:05:34 +0200 received badge  Popular Question (source)
2020-09-04 15:04:44 +0200 received badge  Famous Question (source)
2020-09-02 18:10:13 +0200 asked a question solving a matrix equation in the integers.

I'm trying to use sage to solve an expression of the form $xS = y$ for a fixed integer matrix $S$ and a fixed integer vector $y$. I need my solution vector $x$ to also have integer coefficients.

How I'm currently doing this is by using the mixed integer linear programming tool and my code looks something like this.

p = MixedIntegerLinearProgram(maximization = True, solver = "GLPK")
x = p.new_variable(integer = True, nonnegative = True)
p.add_constraint( x*S == y) #here the vector can be anything
p.add_constraint( x[25] <= 2020 )
p.set_objective( x[25] )
p.solve()
p.get_values(x)

The issue I have here is that the objective and the constraint are arbitrary and were just picked so that the space that the MixedIntegerLinearProgram function is trying to optimize over has a "feasable solution" (sage's words not mine). Is there a better way to find a solution to $xS = y$ in the integers (i.e a different function or package supported by sage) and if not, is there a way to have my constarint be to make the norm of the vector as small as possible (so that I'm not just artificially picking an entry to optimize my search over)?

2020-06-01 14:02:30 +0200 received badge  Famous Question (source)
2019-12-24 15:52:39 +0200 received badge  Notable Question (source)
2019-12-13 11:38:09 +0200 received badge  Notable Question (source)
2019-10-29 15:44:32 +0200 asked a question qsieve error

I'm trying to run the qsieve function in sage (version 8.9) and I'm getting a file or directory error.

For example, when I try the code

n = next_prime(10^20)*next_prime(10^21)
q,t = qsieve(n,time = True)

I get the error,

OSError: [Errno 2] No such file or directory.

I tried looking up if there was an additional library or package I needed to download to make the qsieve function work, but I couldn't find anything. Any advice or insight would be appreciated.

2019-09-30 21:18:01 +0200 received badge  Popular Question (source)
2019-05-24 21:22:15 +0200 commented answer installing the kohel database

Thank you, this worked perfectly once I realized I needed to run this in the sage shell! thanks a ton!

2019-05-24 19:43:09 +0200 asked a question installing the kohel database

I'm trying to work with modular polynomials and Hilbert class polynomials in sage, and I would try the code

Phi = ClassicalModularPolynomialDatabase()
print(Phi[2])

The resulting error is a "LookupError: filename /opt/sagemath-8.6/local/share/kohel/PolMod/Cls/pol.002.dbz does not exist"

Okay, no big deal, I just need to download the right database follow the path that sage is looking for the file at and put it there right? The problem is, when I went to download the database, from the sage website (http://ftp.yz.yamagata-u.ac.jp/pub/ma...) I get a strange .gz document, that doesn't let me use the classical modular polynomial database like I need to. I'm I doing this right? If not, where can I download the required package, and if I am, what's the next step to be able to use this database in sage? I'm using version 8.6.

2019-04-23 00:02:01 +0200 received badge  Popular Question (source)
2019-04-23 00:02:01 +0200 received badge  Notable Question (source)
2018-12-10 16:27:29 +0200 received badge  Popular Question (source)
2018-03-14 02:12:23 +0200 asked a question local variable 'Integer' referenced before assignment

I'll be honest I have no idea why this error is popping up, and it's really weird! I'm writing code that will generate a closed form solution to the partial sums of integer powers up to n, and this is what I have

def sum_first_n_p_powers(p):
    length = p+1
    vector = zero_vector(length)
    matrix_list = []
    for x in xrange(0,length):
        copy = vector[:]
        for y in xrange(0,length): 
            copy[y] = binomial(length-y,x-y+1)
        matrix_list.append(copy)
    M = Matrix(matrix_list)
    solution_vector = zero_vector(length)
    solution_vector[0] = 1
    coeffs = M.solve_right(solution_vector)
    n = var('n')
    0 = polynomial
    for x in xrange(0,len(coeffs)):
        polynomial = polynomial + coeffs[x]*n^(length-x)
    return polynomial

And when I try to run it I get the most unusual error, it just says to me

UnboundLocalError: local variable 'Integer' referenced before assignment. I have never run into this before and have no idea what's wrong with the line. Thanks in advance!

2018-03-14 01:58:53 +0200 commented answer Trouble with mixed integer linear programming

Thank you so much! That looks like exactly what I want to do!

2018-03-14 01:57:56 +0200 received badge  Scholar (source)
2018-03-10 04:10:04 +0200 commented question Trouble with mixed integer linear programming

I added the code that's running, besides the construction of the matrix which is 56x34, this is literally all that I'm inputting, and I don't know why sage thinks the feasible set is empty since it at least has the trivial solution.

2018-03-10 04:07:30 +0200 received badge  Editor (source)
2018-03-10 02:58:43 +0200 commented answer given a prime, finding where it is the list of primes

This work! Thanks a ton!

2018-03-10 02:58:24 +0200 received badge  Supporter (source)
2018-03-10 01:53:02 +0200 asked a question Trouble with mixed integer linear programming

I'm having trouble with sage's mixed linear programming tool. I entered the following code trying to maximize a variable with the given constraint that multiplication by a matrix would result in the zero vector.

picture of inputs is here

sage: p = MixedIntegerLinearProgram(maximization = True, solver = "GLPK")
sage: x = p.new_variable(integer = True, nonnegative = True)
sage: p.add_constraint(x*spoof_matrix == 0)
sage: p.set_objective(x[25])
sage: p.solve()

Where spoof_matrix is a 56x34 matrix that has the specific constraints I'm working with.

When I use the p.solve() it gives the error in the picture.

I know that there is at least one non-trivial solution since the test vector seen in the picture solves the constraints.

I don't know why sage isn't solving the optimization problem, and any help would be really great!

Also, if it's possible to not only get the maximum value but also obtain the vector that gives the maximum value, that would be really awesome. Thanks!!

Edit: People have asked for the code for how spoof matrix is created, using these functions the spoof matrix would be given by the following line.

create_spoof_matrix(reduce_n_times(25,generate_possible_factors_list(3),primes_first_n(168),168),168)

def spoof(p,a):
    if p*a == 0:
        return 1
    if p!=1 and a != 1:
        value = ((1/p)^(a+1)-1)/(1/p-1)
    elif p ==1:
        value = (a+1)
    elif a ==1:
        value = (1+1/p)
    return value

def reduce_possible_primes(possible_factors,number_of_primes):
    new_prime_list = []
    for x in possible_factors:
        for p in primes_first_n(number_of_primes):
            if x % p == 0:
                if p not in new_prime_list:
                    new_prime_list.append(p)
    for x in xrange(0,len(new_prime_list)): #this is just ordering the primes from least to greatest.
        for y in xrange(0,len(new_prime_list)):
            if new_prime_list[x] < new_prime_list[y]:
                cache = new_prime_list[x]
                new_prime_list[x] = new_prime_list[y]
                new_prime_list[y] = cache
    return new_prime_list


def generate_possible_factors_list(size_of_factors):
    possible_factors = []
    for x in xrange(-1*(10^size_of_factors)+1,10^size_of_factors,2):
        if x != -1:
            numerator_factor = list(factor(numerator(spoof(x,2))))
            if numerator_factor[-1][0] < 10^size_of_factors:
                possible_factors.append(x)
    return possible_factors



def reduce_possible_factors(possible_factors,possible_primes):
    for x in possible_factors:
        numerator_factor_bank = list(factor(numerator(spoof(x,2))))
        for y in xrange(0,len(numerator_factor_bank)):
            if numerator_factor_bank[y][0] not in possible_primes:
                 if x in possible_factors:
                    possible_factors.remove(x)
    return possible_factors
def reduce_n_times(n,possible_factors, possible_primes, number_of_primes):
    x = 0
    while x < n:
        possible_primes = reduce_possible_primes(possible_factors,number_of_primes)
        possible_factors = reduce_possible_factors(possible_factors,possible_primes)
        x = x+1
    return possible_factors
def create_spoof_matrix(possible_factors,n):
    possible_primes = reduce_possible_primes(possible_factors,n)
    vector = zero_vector(len(possible_primes))
    matrix_list = []
    for x in possible_factors:
        copy_vector = vector[:]
        denominator_list = list(factor(x))
        numerator_list = list(factor(numerator(spoof(x,2))))
        for y in denominator_list:
            copy_vector[possible_primes.index(y[0])] = -2*y[1]
        for y in numerator_list:
            copy_vector[possible_primes.index(y[0])] = y[1]
        matrix_list.append(copy_vector)
    return Matrix(matrix_list)
2018-02-11 21:47:59 +0200 received badge  Student (source)
2018-02-11 18:51:42 +0200 asked a question given a prime, finding where it is the list of primes

I'm writing a program that gives as an output the prime factorization of a number, and then I'm putting the primes into a matrix based on what prime number it is, (i.e 541 is the 100th prime, so if 541^2 divides my integer, then there would be a 2 in the 100th spot of my vector that represents my number)

Is there a function that takes as its input a prime and gives as an output where it is in the list of primes. (i.e we want f(541) = 100)

Thanks in advance!