<commit_before>/**
 *  @file SquareMatrix.cpp
 */

/*
 * Copyright 2004 Sandia Corporation. Under the terms of Contract
 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
 * retains certain rights in this software.
 * See file License.txt for licensing information.
 */

#include "cantera/base/stringUtils.h"
#include "cantera/numerics/ctlapack.h"
#include "cantera/numerics/SquareMatrix.h"
#include "cantera/base/global.h"

using namespace std;

namespace Cantera
{

SquareMatrix::SquareMatrix() :
    DenseMatrix(),
    GeneralMatrix(0),
    m_factored(0),
    a1norm_(0.0),
    useQR_(0)
{
}

SquareMatrix::SquareMatrix(size_t n, doublereal v)  :
    DenseMatrix(n, n, v),
    GeneralMatrix(0),
    m_factored(0),
    a1norm_(0.0),
    useQR_(0)

{
}

SquareMatrix::SquareMatrix(const SquareMatrix& y) :
    DenseMatrix(y),
    GeneralMatrix(0),
    m_factored(y.m_factored),
    a1norm_(y.a1norm_),
    useQR_(y.useQR_)
{
}

SquareMatrix& SquareMatrix::operator=(const SquareMatrix& y)
{
    if (&y == this) {
        return *this;
    }
    DenseMatrix::operator=(y);
    GeneralMatrix::operator=(y);
    m_factored = y.m_factored;
    a1norm_ = y.a1norm_;
    useQR_ = y.useQR_;
    return *this;
}

int SquareMatrix::solve(doublereal* b, size_t nrhs, size_t ldb)
{
    if (useQR_) {
        return solveQR(b);
    }
    int info=0;
    /*
     * Check to see whether the matrix has been factored.
     */
    if (!m_factored) {
        int retn = factor();
        if (retn) {
            return retn;
        }
    }
    if (ldb == 0) {
        ldb = nColumns();
    }
    /*
     * Solve the factored system
     */
    ct_dgetrs(ctlapack::NoTranspose, static_cast<int>(nRows()),
              nrhs, &(*(begin())), static_cast<int>(nRows()),
              DATA_PTR(ipiv()), b, ldb, info);
    if (info != 0) {
        if (m_printLevel) {
            writelogf("SquareMatrix::solve(): DGETRS returned INFO = %d\n", info);
        }
        if (! m_useReturnErrorCode) {
            throw CELapackError("SquareMatrix::solve()", "DGETRS returned INFO = " + int2str(info));
        }
    }
    return info;
}

void SquareMatrix::zero()
{
    size_t n = nRows();
    if (n > 0) {
        size_t nn = n * n;
        double* sm = &m_data[0];
        /*
         * Using memset is the fastest way to zero a contiguous
         * section of memory.
         */
        (void) memset((void*) sm, 0, nn * sizeof(double));
    }
}

void SquareMatrix::resize(size_t n, size_t m, doublereal v)
{
    DenseMatrix::resize(n, m, v);
}

void  SquareMatrix::mult(const doublereal* b, doublereal* prod) const
{
    DenseMatrix::mult(b, prod);
}

void SquareMatrix::mult(const DenseMatrix& b, DenseMatrix& prod) const
{
    DenseMatrix::mult(b, prod);
}

void  SquareMatrix::leftMult(const doublereal* const b, doublereal* const prod) const
{
    DenseMatrix::leftMult(b, prod);
}

int SquareMatrix::factor()
{
    if (useQR_) {
        return factorQR();
    }
    a1norm_ = ct_dlange('1', m_nrows, m_nrows, &(*(begin())), m_nrows, DATA_PTR(work));
    integer n = static_cast<int>(nRows());
    int info=0;
    m_factored = 1;
    ct_dgetrf(n, n, &(*(begin())), static_cast<int>(nRows()), DATA_PTR(ipiv()), info);
    if (info != 0) {
        if (m_printLevel) {
            writelogf("SquareMatrix::factor(): DGETRS returned INFO = %d\n", info);
        }
        if (! m_useReturnErrorCode) {
            throw CELapackError("SquareMatrix::factor()", "DGETRS returned INFO = "+int2str(info));
        }
    }
    return info;
}

void SquareMatrix::clearFactorFlag()
{
    m_factored = 0;
}

void SquareMatrix::setFactorFlag()
{
    m_factored = 1;
}

int SquareMatrix::factorQR()
{
    if (tau.size() < m_nrows)  {
        tau.resize(m_nrows, 0.0);
        work.resize(8 * m_nrows, 0.0);
    }<commit_msg>[Numerics] Fix invalid dereference in SquareMatrix::factor<commit_after>/**
 *  @file SquareMatrix.cpp
 */

/*
 * Copyright 2004 Sandia Corporation. Under the terms of Contract
 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
 * retains certain rights in this software.
 * See file License.txt for licensing information.
 */

#include "cantera/base/stringUtils.h"
#include "cantera/numerics/ctlapack.h"
#include "cantera/numerics/SquareMatrix.h"
#include "cantera/base/global.h"

using namespace std;

namespace Cantera
{

SquareMatrix::SquareMatrix() :
    DenseMatrix(),
    GeneralMatrix(0),
    m_factored(0),
    a1norm_(0.0),
    useQR_(0)
{
}

SquareMatrix::SquareMatrix(size_t n, doublereal v)  :
    DenseMatrix(n, n, v),
    GeneralMatrix(0),
    m_factored(0),
    a1norm_(0.0),
    useQR_(0)

{
}

SquareMatrix::SquareMatrix(const SquareMatrix& y) :
    DenseMatrix(y),
    GeneralMatrix(0),
    m_factored(y.m_factored),
    a1norm_(y.a1norm_),
    useQR_(y.useQR_)
{
}

SquareMatrix& SquareMatrix::operator=(const SquareMatrix& y)
{
    if (&y == this) {
        return *this;
    }
    DenseMatrix::operator=(y);
    GeneralMatrix::operator=(y);
    m_factored = y.m_factored;
    a1norm_ = y.a1norm_;
    useQR_ = y.useQR_;
    return *this;
}

int SquareMatrix::solve(doublereal* b, size_t nrhs, size_t ldb)
{
    if (useQR_) {
        return solveQR(b);
    }
    int info=0;
    /*
     * Check to see whether the matrix has been factored.
     */
    if (!m_factored) {
        int retn = factor();
        if (retn) {
            return retn;
        }
    }
    if (ldb == 0) {
        ldb = nColumns();
    }
    /*
     * Solve the factored system
     */
    ct_dgetrs(ctlapack::NoTranspose, static_cast<int>(nRows()),
              nrhs, &(*(begin())), static_cast<int>(nRows()),
              DATA_PTR(ipiv()), b, ldb, info);
    if (info != 0) {
        if (m_printLevel) {
            writelogf("SquareMatrix::solve(): DGETRS returned INFO = %d\n", info);
        }
        if (! m_useReturnErrorCode) {
            throw CELapackError("SquareMatrix::solve()", "DGETRS returned INFO = " + int2str(info));
        }
    }
    return info;
}

void SquareMatrix::zero()
{
    size_t n = nRows();
    if (n > 0) {
        size_t nn = n * n;
        double* sm = &m_data[0];
        /*
         * Using memset is the fastest way to zero a contiguous
         * section of memory.
         */
        (void) memset((void*) sm, 0, nn * sizeof(double));
    }
}

void SquareMatrix::resize(size_t n, size_t m, doublereal v)
{
    DenseMatrix::resize(n, m, v);
}

void  SquareMatrix::mult(const doublereal* b, doublereal* prod) const
{
    DenseMatrix::mult(b, prod);
}

void SquareMatrix::mult(const DenseMatrix& b, DenseMatrix& prod) const
{
    DenseMatrix::mult(b, prod);
}

void  SquareMatrix::leftMult(const doublereal* const b, doublereal* const prod) const
{
    DenseMatrix::leftMult(b, prod);
}

int SquareMatrix::factor()
{
    if (useQR_) {
        return factorQR();
    }
    a1norm_ = ct_dlange('1', m_nrows, m_nrows, &(*(begin())), m_nrows, 0);
    integer n = static_cast<int>(nRows());
    int info=0;
    m_factored = 1;
    ct_dgetrf(n, n, &(*(begin())), static_cast<int>(nRows()), DATA_PTR(ipiv()), info);
    if (info != 0) {
        if (m_printLevel) {
            writelogf("SquareMatrix::factor(): DGETRS returned INFO = %d\n", info);
        }
        if (! m_useReturnErrorCode) {
            throw CELapackError("SquareMatrix::factor()", "DGETRS returned INFO = "+int2str(info));
        }
    }
    return info;
}

void SquareMatrix::clearFactorFlag()
{
    m_factored = 0;
}

void SquareMatrix::setFactorFlag()
{
    m_factored = 1;
}

int SquareMatrix::factorQR()
{
    if (tau.size() < m_nrows)  {
        tau.resize(m_nrows, 0.0);
        work.resize(8 * m_nrows, 0.0);
    }
{'fraction_non_alphanumeric': 0.148380355276907, 'fraction_numerical': 0.01319226750261233, 'mean_word_length': 2.7719211822660097, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2736458', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2760, 'n_words': 597}