[Numpy-discussion] Using the C-API iterator with object arrays

2018-02-12 Thread Eugen Wintersberger
Hi there, 
I have a question concerning the numpy iterator C-API. I want to create
a numpy 
string array using NPY_OBJECT as a datatype for creating the array (the
reason I am going for this 
approach is that I do not know the length of the individual strings at
the time I construct 
the array, I only know its shape). The original strings are stored 
in a std::vector instance. The approach I took was something like
this


std::vector buffer = ;
NpyIter *iter = NpyIter_New(numpy_array,
NPY_ITER_READWRITE | NPY_ITER_C_INDEX | 
NPY_ITER_REFS_OK,
NPY_CORDER , NPY_NO_CASTING,nullptr);
if(iter==NULL)
{
  return;
}
NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(iter,nullptr);
if(iternext == NULL)
{
  std::cerr<<"Could not instantiate next iterator function"<

signature.asc
Description: This is a digitally signed message part
___
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Using the C-API iterator with object arrays

2018-02-12 Thread Jaime Fernández del Río
On Mon, Feb 12, 2018 at 12:13 PM Eugen Wintersberger <
eugen.wintersber...@gmail.com> wrote:

> Hi there,
> I have a question concerning the numpy iterator C-API. I want to create a
> numpy
> string array using NPY_OBJECT as a datatype for creating the array (the
> reason I am going for this
> approach is that I do not know the length of the individual strings at the
> time I construct
> the array, I only know its shape). The original strings are stored
> in a std::vector instance. The approach I took was something like
> this
>
> std::vector buffer = ;
> NpyIter *iter = NpyIter_New(numpy_array,
> NPY_ITER_READWRITE | NPY_ITER_C_INDEX | 
> NPY_ITER_REFS_OK,
> NPY_CORDER , NPY_NO_CASTING,nullptr);
> if(iter==NULL)
> {
>   return;
> }
> NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(iter,nullptr);
> if(iternext == NULL)
> {
>   std::cerr<<"Could not instantiate next iterator function"<   return;
> }
> PyObject **dataptr = (PyObject**)NpyIter_GetDataPtrArray(iter);
> for(auto string: buffer)
> {
>   dataptr[0] = PyString_FromSting(string); // this string construction 
> seem to work
>   iternext(iter);
> }
> NpyIter_Deallocate(iter);
>
>
> This code snippet is a bit stripped down with all the safety checks
> removed to make
> things more readable.
> However, the array I get back still contains only a None instance. Does
> anyone have an idea
> what I am doing wrong here?
>

I think you have the indirections wrong in dataptr?

NpyIter_GetDataPtrArray returns a char**, that hold the address of the
variable where the iterator holds the address of the first byte of the
current item being iterated.

When you write to dataptr[0] you are not writing to the array, but to where
the iterator holds the address of the first byte of the current item.

So you would have to write to dataptr[0][0], or **dataptr, to actually
affect the contents of the array. Of course, your dataptr being a PyObject**,
the compiler would probably complaint about such an assignment.

I think that if you define dataptr as a PyObject*** (yay, three star
programming !) and then assign to
**dataptr, everything will work fine. If that doesn't work, maybe try to
make dataptr a char**, and then assign to (PyObject *)(**dataptr) = ...

Jaime



> Thanks in advance.
>
> best regards
>Eugen
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>


-- 
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
___
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Using the C-API iterator with object arrays

2018-02-12 Thread Eric Moore
On Mon, Feb 12, 2018 at 6:13 AM, Eugen Wintersberger <
eugen.wintersber...@gmail.com> wrote:

> Hi there,
> I have a question concerning the numpy iterator C-API. I want to create a
> numpy
> string array using NPY_OBJECT as a datatype for creating the array (the
> reason I am going for this
> approach is that I do not know the length of the individual strings at the
> time I construct
> the array, I only know its shape). The original strings are stored
> in a std::vector instance. The approach I took was something like
> this
>
> std::vector buffer = ;
> NpyIter *iter = NpyIter_New(numpy_array,
> NPY_ITER_READWRITE | NPY_ITER_C_INDEX | 
> NPY_ITER_REFS_OK,
> NPY_CORDER , NPY_NO_CASTING,nullptr);
> if(iter==NULL)
> {
>   return;
> }
> NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(iter,nullptr);
> if(iternext == NULL)
> {
>   std::cerr<<"Could not instantiate next iterator function"<   return;
> }
> PyObject **dataptr = (PyObject**)NpyIter_GetDataPtrArray(iter);
> for(auto string: buffer)
> {
>   dataptr[0] = PyString_FromSting(string); // this string construction 
> seem to work
>   iternext(iter);
> }
> NpyIter_Deallocate(iter);
>
>
> This code snippet is a bit stripped down with all the safety checks
> removed to make
> things more readable.
> However, the array I get back still contains only a None instance. Does
> anyone have an idea
> what I am doing wrong here?
> Thanks in advance.
>
> best regards
>Eugen
>
>
Honestly, given that you're iterating over a single 1D array you've just
constructed, I don't think I would even bother trying to use the iterator.
For this case, a simple loop will be both concise and clear.

Eric
___
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] improving arange()? introducing fma()?

2018-02-12 Thread Chris Barker
I think it's all been said, but a few comments:

On Sun, Feb 11, 2018 at 2:19 PM, Nils Becker  wrote:

> Generating equidistantly spaced grids is simply not always possible.
>

exactly -- and linspace gives pretty much teh best possible result,
guaranteeing tha tthe start an end points are exact, and the spacing is
within an ULP or two (maybe we could make that within 1 ULP always, but not
sure that's worth it).


> The reason is that the absolute spacing of the possible floating point
> numbers depends on their magnitude [1].
>

Also that the exact spacing may not be exactly representable in FP -- so
you have to have at least one space that's a bit off to get the end points
right (or have the endpoints not exact).


> If you - for some reason - want the same grid spacing everywhere you may
> choose an appropriate new spacing.
>

well, yeah, but usually you are trying to fit to some other constraint. I'm
still confused as to where these couple of ULPs actually cause problems,
unless you are doing in appropriate FP comparisons elsewhere.

Curiously, either by design or accident, arange() seems to do something
> similar as was mentioned by Eric. It creates a new grid spacing by adding
> and subtracting the starting point of the grid. This often has similar
> effect as adding and subtracting N*dx (e.g. if the grid is symmetric around
> 0.0). Consequently, arange() seems to trade keeping the grid spacing
> constant for a larger error in the grid size and consequently in the end
> point.
>

interesting -- but it actually makes sense -- that is the definition of
arange(), borrowed from range(), which was designed for integers, and, in
fact, pretty much mirroered the classic C index for loop:


for (int i=0; i 1. Comparison to calculations with decimal can be difficult as not all
> simple decimal step sizes are exactly representable as
>
finite floating point numbers.
>

yeah, this is what I mean by inappropriate use of Decimal -- decimal is not
inherently "more accurate" than fp -- is just can represent _decimal_
numbers exactly, which we are all use to -- we want  1 / 10 to be exact,
but dont mind that 1 / 3 isn't.

Decimal also provided variable precision -- so it can be handy for that. I
kinda wish Python had an arbitrary precision binary floating point built
in...

-CHB

-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

chris.bar...@noaa.gov
___
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] @xoviat

2018-02-12 Thread Charles R Harris
Anyone know what happened to xoviat? He seems to have been disappeared.

Chuck
___
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] @xoviat

2018-02-12 Thread Ralf Gommers
On Mon, Feb 12, 2018 at 7:42 PM, Charles R Harris  wrote:

> Anyone know what happened to xoviat? He seems to have been disappeared.
>

His account was flagged by GitHub after scipy issue gh-8373. Flagging means
your profile, and all your issue comments, PRs, etc. are all made private.
It is quite bad if they don't come back; GitHub shouldn't break
conversations like that by removing comments on a different repo made well
prior to an incident. We'll be contacting GitHub about this.

Ralf
___
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion