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, 28 Jun 2021 21:17:46 +0200cannot convert log(x)/log(2) to inthttps://ask.sagemath.org/question/57798/cannot-convert-logxlog2-to-int/If I define `J(x)` function as:
def J(x):
sum = 0
print(int(log(x)/log(2)))
for i in range(1, int(log(x)/log(2)) + 1):
sum = sum + 1 / i * prime_pi(x**(1/i))
return(N(sum))
when I try to plot it using:
p = plot(J(x), (x, 1, 20))
show(p)
I get the message:
cannot convert log(x)/log(2) to int
but if try `J` function acting on any float number I get a perfect value like:
sage: J(123.456789)
34.0333333333333
I have tried to replace int by floor but with the same result.
Any help to solve the plotting problem will be welcome. Thank you.zplotMon, 28 Jun 2021 21:17:46 +0200https://ask.sagemath.org/question/57798/TypeError: 'int' object is not iterablehttps://ask.sagemath.org/question/9125/typeerror-int-object-is-not-iterable/Hello,
For a given matrix M (m by n) and vector V of size n, my goal is to do the following:
1. Take the $\text{i}^{\text{th}}$ column of M and convert them as keywords for a dictionary and associate the keywords to the $\text{i}^{\text{th}}$ value of the vector V.
2. I do (1) in MatVec(M,V) in the code below by creating a dictionary for each keyword and value. Finally I try to merge all the dictionaries.
Here is my code:
def DictionaryMerge(*args):
import collections
super_dict = collections.defaultdict(set)
for d in args:
for k, v in d.iteritmes():
super_dict[k].add(v)
return super_dict
def MatVec(M,V):
from sets import Set
Vec = V.list()
MatCols = M.ncols()
D2 = {}
for i in range(MatCols):
ColumnVec = M.column(i).list()
CVec = list()
for k in range(len(ColumnVec)):
CVec.append(int(ColumnVec[k]))
for j in CVec:
dict = {j:Set(int(Vec[i]))}
D2 = DictionaryMerge(dict,D2)
return dict
M = matrix([[(i+1)*(j+2) for i in range(5)] for j in range(6)])
V = vector([int(i+100) for i in range(5)])
MatVec(M,V)
The error I get is:
Traceback (most recent call last): return super_dict
File "", line 1, in <module>
File "/tmp/tmp4m6HiR/___code___.py", line 29, in <module>
exec compile(u'MatVec(M,V)
File "", line 1, in <module>
File "/tmp/tmp4m6HiR/___code___.py", line 22, in MatVec
dict = {j:Set(int(Vec[i]))}
File "/home/usr111/sage/local/lib/python/sets.py", line 414, in __init__
self._update(iterable)
File "/home/usr111/sage/local/lib/python/sets.py", line 368, in _update
for element in iterable:
TypeError: 'int' object is not iterable
I am not so sure why I am getting this error. MaelstromYamatoTue, 03 Jul 2012 13:03:11 +0200https://ask.sagemath.org/question/9125/Converting string into number does not work properlyhttps://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/Here is an example with huge integer number "n".
n=2^7000000;
Sage can convert huge integer number into string very quickly:
s=str(n);
But the other way, i.e. the same huge string converting back into integer number is very slow:
m=int(s);
It should be done in approximately same time as variables "m", "n", "s" are of the same length.
I also tried to use eval(s) and sage_eval(s) instead of int(s) and all are very slow.
Mathematica can do all the above operation much more quickly than Sage.
Do you know, where the problem is?
Is there some other function besides "int", "eval" and "sage_eval" that can do it faster?azerbajdzanWed, 06 Jun 2012 08:02:20 +0200https://ask.sagemath.org/question/9034/Interpreting ints as intshttps://ask.sagemath.org/question/8663/interpreting-ints-as-ints/I am working with a python package called Theano, which has a function called "function" which basically takes in the symbolic representation of a function and compiles it for fast execution on a block of memory either on a CPU or a GPU. When trying to use it in sage, however, it gets a bit flummoxed by the fact that integers and such are interpreted as sage integers, rather than python basic types. Is there a way to (at runtime) turn off that part of the interpreter, so that code doesn't have to be rewritten to work in sage?kartikvMon, 23 Jan 2012 13:01:00 +0100https://ask.sagemath.org/question/8663/Abstract base classeshttps://ask.sagemath.org/question/8352/abstract-base-classes/Python has a module named abc for implementing "abstract base classes", see http://www.python.org/dev/peps/pep-3119/ for definitions and usage. Why is it not used in Sage ?
Example: if type(i) is 'int', isinstance (i, Integer) should be True (today it's False).BétrémaFri, 30 Sep 2011 14:12:06 +0200https://ask.sagemath.org/question/8352/Cannot convert int to sage.rings.integer.Integerhttps://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/I created a minimal example in the file bad.py
from sage.all import *
j = 0
m = matrix(3,3)
m.insert_row(j, [1,1,1])
Then from sage:
sage: load bad.py
TypeError: Cannot convert int to sage.rings.integer.Integer
I think I can see an easy workaround, I can wrap all the 1's in Integer() (or change the name of the file to .sage so the preparser does it), but why is that necessary? Is this a bug? Why would sage not be able to convert int to Integer? Isn't that a pretty obvious conversion?
paragonMon, 02 May 2011 14:23:12 +0200https://ask.sagemath.org/question/8099/