/*
 Copyright (C) 2007 Richard Gomes

 This source code is release under the BSD License.

 This file is part of JQuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://jquantlib.org/

 JQuantLib is free software: you can redistribute it and/or modify it
 under the terms of the JQuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 . The license is also available online at
 .

 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 license for more details.

 JQuantLib is based on QuantLib. http://quantlib.org/
 When applicable, the original copyright notice follows this notice.
 */


package org.jquantlib.time;

import java.util.ArrayList;
import java.util.List;

import org.jquantlib.QL;
import org.jquantlib.lang.annotation.NonNegative;
import org.jquantlib.lang.annotation.Time;
import org.jquantlib.lang.iterators.Iterables;
import org.jquantlib.math.Closeness;
import org.jquantlib.math.matrixutilities.Array;


/**
 * TimeGrid class.
 *
 * @author Dominik Holenstein
 */
// TODO: Taken over from QuantLib: What was the rationale for limiting the grid to positive times?
// Investigate and see whether we can use it for negative ones as well.
public class TimeGrid {

    //
    // private fields
    //
    private final Array times;
    private final Array dt;
    private final Array mandatoryTimes;



    //
    // Constructors
    //

    public TimeGrid() {
        this.times = null; //XXX  new Array();
        this.dt = null; //XXX: new Array();
        this.mandatoryTimes = null; //XXX: new Array();
    }

    /**
     * Regularly spaced time-grid
     *
     * @param end
     * @param steps
     */
    public TimeGrid(@Time @NonNegative final double end, @NonNegative final int steps) {

        // THIS COMMENT COMES FROM QuantLib/C++ code
        //
        // We seem to assume that the grid begins at 0.
        // Let's enforce the assumption for the time being
        // (even though I'm not sure that I agree.)
        QL.require(end > 0.0 , "negative times not allowed"); // QA:[RG]::verified // FIXME: message

        /*@Time*/ final double dt = end/steps;
        this.times = new Array(steps+1);
        for (int i=0; i<=steps; i++) {
            times.set(i, dt*i);
        }
        this.mandatoryTimes = new Array(1).fill(end);
        this.dt = new Array(steps).fill(dt);
    }


    /**
     * Time grid with mandatory time points.
     * 

* Mandatory points are guaranteed to belong to the grid. No additional points are added. * * @note This constructor is not available yet * * @param list */ public TimeGrid(@Time @NonNegative final Array mandatoryTimes) { if (System.getProperty("EXPERIMENTAL")==null) throw new UnsupportedOperationException("This constructor is not available yet"); //XXX this.mandatoryTimes = mandatoryTimes.clone(); this.mandatoryTimes = mandatoryTimes; this.mandatoryTimes.sort(); // THIS COMMENT COMES FROM QuantLib/C++ code // // We seem to assume that the grid begins at 0. // Let's enforce the assumption for the time being // (even though I'm not sure that I agree.) QL.require(mandatoryTimes.first() < 0.0 , "negative times not allowed"); // TODO: message final List unique = new ArrayList(); double prev = this.mandatoryTimes.get(0); unique.add(prev); for (int i=1; i * Mandatory points are guaranteed to belong to the grid. * Additional points are then added with regular spacing between pairs of mandatory times in order * to reach the desired number of steps. * * @note This constructor is not available yet - fix adjacent_difference before using */ //TODO: needs code review // template // TimeGrid(Iterator begin, Iterator end, Size steps) // : mandatoryTimes_(begin, end) { // std::sort(mandatoryTimes_.begin(),mandatoryTimes_.end()); // // We seem to assume that the grid begins at 0. // // Let's enforce the assumption for the time being // // (even though I'm not sure that I agree.) // QL_REQUIRE(mandatoryTimes_.front() >= 0.0, // "negative times not allowed"); // std::vector

Ads help maintain this website.