Discussion:
[Numpy-discussion] Fast vectorized arithmetic with ~32 significant digits under Numpy
Thomas Baruchel
2015-12-11 13:25:33 UTC
Permalink
From time to time it is asked on forums how to extend precision of computation on Numpy array. The most common answer
given to this question is: use the dtype=object with some arbitrary precision module like mpmath or gmpy.
See http://stackoverflow.com/questions/6876377/numpy-arbitrary-precision-linear-algebra or http://stackoverflow.com/questions/21165745/precision-loss-numpy-mpmath or http://stackoverflow.com/questions/15307589/numpy-array-with-mpz-mpfr-values

While this is obviously the most relevant answer for many users because it will allow them to use Numpy arrays exactly
as they would have used them with native types, the wrong thing is that from some point of view "true" vectorization
will be lost.

With years I got very familiar with the extended double-double type which has (for usual architectures) about 32 accurate
digits with faster arithmetic than "arbitrary precision types". I even used it for research purpose in number theory and
I got convinced that it is a very wonderful type as long as such precision is suitable.

I often implemented it partially under Numpy, most of the time by trying to vectorize at a low-level the libqd library.

But I recently thought that a very nice and portable way of implementing it under Numpy would be to use the existing layer
of vectorization on floats for computing the arithmetic operations by "columns containing half of the numbers" rather than
by "full numbers". As a proof of concept I wrote the following file: https://gist.github.com/baruchel/c86ed748939534d8910d

I converted and vectorized the Algol 60 codes from http://szmoore.net/ipdf/documents/references/dekker1971afloating.pdf
(Dekker, 1971).

A test is provided at the end; for inverting 100,000 numbers, my type is about 3 or 4 times faster than GMPY and almost
50 times faster than MPmath. It should be even faster for some other operations since I had to create another np.ones
array for testing this type because inversion isn't implemented here (which could of course be done). You can run this file by yourself
(maybe you will have to discard mpmath or gmpy if you don't have it).

I would like to discuss about the way to make available something related to that.

a) Would it be relevant to include that in Numpy ? (I would think to some "contribution"-tool rather than including it in
the core of Numpy because it would be painful to code all ufuncs; on the other hand I am pretty sure that many would be happy
to perform several arithmetic operations by knowing that they can't use cos/sin/etc. on this type; in other words, I am not
sure it would be a good idea to embed it as an every-day type but I think it would be nice to have it quickly available
in some way). If you agree with that, in which way should I code it (the current link only is a "proof of concept"; I would
be very happy to code it in some cleaner way)?

b) Do you think such attempt should remain something external to Numpy itself and be released on my Github account without being
integrated to Numpy?

Best regards,
--
Thomas Baruchel
Charles R Harris
2015-12-11 15:46:26 UTC
Permalink
Post by Thomas Baruchel
From time to time it is asked on forums how to extend precision of
computation on Numpy array. The most common answer
given to this question is: use the dtype=object with some arbitrary
precision module like mpmath or gmpy.
See
http://stackoverflow.com/questions/6876377/numpy-arbitrary-precision-linear-algebra
or http://stackoverflow.com/questions/21165745/precision-loss-numpy-mpmath
or
http://stackoverflow.com/questions/15307589/numpy-array-with-mpz-mpfr-values
While this is obviously the most relevant answer for many users because it
will allow them to use Numpy arrays exactly
as they would have used them with native types, the wrong thing is that
from some point of view "true" vectorization
will be lost.
With years I got very familiar with the extended double-double type which
has (for usual architectures) about 32 accurate
digits with faster arithmetic than "arbitrary precision types". I even
used it for research purpose in number theory and
I got convinced that it is a very wonderful type as long as such precision is suitable.
I often implemented it partially under Numpy, most of the time by trying
to vectorize at a low-level the libqd library.
But I recently thought that a very nice and portable way of implementing
it under Numpy would be to use the existing layer
of vectorization on floats for computing the arithmetic operations by
"columns containing half of the numbers" rather than
https://gist.github.com/baruchel/c86ed748939534d8910d
I converted and vectorized the Algol 60 codes from
http://szmoore.net/ipdf/documents/references/dekker1971afloating.pdf
(Dekker, 1971).
A test is provided at the end; for inverting 100,000 numbers, my type is
about 3 or 4 times faster than GMPY and almost
50 times faster than MPmath. It should be even faster for some other
operations since I had to create another np.ones
array for testing this type because inversion isn't implemented here
(which could of course be done). You can run this file by yourself
(maybe you will have to discard mpmath or gmpy if you don't have it).
I would like to discuss about the way to make available something related to that.
a) Would it be relevant to include that in Numpy ? (I would think to some
"contribution"-tool rather than including it in
the core of Numpy because it would be painful to code all ufuncs; on the
other hand I am pretty sure that many would be happy
to perform several arithmetic operations by knowing that they can't use
cos/sin/etc. on this type; in other words, I am not
sure it would be a good idea to embed it as an every-day type but I think
it would be nice to have it quickly available
in some way). If you agree with that, in which way should I code it (the
current link only is a "proof of concept"; I would
be very happy to code it in some cleaner way)?
b) Do you think such attempt should remain something external to Numpy
itself and be released on my Github account without being
integrated to Numpy?
I think astropy does something similar for time and dates. There has also
been some talk of adding a user type for ieee 128 bit doubles. I've looked
once for relevant code for the latter and, IIRC, the available packages
were GPL :(.

Chuck
Chris Barker - NOAA Federal
2015-12-11 16:16:04 UTC
Permalink
There has also been some talk of adding a user type for ieee 128 bit doubles. I've looked once for relevant code for the latter and, IIRC, the available packages were GPL :(.
This looks like it's BSD-Ish:

http://www.jhauser.us/arithmetic/SoftFloat.html

Don't know if it's any good....

CHB
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Anne Archibald
2015-12-11 16:22:55 UTC
Permalink
Actually, GCC implements 128-bit floats in software and provides them as
__float128; there are also quad-precision versions of the usual functions.
The Intel compiler provides this as well, I think, but I don't think
Microsoft compilers do. A portable quad-precision library might be less
painful.

The cleanest way to add extended precision to numpy is by adding a
C-implemented dtype. This can be done in an extension module; see the
quaternion and half-precision modules online.

Anne
Post by Charles R Harris
Post by Thomas Baruchel
From time to time it is asked on forums how to extend precision of
computation on Numpy array. The most common answer
given to this question is: use the dtype=object with some arbitrary
precision module like mpmath or gmpy.
See
http://stackoverflow.com/questions/6876377/numpy-arbitrary-precision-linear-algebra
or
http://stackoverflow.com/questions/21165745/precision-loss-numpy-mpmath
or
http://stackoverflow.com/questions/15307589/numpy-array-with-mpz-mpfr-values
While this is obviously the most relevant answer for many users because
it will allow them to use Numpy arrays exactly
as they would have used them with native types, the wrong thing is that
from some point of view "true" vectorization
will be lost.
With years I got very familiar with the extended double-double type which
has (for usual architectures) about 32 accurate
digits with faster arithmetic than "arbitrary precision types". I even
used it for research purpose in number theory and
I got convinced that it is a very wonderful type as long as such precision is suitable.
I often implemented it partially under Numpy, most of the time by trying
to vectorize at a low-level the libqd library.
But I recently thought that a very nice and portable way of implementing
it under Numpy would be to use the existing layer
of vectorization on floats for computing the arithmetic operations by
"columns containing half of the numbers" rather than
https://gist.github.com/baruchel/c86ed748939534d8910d
I converted and vectorized the Algol 60 codes from
http://szmoore.net/ipdf/documents/references/dekker1971afloating.pdf
(Dekker, 1971).
A test is provided at the end; for inverting 100,000 numbers, my type is
about 3 or 4 times faster than GMPY and almost
50 times faster than MPmath. It should be even faster for some other
operations since I had to create another np.ones
array for testing this type because inversion isn't implemented here
(which could of course be done). You can run this file by yourself
(maybe you will have to discard mpmath or gmpy if you don't have it).
I would like to discuss about the way to make available something related to that.
a) Would it be relevant to include that in Numpy ? (I would think to some
"contribution"-tool rather than including it in
the core of Numpy because it would be painful to code all ufuncs; on the
other hand I am pretty sure that many would be happy
to perform several arithmetic operations by knowing that they can't use
cos/sin/etc. on this type; in other words, I am not
sure it would be a good idea to embed it as an every-day type but I think
it would be nice to have it quickly available
in some way). If you agree with that, in which way should I code it (the
current link only is a "proof of concept"; I would
be very happy to code it in some cleaner way)?
b) Do you think such attempt should remain something external to Numpy
itself and be released on my Github account without being
integrated to Numpy?
I think astropy does something similar for time and dates. There has also
been some talk of adding a user type for ieee 128 bit doubles. I've looked
once for relevant code for the latter and, IIRC, the available packages
were GPL :(.
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
j***@gmail.com
2015-12-11 16:40:19 UTC
Permalink
Post by Anne Archibald
Actually, GCC implements 128-bit floats in software and provides them as
__float128; there are also quad-precision versions of the usual functions.
The Intel compiler provides this as well, I think, but I don't think
Microsoft compilers do. A portable quad-precision library might be less
painful.
The cleanest way to add extended precision to numpy is by adding a
C-implemented dtype. This can be done in an extension module; see the
quaternion and half-precision modules online.
Anne
Post by Charles R Harris
Post by Thomas Baruchel
From time to time it is asked on forums how to extend precision of
computation on Numpy array. The most common answer
given to this question is: use the dtype=object with some arbitrary
precision module like mpmath or gmpy.
See
http://stackoverflow.com/questions/6876377/numpy-arbitrary-precision-linear-algebra
or http://stackoverflow.com/questions/21165745/precision-loss-numpy-mpmath
or
http://stackoverflow.com/questions/15307589/numpy-array-with-mpz-mpfr-values
While this is obviously the most relevant answer for many users because
it will allow them to use Numpy arrays exactly
as they would have used them with native types, the wrong thing is that
from some point of view "true" vectorization
will be lost.
With years I got very familiar with the extended double-double type which
has (for usual architectures) about 32 accurate
digits with faster arithmetic than "arbitrary precision types". I even
used it for research purpose in number theory and
I got convinced that it is a very wonderful type as long as such precision is suitable.
I often implemented it partially under Numpy, most of the time by trying
to vectorize at a low-level the libqd library.
But I recently thought that a very nice and portable way of implementing
it under Numpy would be to use the existing layer
of vectorization on floats for computing the arithmetic operations by
"columns containing half of the numbers" rather than
https://gist.github.com/baruchel/c86ed748939534d8910d
I converted and vectorized the Algol 60 codes from
http://szmoore.net/ipdf/documents/references/dekker1971afloating.pdf
(Dekker, 1971).
A test is provided at the end; for inverting 100,000 numbers, my type is
about 3 or 4 times faster than GMPY and almost
50 times faster than MPmath. It should be even faster for some other
operations since I had to create another np.ones
array for testing this type because inversion isn't implemented here
(which could of course be done). You can run this file by yourself
(maybe you will have to discard mpmath or gmpy if you don't have it).
I would like to discuss about the way to make available something related to that.
a) Would it be relevant to include that in Numpy ? (I would think to some
"contribution"-tool rather than including it in
the core of Numpy because it would be painful to code all ufuncs; on the
other hand I am pretty sure that many would be happy
to perform several arithmetic operations by knowing that they can't use
cos/sin/etc. on this type; in other words, I am not
sure it would be a good idea to embed it as an every-day type but I think
it would be nice to have it quickly available
in some way). If you agree with that, in which way should I code it (the
current link only is a "proof of concept"; I would
be very happy to code it in some cleaner way)?
b) Do you think such attempt should remain something external to Numpy
itself and be released on my Github account without being
integrated to Numpy?
I think astropy does something similar for time and dates. There has also
been some talk of adding a user type for ieee 128 bit doubles. I've looked
once for relevant code for the latter and, IIRC, the available packages were
GPL :(.
This might be the same as or similar to a recent announcement for Julia

https://groups.google.com/d/msg/julia-users/iHTaxRVj1yM/M-WtZCedCQAJ


It would be useful to get this in a consistent way across platforms
and compilers.
I can think of several applications where higher precision reduce
operations would be
useful in statistics.
As Windows user, I never even saw a higher precision float.

Josef
Post by Anne Archibald
Post by Charles R Harris
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
David Cournapeau
2015-12-11 17:04:36 UTC
Permalink
Post by Anne Archibald
Actually, GCC implements 128-bit floats in software and provides them as
__float128; there are also quad-precision versions of the usual functions.
The Intel compiler provides this as well, I think, but I don't think
Microsoft compilers do. A portable quad-precision library might be less
painful.
The cleanest way to add extended precision to numpy is by adding a
C-implemented dtype. This can be done in an extension module; see the
quaternion and half-precision modules online.
We actually used __float128 dtype as an example of how to create a custom
dtype for a numpy C tutorial we did w/ Stefan Van der Walt a few years ago
at SciPy.

IIRC, one of the issue to make it more than a PoC was that numpy hardcoded
things like long double being the higest precision, etc... But that may has
been fixed since then.

David
Post by Anne Archibald
Anne
Post by Charles R Harris
Post by Thomas Baruchel
From time to time it is asked on forums how to extend precision of
computation on Numpy array. The most common answer
given to this question is: use the dtype=object with some arbitrary
precision module like mpmath or gmpy.
See
http://stackoverflow.com/questions/6876377/numpy-arbitrary-precision-linear-algebra
or
http://stackoverflow.com/questions/21165745/precision-loss-numpy-mpmath
or
http://stackoverflow.com/questions/15307589/numpy-array-with-mpz-mpfr-values
While this is obviously the most relevant answer for many users because
it will allow them to use Numpy arrays exactly
as they would have used them with native types, the wrong thing is that
from some point of view "true" vectorization
will be lost.
With years I got very familiar with the extended double-double type
which has (for usual architectures) about 32 accurate
digits with faster arithmetic than "arbitrary precision types". I even
used it for research purpose in number theory and
I got convinced that it is a very wonderful type as long as such precision is suitable.
I often implemented it partially under Numpy, most of the time by trying
to vectorize at a low-level the libqd library.
But I recently thought that a very nice and portable way of implementing
it under Numpy would be to use the existing layer
of vectorization on floats for computing the arithmetic operations by
"columns containing half of the numbers" rather than
https://gist.github.com/baruchel/c86ed748939534d8910d
I converted and vectorized the Algol 60 codes from
http://szmoore.net/ipdf/documents/references/dekker1971afloating.pdf
(Dekker, 1971).
A test is provided at the end; for inverting 100,000 numbers, my type is
about 3 or 4 times faster than GMPY and almost
50 times faster than MPmath. It should be even faster for some other
operations since I had to create another np.ones
array for testing this type because inversion isn't implemented here
(which could of course be done). You can run this file by yourself
(maybe you will have to discard mpmath or gmpy if you don't have it).
I would like to discuss about the way to make available something related to that.
a) Would it be relevant to include that in Numpy ? (I would think to
some "contribution"-tool rather than including it in
the core of Numpy because it would be painful to code all ufuncs; on the
other hand I am pretty sure that many would be happy
to perform several arithmetic operations by knowing that they can't use
cos/sin/etc. on this type; in other words, I am not
sure it would be a good idea to embed it as an every-day type but I
think it would be nice to have it quickly available
in some way). If you agree with that, in which way should I code it (the
current link only is a "proof of concept"; I would
be very happy to code it in some cleaner way)?
b) Do you think such attempt should remain something external to Numpy
itself and be released on my Github account without being
integrated to Numpy?
I think astropy does something similar for time and dates. There has also
been some talk of adding a user type for ieee 128 bit doubles. I've looked
once for relevant code for the latter and, IIRC, the available packages
were GPL :(.
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Anne Archibald
2015-12-12 19:15:43 UTC
Permalink
On Fri, Dec 11, 2015, 18:04 David Cournapeau <***@gmail.com> wrote:

On Fri, Dec 11, 2015 at 4:22 PM, Anne Archibald <***@astron.nl> wrote:

Actually, GCC implements 128-bit floats in software and provides them as
__float128; there are also quad-precision versions of the usual functions.
The Intel compiler provides this as well, I think, but I don't think
Microsoft compilers do. A portable quad-precision library might be less
painful.

The cleanest way to add extended precision to numpy is by adding a
C-implemented dtype. This can be done in an extension module; see the
quaternion and half-precision modules online.

We actually used __float128 dtype as an example of how to create a custom
dtype for a numpy C tutorial we did w/ Stefan Van der Walt a few years ago
at SciPy.

IIRC, one of the issue to make it more than a PoC was that numpy hardcoded
things like long double being the higest precision, etc... But that may has
been fixed since then.

I did some work on numpy's long-double support, partly to better understand
what would be needed to make quads work. The main obstacle is, I think, the
same: python floats are only 64-bit, and many functions are stuck passing
through them. It takes a lot of fiddling to make string conversions work
without passing through python floats, for example, and it takes some care
to produce scalars of the appropriate type. There are a few places where
you'd want to modify the guts of numpy if you had a higher precision
available than long doubles.

Anne

Nathaniel Smith
2015-12-11 17:45:35 UTC
Permalink
Post by Charles R Harris
Post by Thomas Baruchel
From time to time it is asked on forums how to extend precision of
computation on Numpy array. The most common answer
Post by Charles R Harris
Post by Thomas Baruchel
given to this question is: use the dtype=object with some arbitrary
precision module like mpmath or gmpy.
Post by Charles R Harris
Post by Thomas Baruchel
See
http://stackoverflow.com/questions/6876377/numpy-arbitrary-precision-linear-algebra
or http://stackoverflow.com/questions/21165745/precision-loss-numpy-mpmath
or
http://stackoverflow.com/questions/15307589/numpy-array-with-mpz-mpfr-values
Post by Charles R Harris
Post by Thomas Baruchel
While this is obviously the most relevant answer for many users because
it will allow them to use Numpy arrays exactly
Post by Charles R Harris
Post by Thomas Baruchel
as they would have used them with native types, the wrong thing is that
from some point of view "true" vectorization
Post by Charles R Harris
Post by Thomas Baruchel
will be lost.
With years I got very familiar with the extended double-double type
which has (for usual architectures) about 32 accurate
Post by Charles R Harris
Post by Thomas Baruchel
digits with faster arithmetic than "arbitrary precision types". I even
used it for research purpose in number theory and
Post by Charles R Harris
Post by Thomas Baruchel
I got convinced that it is a very wonderful type as long as such precision is suitable.
I often implemented it partially under Numpy, most of the time by trying
to vectorize at a low-level the libqd library.
Post by Charles R Harris
Post by Thomas Baruchel
But I recently thought that a very nice and portable way of implementing
it under Numpy would be to use the existing layer
Post by Charles R Harris
Post by Thomas Baruchel
of vectorization on floats for computing the arithmetic operations by
"columns containing half of the numbers" rather than
https://gist.github.com/baruchel/c86ed748939534d8910d
Post by Charles R Harris
Post by Thomas Baruchel
I converted and vectorized the Algol 60 codes from
http://szmoore.net/ipdf/documents/references/dekker1971afloating.pdf
Post by Charles R Harris
Post by Thomas Baruchel
(Dekker, 1971).
A test is provided at the end; for inverting 100,000 numbers, my type is
about 3 or 4 times faster than GMPY and almost
Post by Charles R Harris
Post by Thomas Baruchel
50 times faster than MPmath. It should be even faster for some other
operations since I had to create another np.ones
Post by Charles R Harris
Post by Thomas Baruchel
array for testing this type because inversion isn't implemented here
(which could of course be done). You can run this file by yourself
Post by Charles R Harris
Post by Thomas Baruchel
(maybe you will have to discard mpmath or gmpy if you don't have it).
I would like to discuss about the way to make available something related to that.
a) Would it be relevant to include that in Numpy ? (I would think to
some "contribution"-tool rather than including it in
Post by Charles R Harris
Post by Thomas Baruchel
the core of Numpy because it would be painful to code all ufuncs; on the
other hand I am pretty sure that many would be happy
Post by Charles R Harris
Post by Thomas Baruchel
to perform several arithmetic operations by knowing that they can't use
cos/sin/etc. on this type; in other words, I am not
Post by Charles R Harris
Post by Thomas Baruchel
sure it would be a good idea to embed it as an every-day type but I
think it would be nice to have it quickly available
Post by Charles R Harris
Post by Thomas Baruchel
in some way). If you agree with that, in which way should I code it (the
current link only is a "proof of concept"; I would
Post by Charles R Harris
Post by Thomas Baruchel
be very happy to code it in some cleaner way)?
b) Do you think such attempt should remain something external to Numpy
itself and be released on my Github account without being
Post by Charles R Harris
Post by Thomas Baruchel
integrated to Numpy?
I think astropy does something similar for time and dates. There has also
been some talk of adding a user type for ieee 128 bit doubles. I've looked
once for relevant code for the latter and, IIRC, the available packages
were GPL :(.

You're probably thinking of the __float128 support in gcc, which relies on
a LGPL (not GPL) runtime support library. (LGPL = any patches to the
support library itself need to remain open source, but no restrictions are
imposed on code that merely uses it.)

Still, probably something that should be done outside of numpy itself for
now.

-n
Charles R Harris
2015-12-11 17:51:06 UTC
Permalink
Post by Thomas Baruchel
Post by Charles R Harris
Post by Thomas Baruchel
From time to time it is asked on forums how to extend precision of
computation on Numpy array. The most common answer
Post by Charles R Harris
Post by Thomas Baruchel
given to this question is: use the dtype=object with some arbitrary
precision module like mpmath or gmpy.
Post by Charles R Harris
Post by Thomas Baruchel
See
http://stackoverflow.com/questions/6876377/numpy-arbitrary-precision-linear-algebra
or http://stackoverflow.com/questions/21165745/precision-loss-numpy-mpmath
or
http://stackoverflow.com/questions/15307589/numpy-array-with-mpz-mpfr-values
Post by Charles R Harris
Post by Thomas Baruchel
While this is obviously the most relevant answer for many users because
it will allow them to use Numpy arrays exactly
Post by Charles R Harris
Post by Thomas Baruchel
as they would have used them with native types, the wrong thing is that
from some point of view "true" vectorization
Post by Charles R Harris
Post by Thomas Baruchel
will be lost.
With years I got very familiar with the extended double-double type
which has (for usual architectures) about 32 accurate
Post by Charles R Harris
Post by Thomas Baruchel
digits with faster arithmetic than "arbitrary precision types". I even
used it for research purpose in number theory and
Post by Charles R Harris
Post by Thomas Baruchel
I got convinced that it is a very wonderful type as long as such
precision is suitable.
Post by Charles R Harris
Post by Thomas Baruchel
I often implemented it partially under Numpy, most of the time by
trying to vectorize at a low-level the libqd library.
Post by Charles R Harris
Post by Thomas Baruchel
But I recently thought that a very nice and portable way of
implementing it under Numpy would be to use the existing layer
Post by Charles R Harris
Post by Thomas Baruchel
of vectorization on floats for computing the arithmetic operations by
"columns containing half of the numbers" rather than
https://gist.github.com/baruchel/c86ed748939534d8910d
Post by Charles R Harris
Post by Thomas Baruchel
I converted and vectorized the Algol 60 codes from
http://szmoore.net/ipdf/documents/references/dekker1971afloating.pdf
Post by Charles R Harris
Post by Thomas Baruchel
(Dekker, 1971).
A test is provided at the end; for inverting 100,000 numbers, my type
is about 3 or 4 times faster than GMPY and almost
Post by Charles R Harris
Post by Thomas Baruchel
50 times faster than MPmath. It should be even faster for some other
operations since I had to create another np.ones
Post by Charles R Harris
Post by Thomas Baruchel
array for testing this type because inversion isn't implemented here
(which could of course be done). You can run this file by yourself
Post by Charles R Harris
Post by Thomas Baruchel
(maybe you will have to discard mpmath or gmpy if you don't have it).
I would like to discuss about the way to make available something
related to that.
Post by Charles R Harris
Post by Thomas Baruchel
a) Would it be relevant to include that in Numpy ? (I would think to
some "contribution"-tool rather than including it in
Post by Charles R Harris
Post by Thomas Baruchel
the core of Numpy because it would be painful to code all ufuncs; on
the other hand I am pretty sure that many would be happy
Post by Charles R Harris
Post by Thomas Baruchel
to perform several arithmetic operations by knowing that they can't use
cos/sin/etc. on this type; in other words, I am not
Post by Charles R Harris
Post by Thomas Baruchel
sure it would be a good idea to embed it as an every-day type but I
think it would be nice to have it quickly available
Post by Charles R Harris
Post by Thomas Baruchel
in some way). If you agree with that, in which way should I code it
(the current link only is a "proof of concept"; I would
Post by Charles R Harris
Post by Thomas Baruchel
be very happy to code it in some cleaner way)?
b) Do you think such attempt should remain something external to Numpy
itself and be released on my Github account without being
Post by Charles R Harris
Post by Thomas Baruchel
integrated to Numpy?
I think astropy does something similar for time and dates. There has
also been some talk of adding a user type for ieee 128 bit doubles. I've
looked once for relevant code for the latter and, IIRC, the available
packages were GPL :(.
You're probably thinking of the __float128 support in gcc, which relies on
a LGPL (not GPL) runtime support library. (LGPL = any patches to the
support library itself need to remain open source, but no restrictions are
imposed on code that merely uses it.)
Still, probably something that should be done outside of numpy itself for
now.
No, there are several other software packages out there. I know of the gcc
version, but was looking for something more portable.

Chuck
Eric Moore
2015-12-11 20:17:57 UTC
Permalink
I have a mostly complete wrapping of the double-double type from the QD
library (http://crd-legacy.lbl.gov/~dhbailey/mpdist/) into a numpy dtype.
The real problem is, as david pointed out, user dtypes aren't quite full
equivalents of the builtin dtypes. I can post the code if there is
interest.

Something along the lines of what's being discussed here would be nice,
since the extended type is subject to such variation.

Eric

On Fri, Dec 11, 2015 at 12:51 PM, Charles R Harris <
Post by Charles R Harris
Post by Thomas Baruchel
Post by Charles R Harris
Post by Thomas Baruchel
From time to time it is asked on forums how to extend precision of
computation on Numpy array. The most common answer
Post by Charles R Harris
Post by Thomas Baruchel
given to this question is: use the dtype=object with some arbitrary
precision module like mpmath or gmpy.
Post by Charles R Harris
Post by Thomas Baruchel
See
http://stackoverflow.com/questions/6876377/numpy-arbitrary-precision-linear-algebra
or
http://stackoverflow.com/questions/21165745/precision-loss-numpy-mpmath
or
http://stackoverflow.com/questions/15307589/numpy-array-with-mpz-mpfr-values
Post by Charles R Harris
Post by Thomas Baruchel
While this is obviously the most relevant answer for many users
because it will allow them to use Numpy arrays exactly
Post by Charles R Harris
Post by Thomas Baruchel
as they would have used them with native types, the wrong thing is
that from some point of view "true" vectorization
Post by Charles R Harris
Post by Thomas Baruchel
will be lost.
With years I got very familiar with the extended double-double type
which has (for usual architectures) about 32 accurate
Post by Charles R Harris
Post by Thomas Baruchel
digits with faster arithmetic than "arbitrary precision types". I even
used it for research purpose in number theory and
Post by Charles R Harris
Post by Thomas Baruchel
I got convinced that it is a very wonderful type as long as such
precision is suitable.
Post by Charles R Harris
Post by Thomas Baruchel
I often implemented it partially under Numpy, most of the time by
trying to vectorize at a low-level the libqd library.
Post by Charles R Harris
Post by Thomas Baruchel
But I recently thought that a very nice and portable way of
implementing it under Numpy would be to use the existing layer
Post by Charles R Harris
Post by Thomas Baruchel
of vectorization on floats for computing the arithmetic operations by
"columns containing half of the numbers" rather than
https://gist.github.com/baruchel/c86ed748939534d8910d
Post by Charles R Harris
Post by Thomas Baruchel
I converted and vectorized the Algol 60 codes from
http://szmoore.net/ipdf/documents/references/dekker1971afloating.pdf
Post by Charles R Harris
Post by Thomas Baruchel
(Dekker, 1971).
A test is provided at the end; for inverting 100,000 numbers, my type
is about 3 or 4 times faster than GMPY and almost
Post by Charles R Harris
Post by Thomas Baruchel
50 times faster than MPmath. It should be even faster for some other
operations since I had to create another np.ones
Post by Charles R Harris
Post by Thomas Baruchel
array for testing this type because inversion isn't implemented here
(which could of course be done). You can run this file by yourself
Post by Charles R Harris
Post by Thomas Baruchel
(maybe you will have to discard mpmath or gmpy if you don't have it).
I would like to discuss about the way to make available something
related to that.
Post by Charles R Harris
Post by Thomas Baruchel
a) Would it be relevant to include that in Numpy ? (I would think to
some "contribution"-tool rather than including it in
Post by Charles R Harris
Post by Thomas Baruchel
the core of Numpy because it would be painful to code all ufuncs; on
the other hand I am pretty sure that many would be happy
Post by Charles R Harris
Post by Thomas Baruchel
to perform several arithmetic operations by knowing that they can't
use cos/sin/etc. on this type; in other words, I am not
Post by Charles R Harris
Post by Thomas Baruchel
sure it would be a good idea to embed it as an every-day type but I
think it would be nice to have it quickly available
Post by Charles R Harris
Post by Thomas Baruchel
in some way). If you agree with that, in which way should I code it
(the current link only is a "proof of concept"; I would
Post by Charles R Harris
Post by Thomas Baruchel
be very happy to code it in some cleaner way)?
b) Do you think such attempt should remain something external to Numpy
itself and be released on my Github account without being
Post by Charles R Harris
Post by Thomas Baruchel
integrated to Numpy?
I think astropy does something similar for time and dates. There has
also been some talk of adding a user type for ieee 128 bit doubles. I've
looked once for relevant code for the latter and, IIRC, the available
packages were GPL :(.
You're probably thinking of the __float128 support in gcc, which relies
on a LGPL (not GPL) runtime support library. (LGPL = any patches to the
support library itself need to remain open source, but no restrictions are
imposed on code that merely uses it.)
Still, probably something that should be done outside of numpy itself for
now.
No, there are several other software packages out there. I know of the gcc
version, but was looking for something more portable.
Chuck
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Sturla Molden
2015-12-12 18:02:37 UTC
Permalink
Post by Thomas Baruchel
While this is obviously the most relevant answer for many users because
it will allow them to use Numpy arrays exactly
as they would have used them with native types, the wrong thing is that
from some point of view "true" vectorization
will be lost.
What does "true vectorization" mean anyway?


Sturla
Marten van Kerkwijk
2015-12-12 19:10:13 UTC
Permalink
Hi All,

astropy `Time` indeed using two doubles internally, but is very limited in
the operations it allows: essentially only addition/subtraction, and
multiplication with/division by a normal double.

It would be great to have better support within numpy; it is a pity to have
a float128 type that does not provide the full associated precision.

All the best,

Marten
Post by Sturla Molden
Post by Thomas Baruchel
While this is obviously the most relevant answer for many users because
it will allow them to use Numpy arrays exactly
as they would have used them with native types, the wrong thing is that
from some point of view "true" vectorization
will be lost.
What does "true vectorization" mean anyway?
Sturla
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Elliot Hallmark
2015-12-12 19:12:34 UTC
Permalink
Post by Sturla Molden
What does "true vectorization" mean anyway?
Calling python functions on python objects in a for loop is not really
vectorized. It's much slower than people intend when they use numpy.

Elliot
Loading...