MATH-792:
- definition of RealVectorChangingVisitor, - various methods to visit the entries of a RealVector (entries are possibly modified), - default implementation in RealVector abstract class, - unit tests in abstract RealVectorAbstractClass. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1343342 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8aa20ce2a8
commit
209f22fc1c
172
checkstyle.xml
172
checkstyle.xml
|
@ -1,196 +1,118 @@
|
||||||
<?xml version="1.0"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
This configuration file was written by the eclipse-cs plugin configuration editor
|
||||||
contributor license agreements. See the NOTICE file distributed with
|
-->
|
||||||
this work for additional information regarding copyright ownership.
|
<!--
|
||||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
Checkstyle-Configuration: commons-math3
|
||||||
(the "License"); you may not use this file except in compliance with
|
Description: none
|
||||||
the License. You may obtain a copy of the License at
|
-->
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
-->
|
|
||||||
|
|
||||||
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.1//EN" "http://www.puppycrawl.com/dtds/configuration_1_1.dtd">
|
|
||||||
|
|
||||||
<!-- commons math customization of default Checkstyle behavior -->
|
|
||||||
<module name="Checker">
|
<module name="Checker">
|
||||||
|
<property name="severity" value="warning"/>
|
||||||
<property name="localeLanguage" value="en"/>
|
<property name="localeLanguage" value="en"/>
|
||||||
|
|
||||||
<module name="TreeWalker">
|
<module name="TreeWalker">
|
||||||
|
|
||||||
<property name="cacheFile" value="target/checkstyle-cachefile"/>
|
<property name="cacheFile" value="target/checkstyle-cachefile"/>
|
||||||
|
<module name="FileContentsHolder"/>
|
||||||
<!-- Operator must be at end of wrapped line -->
|
|
||||||
<module name="OperatorWrap">
|
<module name="OperatorWrap">
|
||||||
<property name="option" value="eol"/>
|
<property name="option" value="eol"/>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
<!-- No if/else/do/for/while without braces -->
|
|
||||||
<module name="NeedBraces"/>
|
<module name="NeedBraces"/>
|
||||||
|
|
||||||
<!-- Interfaces must be types (not just constants) -->
|
|
||||||
<module name="InterfaceIsType"/>
|
<module name="InterfaceIsType"/>
|
||||||
|
|
||||||
<!-- Must have class / interface header comments including scm version -->
|
|
||||||
<module name="JavadocType">
|
<module name="JavadocType">
|
||||||
<property name="versionFormat" value="\$Id.*\$"/>
|
<property name="versionFormat" value="\$Id.*\$"/>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
<!-- Require method javadocs, allow undeclared RTE -->
|
|
||||||
<module name="JavadocMethod">
|
<module name="JavadocMethod">
|
||||||
<property name="allowUndeclaredRTE" value="true"/>
|
<property name="allowUndeclaredRTE" value="true"/>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
<!-- Require field javadoc -->
|
|
||||||
<module name="JavadocVariable"/>
|
<module name="JavadocVariable"/>
|
||||||
|
|
||||||
<!-- No public fields -->
|
|
||||||
<module name="VisibilityModifier">
|
<module name="VisibilityModifier">
|
||||||
<property name="protectedAllowed" value="true"/>
|
<property name="protectedAllowed" value="true"/>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
<!-- Require hash code override when equals is -->
|
|
||||||
<module name="EqualsHashCode"/>
|
<module name="EqualsHashCode"/>
|
||||||
|
|
||||||
<!-- Disallow unnecessary instantiation of Boolean, String -->
|
|
||||||
<module name="IllegalInstantiation">
|
<module name="IllegalInstantiation">
|
||||||
<property name="classes" value="java.lang.Boolean, java.lang.String"/>
|
<property name="classes" value="java.lang.Boolean, java.lang.String"/>
|
||||||
</module>
|
</module>
|
||||||
|
<module name="AvoidStarImport"/>
|
||||||
<!-- Required for SuppressionCommentFilter below -->
|
<module name="UnusedImports"/>
|
||||||
<module name="FileContentsHolder"/>
|
<module name="IllegalImport"/>
|
||||||
|
<module name="HideUtilityClassConstructor"/>
|
||||||
<!-- Import should be explicit, really needed and only from pure java packages -->
|
<module name="FallThrough"/>
|
||||||
<module name="AvoidStarImport" />
|
<module name="MissingSwitchDefault"/>
|
||||||
<module name="UnusedImports" />
|
<module name="ConstantName"/>
|
||||||
<module name="IllegalImport" />
|
|
||||||
|
|
||||||
<!-- Utility class should not be instantiated, they must have a private constructor -->
|
|
||||||
<module name="HideUtilityClassConstructor" />
|
|
||||||
|
|
||||||
<!-- Switch statements should be complete and with independent cases -->
|
|
||||||
<module name="FallThrough" />
|
|
||||||
<module name="MissingSwitchDefault" />
|
|
||||||
|
|
||||||
<!-- Constant names should obey the traditional all uppercase naming convention -->
|
|
||||||
<module name="ConstantName" />
|
|
||||||
|
|
||||||
<!-- Method parameters and local variables should not hide fields, except in constructors and setters -->
|
|
||||||
<module name="HiddenField">
|
<module name="HiddenField">
|
||||||
<property name="ignoreConstructorParameter" value="true" />
|
<property name="ignoreConstructorParameter" value="true"/>
|
||||||
<property name="ignoreSetter" value="true" />
|
<property name="ignoreSetter" value="true"/>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
<!-- No trailing whitespace -->
|
|
||||||
<module name="Regexp">
|
<module name="Regexp">
|
||||||
<property name="format" value="[ \t]+$"/>
|
<property name="format" value="[ \t]+$"/>
|
||||||
<property name="illegalPattern" value="true"/>
|
|
||||||
<property name="message" value="Trailing whitespace"/>
|
<property name="message" value="Trailing whitespace"/>
|
||||||
|
<property name="illegalPattern" value="true"/>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
<!-- No System.out.println() statements -->
|
|
||||||
<module name="Regexp">
|
<module name="Regexp">
|
||||||
<!-- no sysouts -->
|
|
||||||
<property name="format" value="System\.out\.println"/>
|
<property name="format" value="System\.out\.println"/>
|
||||||
<property name="illegalPattern" value="true"/>
|
<property name="illegalPattern" value="true"/>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
<!-- Authors should be in pom.xml file -->
|
|
||||||
<module name="Regexp">
|
<module name="Regexp">
|
||||||
<property name="format" value="@author"/>
|
<property name="format" value="@author"/>
|
||||||
<property name="illegalPattern" value="true"/>
|
|
||||||
<property name="message" value="developers names should be in pom file"/>
|
<property name="message" value="developers names should be in pom file"/>
|
||||||
|
<property name="illegalPattern" value="true"/>
|
||||||
</module>
|
</module>
|
||||||
|
<module name="RedundantModifier"/>
|
||||||
<!-- Use a consistent way to put modifiers -->
|
<module name="ModifierOrder"/>
|
||||||
<module name="RedundantModifier" />
|
<module name="DeclarationOrder"/>
|
||||||
<module name="ModifierOrder" />
|
<module name="UnnecessaryParentheses"/>
|
||||||
|
<module name="IllegalCatch"/>
|
||||||
<!-- Use a consistent way to put declarations -->
|
<module name="StringLiteralEquality"/>
|
||||||
<module name="DeclarationOrder" />
|
<module name="MultipleVariableDeclarations"/>
|
||||||
|
<module name="MultipleStringLiterals">
|
||||||
<!-- Don't add up parentheses when they are not required -->
|
<property name="ignoreStringsRegexp" value="^(("")|(".")|("unchecked"))$"/>
|
||||||
<module name="UnnecessaryParentheses" />
|
|
||||||
|
|
||||||
<!-- Don't use too widespread catch (Exception, Throwable, RuntimeException) -->
|
|
||||||
<module name="IllegalCatch" />
|
|
||||||
|
|
||||||
<!-- Don't use = or != for string comparisons -->
|
|
||||||
<module name="StringLiteralEquality" />
|
|
||||||
|
|
||||||
<!-- Don't declare multiple variables in the same statement -->
|
|
||||||
<module name="MultipleVariableDeclarations" />
|
|
||||||
|
|
||||||
<!-- String literals more than one character long should not be repeated several times -->
|
|
||||||
<!-- the "unchecked" string is also accepted to allow @SuppressWarnings("unchecked") -->
|
|
||||||
<module name="MultipleStringLiterals" >
|
|
||||||
<property name="ignoreStringsRegexp" value='^(("")|(".")|("unchecked"))$'/>
|
|
||||||
</module>
|
</module>
|
||||||
|
<module name="LineLength"/>
|
||||||
<!-- <module name="TodoComment" /> -->
|
|
||||||
|
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
<!-- Verify that EVERY source file has the appropriate license -->
|
|
||||||
<module name="Header">
|
<module name="Header">
|
||||||
<property name="headerFile" value="${checkstyle.header.file}"/>
|
<property name="headerFile" value="${checkstyle.header.file}"/>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
<!-- No tabs allowed! -->
|
|
||||||
<module name="FileTabCharacter"/>
|
<module name="FileTabCharacter"/>
|
||||||
|
|
||||||
<!-- Require files to end with newline characters -->
|
|
||||||
<module name="NewlineAtEndOfFile"/>
|
<module name="NewlineAtEndOfFile"/>
|
||||||
|
|
||||||
<!-- Require package javadoc -->
|
|
||||||
<module name="JavadocPackage"/>
|
<module name="JavadocPackage"/>
|
||||||
|
|
||||||
<!-- Setup special comments to suppress specific checks from source files -->
|
|
||||||
<module name="SuppressionCommentFilter">
|
<module name="SuppressionCommentFilter">
|
||||||
<property name="offCommentFormat" value="CHECKSTYLE\: stop JavadocVariable"/>
|
<property name="offCommentFormat" value="CHECKSTYLE\: stop JavadocVariable"/>
|
||||||
<property name="onCommentFormat" value="CHECKSTYLE\: resume JavadocVariable"/>
|
<property name="onCommentFormat" value="CHECKSTYLE\: resume JavadocVariable"/>
|
||||||
<property name="checkFormat" value="JavadocVariable"/>
|
<property name="checkFormat" value="JavadocVariable"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="SuppressionCommentFilter">
|
<module name="SuppressionCommentFilter">
|
||||||
<property name="offCommentFormat" value="CHECKSTYLE\: stop JavadocMethodCheck"/>
|
<property name="offCommentFormat" value="CHECKSTYLE\: stop JavadocMethodCheck"/>
|
||||||
<property name="onCommentFormat" value="CHECKSTYLE\: resume JavadocMethodCheck"/>
|
<property name="onCommentFormat" value="CHECKSTYLE\: resume JavadocMethodCheck"/>
|
||||||
<property name="checkFormat" value="JavadocMethodCheck"/>
|
<property name="checkFormat" value="JavadocMethodCheck"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="SuppressionCommentFilter">
|
<module name="SuppressionCommentFilter">
|
||||||
<property name="offCommentFormat" value="CHECKSTYLE\: stop ConstantName"/>
|
<property name="offCommentFormat" value="CHECKSTYLE\: stop ConstantName"/>
|
||||||
<property name="onCommentFormat" value="CHECKSTYLE\: resume ConstantName"/>
|
<property name="onCommentFormat" value="CHECKSTYLE\: resume ConstantName"/>
|
||||||
<property name="checkFormat" value="ConstantName"/>
|
<property name="checkFormat" value="ConstantName"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="SuppressionCommentFilter">
|
<module name="SuppressionCommentFilter">
|
||||||
<property name="offCommentFormat" value="CHECKSTYLE\: stop HideUtilityClassConstructor"/>
|
<property name="offCommentFormat" value="CHECKSTYLE\: stop HideUtilityClassConstructor"/>
|
||||||
<property name="onCommentFormat" value="CHECKSTYLE\: resume HideUtilityClassConstructor"/>
|
<property name="onCommentFormat" value="CHECKSTYLE\: resume HideUtilityClassConstructor"/>
|
||||||
<property name="checkFormat" value="HideUtilityClassConstructor"/>
|
<property name="checkFormat" value="HideUtilityClassConstructor"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="SuppressionCommentFilter">
|
<module name="SuppressionCommentFilter">
|
||||||
<property name="offCommentFormat" value="CHECKSTYLE\: stop MultipleVariableDeclarations"/>
|
<property name="offCommentFormat" value="CHECKSTYLE\: stop MultipleVariableDeclarations"/>
|
||||||
<property name="onCommentFormat" value="CHECKSTYLE\: resume MultipleVariableDeclarations"/>
|
<property name="onCommentFormat" value="CHECKSTYLE\: resume MultipleVariableDeclarations"/>
|
||||||
<property name="checkFormat" value="MultipleVariableDeclarations"/>
|
<property name="checkFormat" value="MultipleVariableDeclarations"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="SuppressionCommentFilter">
|
<module name="SuppressionCommentFilter">
|
||||||
<property name="offCommentFormat" value="CHECKSTYLE\: stop IllegalCatch"/>
|
<property name="offCommentFormat" value="CHECKSTYLE\: stop IllegalCatch"/>
|
||||||
<property name="onCommentFormat" value="CHECKSTYLE\: resume IllegalCatch"/>
|
<property name="onCommentFormat" value="CHECKSTYLE\: resume IllegalCatch"/>
|
||||||
<property name="checkFormat" value="IllegalCatch"/>
|
<property name="checkFormat" value="IllegalCatch"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="SuppressionCommentFilter">
|
<module name="SuppressionCommentFilter">
|
||||||
<property name="offCommentFormat" value="CHECKSTYLE\: stop DeclarationOrder"/>
|
<property name="offCommentFormat" value="CHECKSTYLE\: stop DeclarationOrder"/>
|
||||||
<property name="onCommentFormat" value="CHECKSTYLE\: resume DeclarationOrder"/>
|
<property name="onCommentFormat" value="CHECKSTYLE\: resume DeclarationOrder"/>
|
||||||
<property name="checkFormat" value="DeclarationOrder"/>
|
<property name="checkFormat" value="DeclarationOrder"/>
|
||||||
</module>
|
</module>
|
||||||
<module name="SuppressionCommentFilter">
|
<module name="SuppressionCommentFilter">
|
||||||
<property name="offCommentFormat" value="CHECKSTYLE\: stop all"/>
|
<property name="offCommentFormat" value="CHECKSTYLE\: stop all"/>
|
||||||
<property name="onCommentFormat" value="CHECKSTYLE\: resume all"/>
|
<property name="onCommentFormat" value="CHECKSTYLE\: resume all"/>
|
||||||
</module>
|
</module>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
|
|
|
@ -881,4 +881,26 @@ public class ArrayRealVector extends RealVector implements Serializable {
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
@Override
|
||||||
|
public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor) {
|
||||||
|
visitor.start(data.length, 0, data.length - 1);
|
||||||
|
for (int i = 0; i < data.length; i++) {
|
||||||
|
visitor.visit(i, data[i]);
|
||||||
|
}
|
||||||
|
return visitor.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
@Override
|
||||||
|
public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor,
|
||||||
|
final int start, final int end) {
|
||||||
|
checkIndices(start, end);
|
||||||
|
visitor.start(data.length, start, end);
|
||||||
|
for (int i = start; i <= end; i++) {
|
||||||
|
visitor.visit(i, data[i]);
|
||||||
|
}
|
||||||
|
return visitor.end();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -835,7 +835,7 @@ public abstract class RealVector {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visits (but does not change) all entries of this vector in default order
|
* Visits (but does not alter) all entries of this vector in default order
|
||||||
* (increasing index).
|
* (increasing index).
|
||||||
*
|
*
|
||||||
* @param visitor the visitor to be used to process the entries of this
|
* @param visitor the visitor to be used to process the entries of this
|
||||||
|
@ -853,7 +853,7 @@ public abstract class RealVector {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visits (and possibly change) some entries of this vector in default order
|
* Visits (but does not alter) some entries of this vector in default order
|
||||||
* (increasing index).
|
* (increasing index).
|
||||||
*
|
*
|
||||||
* @param visitor visitor to be used to process the entries of this vector
|
* @param visitor visitor to be used to process the entries of this vector
|
||||||
|
@ -865,7 +865,7 @@ public abstract class RealVector {
|
||||||
* the indices are not valid.
|
* the indices are not valid.
|
||||||
*/
|
*/
|
||||||
public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor,
|
public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor,
|
||||||
int start, int end) {
|
int start, int end) {
|
||||||
checkIndices(start, end);
|
checkIndices(start, end);
|
||||||
visitor.start(getDimension(), start, end);
|
visitor.start(getDimension(), start, end);
|
||||||
for (int i = start; i <= end; i++) {
|
for (int i = start; i <= end; i++) {
|
||||||
|
@ -875,7 +875,7 @@ public abstract class RealVector {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visits (but does not change) all entries of this vector in optimized
|
* Visits (but does not alter) all entries of this vector in optimized
|
||||||
* order. The order in which the entries are visited is selected so as to
|
* order. The order in which the entries are visited is selected so as to
|
||||||
* lead to the most efficient implementation; it might depend on the
|
* lead to the most efficient implementation; it might depend on the
|
||||||
* concrete implementation of this abstract class.
|
* concrete implementation of this abstract class.
|
||||||
|
@ -890,10 +890,10 @@ public abstract class RealVector {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visits (and possibly change) some entries of this vector in default order
|
* Visits (but does not alter) some entries of this vector in optimized
|
||||||
* (increasing index). The order in which the entries are visited is
|
* order. The order in which the entries are visited is selected so as to
|
||||||
* selected so as to lead to the most efficient implementation; it might
|
* lead to the most efficient implementation; it might depend on the
|
||||||
* depend on the concrete implementation of this abstract class.
|
* concrete implementation of this abstract class.
|
||||||
*
|
*
|
||||||
* @param visitor visitor to be used to process the entries of this vector
|
* @param visitor visitor to be used to process the entries of this vector
|
||||||
* @param start the index of the first entry to be visited
|
* @param start the index of the first entry to be visited
|
||||||
|
@ -908,6 +908,80 @@ public abstract class RealVector {
|
||||||
return walkInDefaultOrder(visitor, start, end);
|
return walkInDefaultOrder(visitor, start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits (and possibly alters) all entries of this vector in default order
|
||||||
|
* (increasing index).
|
||||||
|
*
|
||||||
|
* @param visitor the visitor to be used to process and modify the entries
|
||||||
|
* of this vector
|
||||||
|
* @return the value returned by {@link RealVectorChangingVisitor#end()}
|
||||||
|
* at the end of the walk
|
||||||
|
*/
|
||||||
|
public double walkInDefaultOrder(final RealVectorChangingVisitor visitor) {
|
||||||
|
final int dim = getDimension();
|
||||||
|
visitor.start(dim, 0, dim - 1);
|
||||||
|
for (int i = 0; i < dim; i++) {
|
||||||
|
setEntry(i, visitor.visit(i, getEntry(i)));
|
||||||
|
}
|
||||||
|
return visitor.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits (and possibly alters) some entries of this vector in default order
|
||||||
|
* (increasing index).
|
||||||
|
*
|
||||||
|
* @param visitor visitor to be used to process the entries of this vector
|
||||||
|
* @param start the index of the first entry to be visited
|
||||||
|
* @param end the index of the last entry to be visited (inclusive)
|
||||||
|
* @return the value returned by {@link RealVectorChangingVisitor#end()}
|
||||||
|
* at the end of the walk
|
||||||
|
* @throws org.apache.commons.math3.exception.OutOfRangeException if
|
||||||
|
* the indices are not valid.
|
||||||
|
*/
|
||||||
|
public double walkInDefaultOrder(final RealVectorChangingVisitor visitor,
|
||||||
|
int start, int end) {
|
||||||
|
checkIndices(start, end);
|
||||||
|
visitor.start(getDimension(), start, end);
|
||||||
|
for (int i = start; i <= end; i++) {
|
||||||
|
setEntry(i, visitor.visit(i, getEntry(i)));
|
||||||
|
}
|
||||||
|
return visitor.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits (and possibly alters) all entries of this vector in optimized
|
||||||
|
* order. The order in which the entries are visited is selected so as to
|
||||||
|
* lead to the most efficient implementation; it might depend on the
|
||||||
|
* concrete implementation of this abstract class.
|
||||||
|
*
|
||||||
|
* @param visitor the visitor to be used to process the entries of this
|
||||||
|
* vector
|
||||||
|
* @return the value returned by {@link RealVectorChangingVisitor#end()}
|
||||||
|
* at the end of the walk
|
||||||
|
*/
|
||||||
|
public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor) {
|
||||||
|
return walkInDefaultOrder(visitor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visits (and possibly change) some entries of this vector in optimized
|
||||||
|
* order. The order in which the entries are visited is selected so as to
|
||||||
|
* lead to the most efficient implementation; it might depend on the
|
||||||
|
* concrete implementation of this abstract class.
|
||||||
|
*
|
||||||
|
* @param visitor visitor to be used to process the entries of this vector
|
||||||
|
* @param start the index of the first entry to be visited
|
||||||
|
* @param end the index of the last entry to be visited (inclusive)
|
||||||
|
* @return the value returned by {@link RealVectorChangingVisitor#end()}
|
||||||
|
* at the end of the walk
|
||||||
|
* @throws org.apache.commons.math3.exception.OutOfRangeException if
|
||||||
|
* the indices are not valid.
|
||||||
|
*/
|
||||||
|
public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor,
|
||||||
|
int start, int end) {
|
||||||
|
return walkInDefaultOrder(visitor, start, end);
|
||||||
|
}
|
||||||
|
|
||||||
/** An entry in the vector. */
|
/** An entry in the vector. */
|
||||||
protected class Entry {
|
protected class Entry {
|
||||||
/** Index of this entry. */
|
/** Index of this entry. */
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.commons.math3.linear;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface defines a visitor for the entries of a vector. Visitors
|
||||||
|
* implementing this interface may alter the entries of the vector being
|
||||||
|
* visited.
|
||||||
|
*
|
||||||
|
* @version $Id$
|
||||||
|
* @since 3.1
|
||||||
|
*/
|
||||||
|
public interface RealVectorChangingVisitor {
|
||||||
|
/**
|
||||||
|
* Start visiting a vector. This method is called once, before any entry
|
||||||
|
* of the vector is visited.
|
||||||
|
*
|
||||||
|
* @param dimension the size of the vector
|
||||||
|
* @param start the index of the first entry to be visited
|
||||||
|
* @param end the index of the last entry to be visited (inclusive)
|
||||||
|
*/
|
||||||
|
void start(int dimension, int start, int end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visit one entry of the vector.
|
||||||
|
*
|
||||||
|
* @param index the index of the entry being visited
|
||||||
|
* @param value the value of the entry being visited
|
||||||
|
* @return the new value of the entry being visited
|
||||||
|
*/
|
||||||
|
double visit(int index, double value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* End visiting a vector. This method is called once, after all entries of
|
||||||
|
* the vector have been visited.
|
||||||
|
*
|
||||||
|
* @return the value returned by
|
||||||
|
* {@link RealVector#walkInDefaultOrder(RealVectorChangingVisitor)},
|
||||||
|
* {@link RealVector#walkInDefaultOrder(RealVectorChangingVisitor, int, int)},
|
||||||
|
* {@link RealVector#walkInOptimizedOrder(RealVectorChangingVisitor)}
|
||||||
|
* or
|
||||||
|
* {@link RealVector#walkInOptimizedOrder(RealVectorChangingVisitor, int, int)}
|
||||||
|
*/
|
||||||
|
double end();
|
||||||
|
}
|
|
@ -41,7 +41,7 @@ public abstract class RealVectorAbstractTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public double end() {
|
public double end() {
|
||||||
return 0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
v.walkInDefaultOrder(visitor);
|
v.walkInDefaultOrder(visitor);
|
||||||
|
@ -51,32 +51,47 @@ public abstract class RealVectorAbstractTest {
|
||||||
@Test
|
@Test
|
||||||
public void testWalkInDefaultOrderPreservingVisitor2() {
|
public void testWalkInDefaultOrderPreservingVisitor2() {
|
||||||
final RealVector v = create(new double[5]);
|
final RealVector v = create(new double[5]);
|
||||||
|
final RealVectorPreservingVisitor visitor;
|
||||||
|
visitor = new RealVectorPreservingVisitor() {
|
||||||
|
|
||||||
|
public void visit(int index, double value) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(int dimension, int start, int end) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
public double end() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
};
|
||||||
try {
|
try {
|
||||||
v.walkInDefaultOrder(null, -1, 4);
|
v.walkInDefaultOrder(visitor, -1, 4);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (OutOfRangeException e) {
|
} catch (OutOfRangeException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
v.walkInDefaultOrder(null, 5, 4);
|
v.walkInDefaultOrder(visitor, 5, 4);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (OutOfRangeException e) {
|
} catch (OutOfRangeException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
v.walkInDefaultOrder(null, 0, -1);
|
v.walkInDefaultOrder(visitor, 0, -1);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (OutOfRangeException e) {
|
} catch (OutOfRangeException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
v.walkInDefaultOrder(null, 0, 5);
|
v.walkInDefaultOrder(visitor, 0, 5);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (OutOfRangeException e) {
|
} catch (OutOfRangeException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
v.walkInDefaultOrder(null, 4, 0);
|
v.walkInDefaultOrder(visitor, 4, 0);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (NumberIsTooSmallException e) {
|
} catch (NumberIsTooSmallException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
|
@ -113,7 +128,7 @@ public abstract class RealVectorAbstractTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public double end() {
|
public double end() {
|
||||||
return 0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
|
v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
|
||||||
|
@ -149,7 +164,7 @@ public abstract class RealVectorAbstractTest {
|
||||||
Assert.assertTrue("entry " + i + "has not been visited",
|
Assert.assertTrue("entry " + i + "has not been visited",
|
||||||
visited[i]);
|
visited[i]);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
v.walkInOptimizedOrder(visitor);
|
v.walkInOptimizedOrder(visitor);
|
||||||
|
@ -159,32 +174,47 @@ public abstract class RealVectorAbstractTest {
|
||||||
@Test
|
@Test
|
||||||
public void testWalkInOptimizedOrderPreservingVisitor2() {
|
public void testWalkInOptimizedOrderPreservingVisitor2() {
|
||||||
final RealVector v = create(new double[5]);
|
final RealVector v = create(new double[5]);
|
||||||
|
final RealVectorPreservingVisitor visitor;
|
||||||
|
visitor = new RealVectorPreservingVisitor() {
|
||||||
|
|
||||||
|
public void visit(int index, double value) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(int dimension, int start, int end) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
public double end() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
};
|
||||||
try {
|
try {
|
||||||
v.walkInOptimizedOrder(null, -1, 4);
|
v.walkInOptimizedOrder(visitor, -1, 4);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (OutOfRangeException e) {
|
} catch (OutOfRangeException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
v.walkInOptimizedOrder(null, 5, 4);
|
v.walkInOptimizedOrder(visitor, 5, 4);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (OutOfRangeException e) {
|
} catch (OutOfRangeException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
v.walkInOptimizedOrder(null, 0, -1);
|
v.walkInOptimizedOrder(visitor, 0, -1);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (OutOfRangeException e) {
|
} catch (OutOfRangeException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
v.walkInOptimizedOrder(null, 0, 5);
|
v.walkInOptimizedOrder(visitor, 0, 5);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (OutOfRangeException e) {
|
} catch (OutOfRangeException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
v.walkInOptimizedOrder(null, 4, 0);
|
v.walkInOptimizedOrder(visitor, 4, 0);
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (NumberIsTooSmallException e) {
|
} catch (NumberIsTooSmallException e) {
|
||||||
// Expected behavior
|
// Expected behavior
|
||||||
|
@ -223,9 +253,271 @@ public abstract class RealVectorAbstractTest {
|
||||||
Assert.assertTrue("entry " + i + "has not been visited",
|
Assert.assertTrue("entry " + i + "has not been visited",
|
||||||
visited[i]);
|
visited[i]);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
|
v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The whole vector is visited. */
|
||||||
|
@Test
|
||||||
|
public void testWalkInDefaultOrderChangingVisitor1() {
|
||||||
|
final double[] data = new double[] {
|
||||||
|
0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
|
||||||
|
};
|
||||||
|
final RealVector v = create(data);
|
||||||
|
final RealVectorChangingVisitor visitor;
|
||||||
|
visitor = new RealVectorChangingVisitor() {
|
||||||
|
|
||||||
|
private int expectedIndex;
|
||||||
|
|
||||||
|
public double visit(final int actualIndex, final double actualValue) {
|
||||||
|
Assert.assertEquals(expectedIndex, actualIndex);
|
||||||
|
Assert.assertEquals(Integer.toString(actualIndex),
|
||||||
|
data[actualIndex], actualValue, 0d);
|
||||||
|
++expectedIndex;
|
||||||
|
return actualIndex + actualValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(final int actualSize, final int actualStart,
|
||||||
|
final int actualEnd) {
|
||||||
|
Assert.assertEquals(data.length, actualSize);
|
||||||
|
Assert.assertEquals(0, actualStart);
|
||||||
|
Assert.assertEquals(data.length - 1, actualEnd);
|
||||||
|
expectedIndex = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double end() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
v.walkInDefaultOrder(visitor);
|
||||||
|
for (int i = 0; i < data.length; i++) {
|
||||||
|
Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Visiting an invalid subvector. */
|
||||||
|
@Test
|
||||||
|
public void testWalkInDefaultOrderChangingVisitor2() {
|
||||||
|
final RealVector v = create(new double[5]);
|
||||||
|
final RealVectorChangingVisitor visitor;
|
||||||
|
visitor = new RealVectorChangingVisitor() {
|
||||||
|
|
||||||
|
public double visit(int index, double value) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(int dimension, int start, int end) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
public double end() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
v.walkInDefaultOrder(visitor, -1, 4);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (OutOfRangeException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
v.walkInDefaultOrder(visitor, 5, 4);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (OutOfRangeException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
v.walkInDefaultOrder(visitor, 0, -1);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (OutOfRangeException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
v.walkInDefaultOrder(visitor, 0, 5);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (OutOfRangeException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
v.walkInDefaultOrder(visitor, 4, 0);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (NumberIsTooSmallException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Visiting a valid subvector. */
|
||||||
|
@Test
|
||||||
|
public void testWalkInDefaultOrderChangingVisitor3() {
|
||||||
|
final double[] data = new double[] {
|
||||||
|
0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
|
||||||
|
};
|
||||||
|
final int expectedStart = 2;
|
||||||
|
final int expectedEnd = 7;
|
||||||
|
final RealVector v = create(data);
|
||||||
|
final RealVectorChangingVisitor visitor;
|
||||||
|
visitor = new RealVectorChangingVisitor() {
|
||||||
|
|
||||||
|
private int expectedIndex;
|
||||||
|
|
||||||
|
public double visit(final int actualIndex, final double actualValue) {
|
||||||
|
Assert.assertEquals(expectedIndex, actualIndex);
|
||||||
|
Assert.assertEquals(Integer.toString(actualIndex),
|
||||||
|
data[actualIndex], actualValue, 0d);
|
||||||
|
++expectedIndex;
|
||||||
|
return actualIndex + actualValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(final int actualSize, final int actualStart,
|
||||||
|
final int actualEnd) {
|
||||||
|
Assert.assertEquals(data.length, actualSize);
|
||||||
|
Assert.assertEquals(expectedStart, actualStart);
|
||||||
|
Assert.assertEquals(expectedEnd, actualEnd);
|
||||||
|
expectedIndex = expectedStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double end() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
|
||||||
|
for (int i = expectedStart; i <= expectedEnd; i++) {
|
||||||
|
Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The whole vector is visited. */
|
||||||
|
@Test
|
||||||
|
public void testWalkInOptimizedOrderChangingVisitor1() {
|
||||||
|
final double[] data = new double[] {
|
||||||
|
0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
|
||||||
|
};
|
||||||
|
final RealVector v = create(data);
|
||||||
|
final RealVectorChangingVisitor visitor;
|
||||||
|
visitor = new RealVectorChangingVisitor() {
|
||||||
|
private final boolean[] visited = new boolean[data.length];
|
||||||
|
|
||||||
|
public double visit(final int actualIndex, final double actualValue) {
|
||||||
|
visited[actualIndex] = true;
|
||||||
|
Assert.assertEquals(Integer.toString(actualIndex),
|
||||||
|
data[actualIndex], actualValue, 0d);
|
||||||
|
return actualIndex + actualValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(final int actualSize, final int actualStart,
|
||||||
|
final int actualEnd) {
|
||||||
|
Assert.assertEquals(data.length, actualSize);
|
||||||
|
Assert.assertEquals(0, actualStart);
|
||||||
|
Assert.assertEquals(data.length - 1, actualEnd);
|
||||||
|
Arrays.fill(visited, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double end() {
|
||||||
|
for (int i = 0; i < data.length; i++) {
|
||||||
|
Assert.assertTrue("entry " + i + "has not been visited",
|
||||||
|
visited[i]);
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
v.walkInOptimizedOrder(visitor);
|
||||||
|
for (int i = 0; i < data.length; i++) {
|
||||||
|
Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Visiting an invalid subvector. */
|
||||||
|
@Test
|
||||||
|
public void testWalkInOptimizedOrderChangingVisitor2() {
|
||||||
|
final RealVector v = create(new double[5]);
|
||||||
|
final RealVectorChangingVisitor visitor;
|
||||||
|
visitor = new RealVectorChangingVisitor() {
|
||||||
|
|
||||||
|
public double visit(int index, double value) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(int dimension, int start, int end) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
public double end() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
v.walkInOptimizedOrder(visitor, -1, 4);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (OutOfRangeException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
v.walkInOptimizedOrder(visitor, 5, 4);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (OutOfRangeException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
v.walkInOptimizedOrder(visitor, 0, -1);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (OutOfRangeException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
v.walkInOptimizedOrder(visitor, 0, 5);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (OutOfRangeException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
v.walkInOptimizedOrder(visitor, 4, 0);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (NumberIsTooSmallException e) {
|
||||||
|
// Expected behavior
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Visiting a valid subvector. */
|
||||||
|
@Test
|
||||||
|
public void testWalkInOptimizedOrderChangingVisitor3() {
|
||||||
|
final double[] data = new double[] {
|
||||||
|
0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
|
||||||
|
};
|
||||||
|
final int expectedStart = 2;
|
||||||
|
final int expectedEnd = 7;
|
||||||
|
final RealVector v = create(data);
|
||||||
|
final RealVectorChangingVisitor visitor;
|
||||||
|
visitor = new RealVectorChangingVisitor() {
|
||||||
|
private final boolean[] visited = new boolean[data.length];
|
||||||
|
|
||||||
|
public double visit(final int actualIndex, final double actualValue) {
|
||||||
|
Assert.assertEquals(Integer.toString(actualIndex),
|
||||||
|
data[actualIndex], actualValue, 0d);
|
||||||
|
visited[actualIndex] = true;
|
||||||
|
return actualIndex + actualValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(final int actualSize, final int actualStart,
|
||||||
|
final int actualEnd) {
|
||||||
|
Assert.assertEquals(data.length, actualSize);
|
||||||
|
Assert.assertEquals(expectedStart, actualStart);
|
||||||
|
Assert.assertEquals(expectedEnd, actualEnd);
|
||||||
|
Arrays.fill(visited, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double end() {
|
||||||
|
for (int i = expectedStart; i <= expectedEnd; i++) {
|
||||||
|
Assert.assertTrue("entry " + i + "has not been visited",
|
||||||
|
visited[i]);
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
|
||||||
|
for (int i = expectedStart; i <= expectedEnd; i++) {
|
||||||
|
Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue