001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.legacy.linear;
018
019import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
020import org.apache.commons.math4.legacy.exception.util.ExceptionContext;
021import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
022
023/**
024 * Exception to be thrown when a positive definite matrix is expected.
025 *
026 * @since 3.0
027 */
028public class NonPositiveDefiniteMatrixException extends NumberIsTooSmallException {
029    /** Serializable version Id. */
030    private static final long serialVersionUID = 1641613838113738061L;
031    /** Index (diagonal element). */
032    private final int index;
033    /** Threshold. */
034    private final double threshold;
035
036    /**
037     * Construct an exception.
038     *
039     * @param wrong Value that fails the positivity check.
040     * @param index Row (and column) index.
041     * @param threshold Absolute positivity threshold.
042     */
043    public NonPositiveDefiniteMatrixException(double wrong,
044                                              int index,
045                                              double threshold) {
046        super(wrong, threshold, false);
047        this.index = index;
048        this.threshold = threshold;
049
050        final ExceptionContext context = getContext();
051        context.addMessage(LocalizedFormats.NOT_POSITIVE_DEFINITE_MATRIX);
052        context.addMessage(LocalizedFormats.ARRAY_ELEMENT, wrong, index);
053    }
054
055    /**
056     * @return the row index.
057     */
058    public int getRow() {
059        return index;
060    }
061    /**
062     * @return the column index.
063     */
064    public int getColumn() {
065        return index;
066    }
067    /**
068     * @return the absolute positivity threshold.
069     */
070    public double getThreshold() {
071        return threshold;
072    }
073}