commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ole Ersoy <ole.er...@gmail.com>
Subject Re: [math] Java 8 RealVector Functional Design
Date Tue, 05 Jan 2016 20:35:50 GMT
Hi,

This is part 2 to the below.  So initially I was thinking about having a Vector with map,
reduce, etc. operations on it.  The map function would handle the mapping of two vectors to
a third (Actually just mapping one vector onto another).  The reduce function would reduce
either the vector or two vectors to a single value.  The dotProduct method would be an example.

However once Lambda functions are implemented they could just be used standalone.  For example:

     public static Function<Vector, Double> Norm = (v) -> {
         return Math.sqrt(
                 IntStream.range(0, v.getDimension()).mapToDouble(i -> Math.pow(v.getEntry(i),
2)).sum());
     };

RUNTIME:
double norm = Norm.apply(v);
// Faster
double norm = ParallelNorm.apply(v);

So it's possible to have a reduce interface on the vector like this:

double = v.reduce(Norm)

That does the same thing, but it's pointless and harder to explain. Also (I have not tested
yet) but just by the looks of it it seems the above Norm function can be applied to any implementation
of Vector.  Thus if the vector interface is dead simple and only supplies getEntry(i), setEntry(i),
getDimension()  that's good enough for just about everything...I think...famous last words
.

Cheers,
Ole


On 01/05/2016 10:35 AM, Ole Ersoy wrote:
> Hi,
>
> I'm attempting a more minimalistic array vector design and just thought I'd float a partial
API to see what you think.  The below methods are both 'mapToSelf' by default.  If the user
wants a new vector, she should first clone the vector and then call the map method (vector.clone().map(...)).
>
>     public void map(BiFunction<Double, Double, Double> function, Vector v) {
>         Arrays.setAll(data, i -> function.apply(data[i], v.getEntry(i)));
>     }
>
>     public void parallelMap(BiFunction<Double, Double, Double> function, Vector
v) {
>         Arrays.parallelSetAll(data, i -> function.apply(data[i], v.getEntry(i)));
>     }
>
> The above two functions (Left the dimension check out) allow you to "Plug in" a lambda
function to perform the mapping.  For example if you want to perform addition, you would use
the addition BiFunction like this:
>
>     public static BiFunction<Double, Double, Double> add = (x, y) -> {
>         return x
>                 + y;
>     };
>
> RUNTIME:
> vector2.map(add, vector1);
>
>
> Then the same for subtraction, multiplication, etc.  I'm thinking the static BiFunction
instances can go in the Arithmetic module. That way the map methods can use both checked and
unchecked arithmetic operations.  I hoping that this will also make the FieldVector and RealVector
implementations more efficient from a code sharing viewpoint and symmetric from an API perspective.
>
> Thoughts?
>
> Cheers,
> Ole
>
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message