package net.sourceforge.gpstools.math;
/* gpsdings
* Copyright (C) 2007 Moritz Ringler
* $Id: PieceWiseDifferentiableFunction.java 441 2010-12-13 20:04:20Z ringler $
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
public class PieceWiseDifferentiableFunction extends PieceWiseFunction implements DifferentiableUnivariateRealFunction {
public PieceWiseDifferentiableFunction(){
super();
}
/** @throws ClassCastException if f.func is not a DifferentiableUnivariateRealFunction
* @throws IllegalArgumentException if the domain of the new segment overlaps with the domain of another one
*/
@Override
public synchronized boolean addSegmentFunction(SegmentFunction f){
//this will throw the required class cast exception
@SuppressWarnings("unused")
DifferentiableUnivariateRealFunction func = (DifferentiableUnivariateRealFunction) f.func;
for(SegmentFunction other : elements){
if(f.domain.overlaps(other.domain) && ! f.equals(other)){
throw new IllegalArgumentException("The domain of this SegmentFunction overlaps with that of another one.");
}
}
return elements.add(f);
}
@Override
public UnivariateRealFunction derivative(){
PieceWiseFunction result = new PieceWiseFunction();
for(SegmentFunction f : elements){
result.addSegmentFunction(new SegmentFunction(f.domain, ((DifferentiableUnivariateRealFunction) f.func).derivative()));
}
return result;
}
}