How to use qflll() in the PARI library? - python

I wanted to use the function qflll from the PARI library in python, so I downloaded pari-python-cygwin-0.1.zip, however when I attempted to use qflll in python, i.e.
qflll([[1,0,0],[0,1,0],[0,0,1]])
I got this error message
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Too few parameters provided: 1
So I how do I invoke the function qflll in python properly without any error?

As you can see in these docs, the qflll function takes a PARI matrix as input. Therefore, you have to do something like:
sage: M = Matrix([[1,0,0],[0,1,0],[0,0,1]])
sage: p = pari(M)
sage: p.qflll()
[1, 0, 0; 0, 1, 0; 0, 0, 1]
Or, if you prefer, one sentence:
sage: pari(Matrix([[1,0,0],[0,1,0],[0,0,1]])).qflll()
[1, 0, 0; 0, 1, 0; 0, 0, 1]

Related

iterating over non-interpolated Array of symbols python equivalent

I am currently writing script in python from a ruby module. I am having trouble with this aspect of the translation of ruby to python.
Ruby:
plan_metrics[test_name]={ passed_count: 0, blocked_count: 0, untested_count: 0, failed_count: 0, reviewed_count: 0, test_harness_issue_count: 0, bug_failure_count: 0, defect_list: [] }
entry['runs'].each do |run|
metric_hash = plan_metrics[test_name]
%i[passed_count blocked_count untested_count failed_count].each do |key|
metric_hash[key] = metric_hash[key] + run[key.to_s]
end
In this code, entry['runs'] holds the actual values of passed_count, blocked_count, untested_count, and failed_count, but in multiple dictionaries. This is supposed to iterate over them and add up all the values and put them into ONE symbol (i.e passed_count) that is held in metric_hash
Now when i try to translate into python, i am not using symbols but instead doing it like this
My Python translation:
plan_metrics[test_name]={ "passed_count": 0, "blocked_count": 0, "untested_count": 0, "failed_count": 0, "reviewed_count": 0, "test_harness_issue_count": 0, "bug_failure_count": 0, "defect_list": [] }
for run in entry["runs"]:
metric_hash = plan_metrics[test_name]
for key in [metric_hash["passed_count"], metric_hash["blocked_count"], metric_hash["untested_count"], metric_hash["failed_count"]:
metric_hash[key] = metric_hash[key] + run[str(key)]
But for this i am getting KeyError: 0 on line metric_hash[key] = metric_hash[key] + run[str(key)]
would
for key in [metric_hash["passed_count"], metric_hash["blocked_count"], metric_hash["untested_count"], metric_hash["failed_count"]:
be the proper equivalent of
%i[passed_count blocked_count untested_count failed_count].each do |key|
and if so what is causing the KeyError: 0?
if not how can i accomplish what the ruby example did, with interating over array of symbols, in python
If you need more information on the data, letme know what to print() thanks
In python you do
for key in [metric_hash["passed_count"], metric_hash["blocked_count"], metric_hash["untested_count"], metric_hash["failed_count"]:
That means that key takes values from a list [0, 0, 0, 0]. Do you see why?

Unsized object error with numpy.random.permutation?

I have some code right now that is getting stuck on one line:
perm = numpy.random.permutation(128)
To which it give the following error: "TypeError: len() of unsized object." I can't figure out what the issue is since 128 is just an integer. I see that this is a problem that has probably been resolved before here: http://mail.scipy.org/pipermail/numpy-discussion/2007-January/025592.html but their solution isn't helpful to me since it is about floats.
Can anyone see what's going wrong here?
In Sage, the input is preparsed by the Sage preparser.
I'll use 12 instead of 128 so the examples fit in one line.
When you input the following:
sage: import numpy
sage: perm = numpy.random.permutation(12)
The error message you get looks like:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-38b6a5e3e889> in <module>()
----> 1 perm = numpy.random.permutation(Integer(12))
/opt/sage/local/lib/python2.7/site-packages/numpy/random/mtrand.so in mtrand.RandomState.permutation (numpy/random/mtrand/mtrand.c:21297)()
/opt/sage/local/lib/python2.7/site-packages/numpy/random/mtrand.so in mtrand.RandomState.shuffle (numpy/random/mtrand/mtrand.c:20965)()
TypeError: len() of unsized object
where you see in particular the line:
----> 1 perm = numpy.random.permutation(Integer(12))
telling you that your input
perm = numpy.random.permutation(12)
was preparsed to
perm = numpy.random.permutation(Integer(12))
However numpy is not so happy being fed a Sage Integer,
it would prefer a Python int.
The simplest way to input a raw Python integer is to append r to it:
sage: perm = numpy.random.permutation(12r)
This will work for you:
sage: perm = numpy.random.permutation(12r)
sage: perm # random
array([ 9, 0, 11, 4, 2, 10, 3, 5, 7, 6, 1, 8])
Another way is to let Sage transform the Python int to a Sage Integer but then force it to convert it back to a Python integer:
sage: perm = numpy.random.permutation(int(12))
sage: perm # random
array([ 5, 9, 1, 7, 0, 2, 10, 6, 3, 8, 4, 11])
Another thing you could do is to turn off the Sage preparser.
sage: preparser(False)
sage: perm = numpy.random.permutation(12)
sage: perm # random
array([ 0, 2, 7, 5, 8, 11, 1, 6, 9, 10, 3, 4])

How do I select components from a particular list of indices of a Python vector?

So here I have x1vals:
>>> x1Vals
[-0.33042515829906227, -0.1085082739900165, 0.93708611747433213, -0.19289496973017362, -0.94365384912207761, 0.43385903975568652, -0.46061140566051262, 0.82767432358782367, -0.24257307936591843, -0.1182761514447952, -0.29794617763330011, -0.87410892638408, -0.34732294121174467, 0.40646145339571249, -0.64082861589870865, -0.45680189916940073, 0.4688889876175073, -0.89399689430691298, 0.53549621114138612]
And here is the list of x1Vals indices that I want to select
>>> np.where(np.dot(XValsOnly,newweights) > 0)
>>>(array([ 1, 2, 4, 5, 6, 8, 9, 13, 15, 16]),)
But when I try to get the values of x1Vals the Matlab way, I get this error:
>>> x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)]
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
x1Vals[np.where(np.dot(XValsOnly,newweights) > 0)]
TypeError: list indices must be integers, not tuple
>>> np.where(np.dot(XValsOnly,newweights) > 0)
Is there a way around this?
The problem is that your x1Vals is a list object, which does not support fancy indexing. You just have to build an array out of it:
x1Vals = np.array(x1Vals)
and your approach will work.
A faster approach would be to use np.take:
np.take(x1Vals, np.where(np.dot(XValsOnly,newweights) > 0))

Theano: Why does indexing fail in this case?

I'm trying to get the max of a vector given a boolean value.
With Numpy:
>>> this = np.arange(10)
>>> this[~(this>=5)].max()
4
But with Theano:
>>> that = T.arange(10, dtype='int32')
>>> that[~(that>=5)].max().eval()
9
>>> that[~(that>=5).nonzero()].max().eval()
Traceback (most recent call last):
File "<pyshell#146>", line 1, in <module>
that[~(that>=5).nonzero()].max().eval()
AttributeError: 'TensorVariable' object has no attribute 'nonzero'
Why does this happen? Is this a subtle nuance that i'm missing?
You are using a version of Theano that is too old. In fact, tensor_var.nonzero() isn't in any released version. You need to update to the development version.
With the development version I have this:
>>> that[~(that>=5).nonzero()].max().eval()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary ~: 'tuple'
This is because you are missing parenthesis in your line. Here is the good line:
>>> that[(~(that>=5)).nonzero()].max().eval()
array(9, dtype=int32)
But we still have unexpected result! The problem is that Theano do not support bool. Doing ~ on int8, is doing the bitwise invert on 8 bits, not 1 bit. It give this result:
>>> (that>=5).eval()
array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype=int8)
>>> (~(that>=5)).eval()
array([-1, -1, -1, -1, -1, -2, -2, -2, -2, -2], dtype=int8)
You can remove the ~ with this:
>>> that[(that<5).nonzero()].max().eval()
array(4, dtype=int32)

How do I fix this error? TypeError: 'str' does not support the buffer interface

>>> import struct
>>> s = '\x00\x00\x00\x01\x00\x00\x00\xff\xff\x00\x00'
>>> struct.unpack('11B', s)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
struct.unpack('11B', s)
TypeError: 'str' does not support the buffer interface
What is wrong with this? Please help.
On python 3, struct.unpack() expects an object that implements the buffer protocol, such as a bytes value, not a unicode str:
>>> import struct
>>> s = b'\x00\x00\x00\x01\x00\x00\x00\xff\xff\x00\x00'
>>> struct.unpack('11B', s)
(0, 0, 0, 1, 0, 0, 0, 255, 255, 0, 0)
If you are reading this data from a file, open the file in binary mode instead of text mode to get bytes.

Categories

Resources