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.Wed, 10 Mar 2021 22:48:57 +0100difference Python and Sage - integer and rational numberhttps://ask.sagemath.org/question/56110/difference-python-and-sage-integer-and-rational-number/in Python, if k is an integer, equation k / 4 returns the integer part of k over 4, while in Sage, it returns a rational.
How to work in Sage and change from result od divide from rational to like in Python ?
example: python
5 divide by 3 = 1
5 divide by 2 = 2
4 divide by 2 = 2MiroslawWed, 10 Mar 2021 22:48:57 +0100https://ask.sagemath.org/question/56110/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/When does 1/2=0 ? (python's integer division Vs Sage's exact fractions)https://ask.sagemath.org/question/7917/when-does-120-pythons-integer-division-vs-sages-exact-fractions/Hi all !
I know that python evaluates 1/2 to zero because it is making an integer division. Well. I was expecting Sage not to do so.
It does more or less ...
------ sagess.py ----------
#! /usr/bin/sage -python
# -*- coding: utf8 -*-
from sage.all import *
def MyFunction(x,y):
print x/y
MyFunction(1,2)
------ end of sagess.py ----------
When I launche that script from the command line, I got what Python would give :
18:08:03 ~/script >$ ./sagess.py
0
But when I launch it from the sage'terminal it is less clear how it works :
18:16:04 ~/script >$ sage
----------------------------------------------------------------------
| Sage Version 4.5.3, Release Date: 2010-09-04 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import sagess
0
sage: sagess.MyFunction(1,2)
1/2
At the import, it returns zero, but when I reask the same computation, it gives the correct 1/2
I know that writing
float(x)/y
"fixes" the problem. But it looks weird and I want to keep exact values.
What do I have to write in my scripts in order to make understand to Sage that I always want 1/2 to be 1/2 ?
Thanks
Have a good night !
Laurent
Laurent ClaessensTue, 01 Feb 2011 12:24:55 +0100https://ask.sagemath.org/question/7917/