Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java best practices in matrix/vector library

i have to write a simple vector/matrix library for a small geometry related project i'm working on. here's what i'm wondering.

when doing mathematical operations on vectors in a java environment, is it better practice to return a new instance of a vector or modify the state of the original.

i've seen it back and forth and would just like to get a majority input.

certain people say that the vectors should be immutable and static methods should be used to create new ones, others say that they should be mutable and normal methods should be used to modify their state. i've seen it in some cases where the object is immutable and normal methods are called which returns a new vector from the object without changing the state - this seems a little off to me.

i would just like to get a feel for if there is any best practice for this - i imagine it's something that's been done a million times and am really just wondering if there's a standard way to do this.

i noticed the apache commons math library returns a new vector every time from the original.

like image 922
sean christe Avatar asked Oct 11 '11 15:10

sean christe


2 Answers

How important is performance going to be? Is vector arithmetic going to be a large component so that it affects the performance of overall system?

If it is not and there is going to be lot of concurrency then immutable vectors will be useful because they reduce concurrency issues.

If there are lot of mutations on vectors then the overhead of new objects that immutable vectors will require will become significant and it may be better to have mutable vectors and do the concurrency the hard way.

like image 135
Miserable Variable Avatar answered Sep 23 '22 15:09

Miserable Variable


It depends. Generally speaking, immutability is better.

First and foremost, it is automatically threadsafe. It is easier to maintain and test.

That said, sometimes you need speed where creating new instances will take too much time.

(Note: If you're not 100% positive you need that amount of speed, you don't need it. Think high-frequency trading and real-time math-intensive applications. And even though, you should go simple first, and optimize later.)

As for static vs normal methods, following good OOP principles, you shouldn't have static methods. To create new vectors/matrices you can use the constructor.

Next, what's your backing structure? Your best bet is probably single-dimensional arrays of doubles for vectors and multi-dimensional arrays of doubles for matrices. This at least lets you stay relatively quick by using primitive objects.

If you get to the point that you need even more performance, you can add modifiers on your Vector/Matrix that can change the backing data. You could even decide that the dimensions are immutable but the contents are mutable which would give you some other safeties as well.

like image 44
Reverend Gonzo Avatar answered Sep 24 '22 15:09

Reverend Gonzo