public inbox for gsl-discuss@sourceware.org
 help / color / mirror / Atom feed
* increment a single element of matrix/vector
@ 2014-01-27 23:47 Patrick Alken
  2014-01-28  2:08 ` Rhys Ulerich
  0 siblings, 1 reply; 7+ messages in thread
From: Patrick Alken @ 2014-01-27 23:47 UTC (permalink / raw)
  To: gsl-discuss

I was thinking of adding two routines to GSL, to increment a single 
element of a matrix or vector by some amount, since I'm always doing 
things like:

gsl_vector_set(v, i, gsl_vector_get(v, i) + x);

which I think is ugly and annoying. Something like:

gsl_vector_add_element(v, i, x);
or
gsl_matrix_add_element(m, i, j, x);

would be really nice. Does anyone have any better suggestions for the 
function name? Or also is there another solution to this problem that I 
don't know about?

Patrick

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: increment a single element of matrix/vector
  2014-01-27 23:47 increment a single element of matrix/vector Patrick Alken
@ 2014-01-28  2:08 ` Rhys Ulerich
  2014-01-28  2:15   ` Rhys Ulerich
  0 siblings, 1 reply; 7+ messages in thread
From: Rhys Ulerich @ 2014-01-28  2:08 UTC (permalink / raw)
  To: Patrick Alken; +Cc: gsl-discuss

> I was thinking of adding two routines to GSL, to increment a single element
> of a matrix or vector by some amount, since I'm always doing things like:
>
> gsl_vector_set(v, i, gsl_vector_get(v, i) + x);
>
> which I think is ugly and annoying....

Agreed.  This has often bugged me.

> Or also is there another solution to this problem that I don't know about?

How about using

INLINE_DECL double * gsl_vector_ptr (gsl_vector * v, const size_t i);

to say *gsl_vector_ptr(v, i) += x ?  Never knew about this until I
just went fishing for it.

- Rhys

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: increment a single element of matrix/vector
  2014-01-28  2:08 ` Rhys Ulerich
@ 2014-01-28  2:15   ` Rhys Ulerich
  2014-01-28  3:06     ` Patrick Alken
  0 siblings, 1 reply; 7+ messages in thread
From: Rhys Ulerich @ 2014-01-28  2:15 UTC (permalink / raw)
  To: Patrick Alken; +Cc: gsl-discuss

> *gsl_vector_ptr(v, i) += x

Of course, make sure warnings about expressions without side effects
are turned on.  Otherwise it may be a long debugging session before
you discover a

    gsl_vector_ptr(v, i) += x

mistake for integer-like types.

- Rhys

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: increment a single element of matrix/vector
  2014-01-28  2:15   ` Rhys Ulerich
@ 2014-01-28  3:06     ` Patrick Alken
  2014-01-28 20:45       ` Patrick Alken
  0 siblings, 1 reply; 7+ messages in thread
From: Patrick Alken @ 2014-01-28  3:06 UTC (permalink / raw)
  To: gsl-discuss

On 01/27/2014 07:14 PM, Rhys Ulerich wrote:
>> *gsl_vector_ptr(v, i) += x
> 
> Of course, make sure warnings about expressions without side effects
> are turned on.  Otherwise it may be a long debugging session before
> you discover a
> 
>     gsl_vector_ptr(v, i) += x
> 
> mistake for integer-like types.
> 
> - Rhys
> 

Nice find - unfortunately since it returns a pointer to the element, you
need to do:

*(gsl_vector_ptr(v, i)) += x;

This could lead to trouble so its probably better to have a function
which does range checking, etc. I'll think a little more on a good way
to do this.

Thanks,
Patrick

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: increment a single element of matrix/vector
  2014-01-28  3:06     ` Patrick Alken
@ 2014-01-28 20:45       ` Patrick Alken
  2014-01-28 21:16         ` Rhys Ulerich
  0 siblings, 1 reply; 7+ messages in thread
From: Patrick Alken @ 2014-01-28 20:45 UTC (permalink / raw)
  To: gsl-discuss

The functions gsl_vector_inc and gsl_matrix_inc are now in the git 
repository to accomplish this

On 01/27/2014 08:06 PM, Patrick Alken wrote:
> On 01/27/2014 07:14 PM, Rhys Ulerich wrote:
>>> *gsl_vector_ptr(v, i) += x
>> Of course, make sure warnings about expressions without side effects
>> are turned on.  Otherwise it may be a long debugging session before
>> you discover a
>>
>>      gsl_vector_ptr(v, i) += x
>>
>> mistake for integer-like types.
>>
>> - Rhys
>>
> Nice find - unfortunately since it returns a pointer to the element, you
> need to do:
>
> *(gsl_vector_ptr(v, i)) += x;
>
> This could lead to trouble so its probably better to have a function
> which does range checking, etc. I'll think a little more on a good way
> to do this.
>
> Thanks,
> Patrick

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: increment a single element of matrix/vector
  2014-01-28 20:45       ` Patrick Alken
@ 2014-01-28 21:16         ` Rhys Ulerich
  2014-01-28 21:26           ` Patrick Alken
  0 siblings, 1 reply; 7+ messages in thread
From: Rhys Ulerich @ 2014-01-28 21:16 UTC (permalink / raw)
  To: Patrick Alken; +Cc: gsl-discuss

>>>> *gsl_vector_ptr(v, i) += x

>> *(gsl_vector_ptr(v, i)) += x;

Why the extra parens?  They hurt nothing, but the former statement
without them works just fine for me.  Unless I'm mistaken, the
function application happens before the dereference.  And the
dereference is much, much higher than anything like += or *= that the
user might want.

>> This could lead to trouble so its probably better to have a function
>> which does range checking, etc.

There is range checking in gsl_vector_ptr, so long as someone doesn't
start doing pointer arithmetic on its result.

Lastly, the pointer-based solution is nicely general: *(i > j ?
gsl_vector_ptr(v, i) : gsl_vector_ptr(j)) += 1

- Rhys

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: increment a single element of matrix/vector
  2014-01-28 21:16         ` Rhys Ulerich
@ 2014-01-28 21:26           ` Patrick Alken
  0 siblings, 0 replies; 7+ messages in thread
From: Patrick Alken @ 2014-01-28 21:26 UTC (permalink / raw)
  To: Rhys Ulerich; +Cc: gsl-discuss

On 01/28/2014 02:16 PM, Rhys Ulerich wrote:
>>>>> *gsl_vector_ptr(v, i) += x
>>> *(gsl_vector_ptr(v, i)) += x;
> Why the extra parens?  They hurt nothing, but the former statement
> without them works just fine for me.  Unless I'm mistaken, the
> function application happens before the dereference.  And the
> dereference is much, much higher than anything like += or *= that the
> user might want.

Yes sorry I didn't see the * the first time I read that
>>> This could lead to trouble so its probably better to have a function
>>> which does range checking, etc.
> There is range checking in gsl_vector_ptr, so long as someone doesn't
> start doing pointer arithmetic on its result.
>
> Lastly, the pointer-based solution is nicely general: *(i > j ?
> gsl_vector_ptr(v, i) : gsl_vector_ptr(j)) += 1
>
> - Rhys

Hmm ok you convinced me, I'll reverse those last few commits

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-01-28 21:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-27 23:47 increment a single element of matrix/vector Patrick Alken
2014-01-28  2:08 ` Rhys Ulerich
2014-01-28  2:15   ` Rhys Ulerich
2014-01-28  3:06     ` Patrick Alken
2014-01-28 20:45       ` Patrick Alken
2014-01-28 21:16         ` Rhys Ulerich
2014-01-28 21:26           ` Patrick Alken

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).