ValArray

J

jacob navia

In the context of the C Containers library I am now implementing "ValArray"

These containers store the basic numeric types (integers and floats)
of the language.

They support the basic operations that can be done with those types.

The question is:

ValArray A,B,C;

// ...

C = iValArrayDouble.SumTo(B,A);

This is the equivalent of B += A in C++.

The question is, what happens when Size(A) != Size(B). C++
leaves that as "undefined", probably crashing. My library
throws an error and does nothing.

An alternative solution would be to use the length of B for the
operation in question, extending the shorter array if necessary
or discarding results that go beyond the length of B.

Operation Identity elem.
+ 0
- 0
* 1
/ 1
& 1
| 0
% 1 (for integers only)
Xor ????

What do you think?

Which alternative is better?

jacob
 
M

Mark Bluemel

In the context of the C Containers library I am now implementing "ValArray"

These containers store the basic numeric types (integers and floats)
of the language.

They support the basic operations that can be done with those types.

The question is:

ValArray A,B,C;

// ...

C = iValArrayDouble.SumTo(B,A);

This is the equivalent of B += A in C++.

The question is, what happens when Size(A) != Size(B). C++
leaves that as "undefined", probably crashing. My library
throws an error and does nothing.

An alternative solution would be to use the length of B for the
operation in question, extending the shorter array if necessary
or discarding results that go beyond the length of B.

Operation Identity elem.
+ 0
- 0
* 1
/ 1
& 1
| 0
% 1 (for integers only)
Xor ????

What do you think?

Which alternative is better?

My immediate reaction, backed up by a quick look at the discussion of
array arithmetic in MATLAB (just happened to find it early in Google),
is to favour the former approach, with a separate set of operations for
the case where you need to perform maths using an array and a single
value (increment all elements by 1, calculate VAT at 20% on all
elements, etc.).

The implicit extension approach seems more likely to lead to errors.
 
B

Ben Bacarisse

jacob navia said:
In the context of the C Containers library I am now implementing "ValArray"

These containers store the basic numeric types (integers and floats)
of the language.

They support the basic operations that can be done with those types.

The question is:

ValArray A,B,C;

// ...

C = iValArrayDouble.SumTo(B,A);

This is the equivalent of B += A in C++.

This confuses me. What's C? If it means B += A, there would be no
third sequence.
The question is, what happens when Size(A) != Size(B). C++
leaves that as "undefined", probably crashing. My library
throws an error and does nothing.

An alternative solution would be to use the length of B for the
operation in question, extending the shorter array if necessary
or discarding results that go beyond the length of B.

Operation Identity elem.
+ 0
- 0
* 1
/ 1
& 1
| 0
% 1 (for integers only)
Xor ????

The identity for &, | and Xor is -1 converted to the width of whatever
integer type is involved. However, I may have missed something you are
getting at because Xor is written ^ and I'd expect the restriction to
apply to other operations and not just %. The identity for % is not 1.
There are lots of identities for % (at least for positive integers) but
none representable in the type of the operand since. For some type T,
one choice would be Tmax + 1.

If these are listed in order to show how to extend a short array, why
not just say that the elements of B beyond the length of A remain
unchanged?
What do you think?

I'd look into providing a function to do the work rather than have SumTo
and MulTo and all the rest. For one thing, how can you know what
operations people will want to perform?

[By the way, it is likely this post will duplicate what others have
written by now, but I forgot to send it before getting distracted by
other tasks and I don't want to waste the typing so I will post it
without my usual check.]

<snip>
 
J

jacob navia

Le 12/04/11 18:46, Ben Bacarisse a écrit :
This confuses me. What's C? If it means B += A, there would be no
third sequence.

Yes, C is unnecessary. Sorry.


[snip]
If these are listed in order to show how to extend a short array, why
not just say that the elements of B beyond the length of A remain
unchanged?

Of course, but I thought that I should justify that mathematically.
I'd look into providing a function to do the work rather than have SumTo
and MulTo and all the rest. For one thing, how can you know what
operations people will want to perform?

I provide "Apply" that will call a function for each
element. But that would be overkill for a simple addition.

The point of ValArray is to speed up vector operations by providing
a nonaliased array. If we would call a function to do a simple
addition that would kill performance.
[By the way, it is likely this post will duplicate what others have
written by now, but I forgot to send it before getting distracted by
other tasks and I don't want to waste the typing so I will post it
without my usual check.]

<snip>

Thanks for your input.
 
J

jacob navia

Le 12/04/11 16:18, Mark Bluemel a écrit :
My immediate reaction, backed up by a quick look at the discussion of
array arithmetic in MATLAB (just happened to find it early in Google),
is to favour the former approach, with a separate set of operations for
the case where you need to perform maths using an array and a single
value (increment all elements by 1, calculate VAT at 20% on all
elements, etc.).


True. I will provide two functions

Array * array
and
Array * scalar
The implicit extension approach seems more likely to lead to errors.

For all binary operations I will provide a scalar variant. Thanks.

For the time being I have a "strict" approach: arrays must be of the
same length. If they are not I raise a signal (instead of crashing like
the STL does now)

But I wanted to explore other possibilities

Thanks for your input
 
K

Keith Thompson

jacob navia said:
In the context of the C Containers library I am now implementing "ValArray"

These containers store the basic numeric types (integers and floats)
of the language.

They support the basic operations that can be done with those types.

The question is:

ValArray A,B,C;

// ...

C = iValArrayDouble.SumTo(B,A);

This is the equivalent of B += A in C++.

The question is, what happens when Size(A) != Size(B). C++
leaves that as "undefined", probably crashing. My library
throws an error and does nothing.

What exactly does "throws an error" mean?
An alternative solution would be to use the length of B for the
operation in question, extending the shorter array if necessary
or discarding results that go beyond the length of B.

Or you could require the sizes to be equal, and provide a separate
function that extends or truncates the result as needed, assuming that
behavior is sufficiently useful. (I have no particular opinion on
whether it is.)
 
J

jacob navia

Le 12/04/11 22:20, Keith Thompson a écrit :
What exactly does "throws an error" mean?

The library calls an error function (that can be replaced by the user)
The default error function prints the function name where the error
occurred, the interface name, and the type of error in the standard
error stream (stderr).

Normally the program would be aborted, but for the time being the
default error function does return to its caller.
 
I

Ian Collins

In the context of the C Containers library I am now implementing "ValArray"

These containers store the basic numeric types (integers and floats)
of the language.

They support the basic operations that can be done with those types.

The question is:

ValArray A,B,C;

// ...

C = iValArrayDouble.SumTo(B,A);

This is the equivalent of B += A in C++.

The question is, what happens when Size(A) != Size(B). C++
leaves that as "undefined", probably crashing. My library
throws an error and does nothing.

That looks like a sensible solution. I can't see any sense in operating
with arrays of different lengths.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top