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.Mon, 12 Oct 2015 07:06:50 +0200Can't Figure out how to Fix IndexError Based on Len()https://ask.sagemath.org/question/29927/cant-figure-out-how-to-fix-indexerror-based-on-len/ Can anyone help fix an error happening on the line "for i in range(1,len(sums)-1):"? I'm relevantly new to sage and more use to python but the few differences sage has I think have been taken into account but I can't figure out what is causing this IndexError..
def Ramanujan(t):
cubes = [x**3 for x in range(1,t/10)];
crev = [] # Calculating Cube Roots;
for x,x3 in enumerate(cubes):
crev[x3] = x + 1;
sums = sorted(x + y for x in cubes for y in cubes if y < x) # Organizing Data
for i in range(1,len(sums)-1):
if sums[i-1] != sums[i] and sums[i] == sums[i+1]: # Finding solutions
if sums[i]<=t: # Limiting how many solutions printed.
print "%10d"%(sums[i]) # Printing desired outputs
else:
break # Ending the function.
Ramanujan(10000)
Error:
Traceback (most recent call last): for i in range(1,len(sums)-1):
File "", line 1, in <module>
File "/private/var/folders/96/g5hyl4ps29dglpy8fnwww6x80000gn/T/tmpWiTKG1/___code___.py", line 16, in <module>
exec compile(u'Ramanujan(_sage_const_10000 )
File "", line 1, in <module>
File "/private/var/folders/96/g5hyl4ps29dglpy8fnwww6x80000gn/T/tmpWiTKG1/___code___.py", line 7, in Ramanujan
crev[x3] = x + _sage_const_1 ;
IndexError: list assignment index out of range
Does anyone have an idea of how to fix the IndexError I am running across?KristofferHMon, 12 Oct 2015 07:06:50 +0200https://ask.sagemath.org/question/29927/re.complile failure in a SageWS (Vertical Bar character)https://ask.sagemath.org/question/25854/recomplile-failure-in-a-sagews-vertical-bar-character/I was trying to use a Python Cookbook recipe (https://www.safaribooksonline.com/library/view/python-cookbook-2nd/0596007973/ch01s19.html) for multiple regular expression matches and substitutions in a Sagemath Cloud Sage Worksheet (final usage is for altering the __latex_/_repr_ attributes of an object). The code from the cookbook is below:
import re
def multiple_replace(txt, adict):
rx = re.compile('|'.join(map(re.escape, adict)))
def one_xlat(match):
return adict[match.group(0)]
return rx.sub(one_xlat, txt)
txt = "Larry Wall is the creator of Perl"
adict = {
"Larry Wall" : "Guido van Rossum",
"creator" : "Benevolent Dictator for Life",
"Perl" : "Python",
}
print(multiple_replace(txt, adict))
When I run this code in the Worksheet I get the error:
Error in lines 13-13
Traceback (most recent call last):
File "/projects/PROJECT/sagemathcloud/sage_server.py", line 873, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "", line 5, in multiple_replace
File "", line 4, in one_xlat
IndexError: no such group
If I run the same code on a Sagemath Cloud IPython Notebook, Windows/Linux Python 2.7 session, or Windows/Linux Python 3.4 session the code outputs the correct result.
I started debugging and found that Sage doesn't like the `'|'` in the compiled string. If I change the character to another (e.g. `','`) I no longer get the error ... though the code no longer functions with anything other than the `'|'`. I also tried to substitute `'|'` with `unichr(int('007c', 16))` which results in the same error.
Has anyone seen anything like this before? I tried to search for it, but it seems so niche I didn't find any results. Any ideas for a workaround?
Thanks
MattJesterEETue, 17 Feb 2015 07:29:24 +0100https://ask.sagemath.org/question/25854/MixedIntegerLinearProgram method .get_values() returns "IndexError: list index out of range"https://ask.sagemath.org/question/11053/mixedintegerlinearprogram-method-get_values-returns-indexerror-list-index-out-of-range/I'm writing an MLP to solve a scheduling problem for a set of jobs. I have several variable types, including family 's_ij', which is a boolean indicating if time i occurs before or after time j. My program works, and returns feasible solutions. I'm trying to see the assigned values for each variable, and while I can see the start time for each job, and some other families of booleans, when I try to display the values for the 's_ij' variables, I get an "IndexError". This is weird, since I'm letting the MILP package do all the work here. Here are relevant sections of the code, as well as the output:
from itertools import product
...
J = range(len(JOBS))
I = range(2*len(JOBS))
IXJ_copy = product(I, J)
...
for pair in IXJ_copy:
i = pair[0]
j = pair[1]
if i == len(I) - 1:
scheduling_mlp.add_constraint(s[(i, j)] <= 0)
else:
scheduling_mlp.add_constraint(M*(1-s[(i, j)]) + b[i+1] - t[j] - EPSILON >= 0)
scheduling_mlp.add_constraint(M*s[(i, j)] + t[j] - b[i+1] >= 0)
scheduling_mlp.add_constraint(M*e[(i, j)] + b[i] - t[j] - T(j) >= 0)
scheduling_mlp.add_constraint(M*(1-e[(i, j)]) + t[j] + T(j) - b[i] - EPSILON >= 0)
scheduling_mlp.add_constraint(M*(1-u[(i, j)]) + s[(i, j)] + e[(i, j)] >= 2)
scheduling_mlp.add_constraint(-M*(u[(i, j)]) + s[(i, j)] + e[(i, j)] <= 1)
...
print("s:")
start = scheduling_mlp.get_values(s)
print(start)
Returns
Traceback (most recent call last):
File "lp_solver.py", line 186, in <module>
start = scheduling_mlp.get_values(s)
File "mip.pyx", line 1161, in sage.numerical.mip.MixedIntegerLinearProgram.get_values (sage/numerical/mip.c:7098)
IndexError: list index out of range
As I said, when I run the code, I get feasible solutions back. I have the case handling in place while defining my constraints to prevent b[i+1] from going out of range. I'm able to get_values and print all other variables. Any ideas what's going wrong here?zbowenWed, 19 Feb 2014 06:45:52 +0100https://ask.sagemath.org/question/11053/