On Sun, Nov 14, 2010 at 5:31 AM, Dima Pasechnik <dimp...@gmail.com> wrote:
>
>
> On Nov 14, 1:51 pm, William Stein <wst...@gmail.com> wrote:
>> Hi,
>>
>> This thread is about sum.
>>
>> 1. Dima said:
>>
>> > I don't think sum() method is needed. It's certainly a code bloat.
>>
>> This is a very, very misinformed comment. I want to address it,
>> especially the statement "It's certainly a code bloat."
>
> Sorry if I sounded too categorical on this.
> I suppose I meant two things:
>
> 1)  self.sum() on vectors is not too useful.
> A fast scalar product would be much more useful than .sum().
>
> 2) wanting a fast sum (or a scalar product), that works faster than
> Python's function sum(), yet does not require a handwritten code in
> every vector*.pyx, one seems to be hitting the limitation of Cython,
> which lacks generics.
>
> The code from TimeSeries:
>
>    cpdef double sum(self):
> ...
>        cdef double s = 0
>        cdef Py_ssize_t i
>        for i from 0 <= i < self._length:
>            s += self._values[i]
>        return s
>
> is totally generic, except the type declaration. If Cython had
> generics (aka templates) one would have written
>
>    cpdef T sum(self):
> ...
>        cdef T s = 0
>        cdef Py_ssize_t i
>        for i from 0 <= i < self._length:
>            s += self._values[i]
>        return s
>
> in the parent class of a vector class over T, rather than in the
> vector classes, for T=double, etc  etc themselves.
> (Perhaps something like this can still be accomplished within Sage,
> but I don't see how.)

There are at least two ways to do something generic with Cython to
solve the above problem.

1) You could use templates in Cython, which now come via the new C++ support:

     http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html

This requires a snippet of C++ that's put in a header file somewhere.
  I.e., right now you have to combine a bunch of Cython code with a
little C++ code.

2) One could use function pointers and/or typecasting in Cython (just
like you would in C), but this is potentially extra dangerous.

Though C++ does support generics well, try spend some time with the
Linbox source code (say) to see how dangerous (to readability) this
feature can be if not used sparingly.

 -- William

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to