# RealVector interface could use some iterators (dense and sparse) and generic map() and collect() methods.

XMLWordPrintableJSON

#### Details

• Type: New Feature
• Status: Closed
• Priority: Major
• Resolution: Fixed
• Affects Version/s: 2.0
• Fix Version/s:
• Labels:
None
• Environment:

all

#### Description

As discussed on the [math] list, there are other projects out there which would love to get a chance to standardize on using commons-math for things like linear algebra primitives, as it would build a common base to build upon. But to do that, some well-known and used techniques for dealing with vectors, for one thing, are missing. Most glaringly is the treatment of sparse vectors: giving no Iterator for non-default values means external clients lose the advantage of sparseness - only internal methods can skip around.

Extending the RealVector interface with sparse (and dense) iterator methods would fix this:

```  double getDefaultValue();
Iterator<RealVector.Entry> iterator();
Iterator<RealVector.Entry> nonDefaultIterator();
```

but there is another way to deal with vector data as well: instead of passing iterators around, and worrying about all the lovely ConcurrentModification and unsupported "remove" methods (which aren't the end of the world), we can instead expose generic map functions:

```  RealVector map(UnivariateRealFunction f);
RealVector mapToSelf(UnivariateRealFunction f);
```

where RealVector mapToSelf(UnivariateRealFunction), which applies the function to the vector's entries (checking whether the function preserves the default value up front allows it to chose between the sparse or dense iterator), and map just applies mapToSelf to a copy.

This doesn't exhaust all possible places where Iterators could be used helpfully (there's also combining two vectors together via a

`map(BinaryRealFunction, RealVector other)`

which could be specialized nonlinear forms of addition or subtraction, and

`double collect(UnivariateRealFunction, BinaryRealFunction)`

which uses the iterates over all of the entries, applying the first unary function to each entry, and then applying the binary function to combine this value with the previous accumulated value - with "pow(2)", and "" as the two functions, you get L2 norm, with "abs()" and "", you get L1 norm, etc...)

#### Attachments

1. MATH-312.patch
76 kB
Jake Mannix
2. MATH-312.patch
48 kB
Jake Mannix

#### People

• Assignee:
Unassigned
Reporter:
Jake Mannix