<commit_before>/*
 * AbstractLikelihoodCore.java
 *
 * Copyright (C) 2002-2006 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.evomodel.newtreelikelihood;

import dr.evomodel.substmodel.SubstitutionModel;
import dr.evomodel.sitemodel.SiteModel;

public class FloatGeneralLikelihoodCore implements LikelihoodCore {

    protected int stateCount;
    protected int nodeCount;
    protected int patternCount;
    protected int partialsSize;
    protected int matrixSize;
    protected int matrixCount;

    protected float[] cMatrix;
    protected float[] storedCMatrix;
    protected float[] eigenValues;
    protected float[] storedEigenValues;

    protected float[] frequencies;
    protected float[] storedFrequencies;
    protected float[] categoryProportions;
    protected float[] storedCategoryProportions;
    protected float[] categoryRates;
    protected float[] storedCategoryRates;

    protected float[][][] partials;

    protected int[][] states;

    protected float[][][] matrices;

    protected int[] currentMatricesIndices;
    protected int[] storedMatricesIndices;
    protected int[] currentPartialsIndices;
    protected int[] storedPartialsIndices;


    /**
     * Constructor
     *
     * @param stateCount number of states
     */
    public FloatGeneralLikelihoodCore(int stateCount) {
        this.stateCount = stateCount;
    }

    /**
     * initializes partial likelihood arrays.
     *
     * @param nodeCount           the number of nodes in the tree
     * @param patternCount        the number of patterns
     * @param matrixCount         the number of matrices (i.e., number of categories)
     */
    public void initialize(int nodeCount, int patternCount, int matrixCount) {

        this.nodeCount = nodeCount;
        this.patternCount = patternCount;
        this.matrixCount = matrixCount;

        cMatrix = new float[stateCount * stateCount * stateCount];
        storedCMatrix = new float[stateCount * stateCount * stateCount];

        eigenValues = new float[stateCount];
        storedEigenValues = new float[stateCount];

        frequencies = new float[stateCount];
        storedFrequencies = new float[stateCount];

        categoryRates = new float[matrixCount];
        storedCategoryRates = new float[matrixCount];

        categoryProportions = new float[matrixCount];
        storedCategoryProportions = new float[matrixCount];

        partialsSize = patternCount * stateCount * matrixCount;

        partials = new float[2][nodeCount][];

        currentMatricesIndices = new int[nodeCount];
        storedMatricesIndices = new int[nodeCount];

        currentPartialsIndices = new int[nodeCount];
        storedPartialsIndices = new int[nodeCount];

        states = new int[nodeCount][];

        for (int i = 0; i < nodeCount; i++) {
            partials[0][i] = new float[partialsSize];
            partials[1][i] = new float[partialsSize];
        }

        matrixSize = stateCount * stateCount;

        matrices = new float[2][nodeCount][matrixCount * matrixSize];
    }

    /**
     * cleans up and deallocates arrays.
     */
    public void finalize() throws Throwable  {
        super.finalize();

        nodeCount = 0;
        patternCount = 0;
        matrixCount = 0;

        partials = null;
        currentPartialsIndices = null;
        storedPartialsIndices = null;
        states = null;
        matrices = null;
        currentMatricesIndices = null;
        storedMatricesIndices = null;
    }


    /**
     * Sets partials for a tip
     */
    public void setTipPartials(int tipIndex, double[] partials) {

        int k = 0;
        for (int i = 0; i < matrixCount; i++) {
            for (int j = 0; j < partials.length; j++) {
                this.partials[0][tipIndex][k] = (float)partials[j];
                k++;
            }
        }
    }

    /**
     * Called when the substitution model has been updated so precalculations
     * can be obtained.
     */
    public void updateSubstitutionModel(SubstitutionModel substitutionModel) {
        System.arraycopy(substitutionModel.getFrequencyModel().getFrequencies(), 0, frequencies, 0, frequencies.length);

        double[][] Evec = substitutionModel.getEigenVectors();
        double[][] Ievc = substitutionModel.getInverseEigenVectors();
        int l =0;
        for (int i = 0; i < stateCount; i++) {<commit_msg>Need to manually convert double[] freqs -> float[] freqs; array-copy does not work.<commit_after>/*
 * AbstractLikelihoodCore.java
 *
 * Copyright (C) 2002-2006 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.evomodel.newtreelikelihood;

import dr.evomodel.substmodel.SubstitutionModel;
import dr.evomodel.sitemodel.SiteModel;

public class FloatGeneralLikelihoodCore implements LikelihoodCore {

    protected int stateCount;
    protected int nodeCount;
    protected int patternCount;
    protected int partialsSize;
    protected int matrixSize;
    protected int matrixCount;

    protected float[] cMatrix;
    protected float[] storedCMatrix;
    protected float[] eigenValues;
    protected float[] storedEigenValues;

    protected float[] frequencies;
    protected float[] storedFrequencies;
    protected float[] categoryProportions;
    protected float[] storedCategoryProportions;
    protected float[] categoryRates;
    protected float[] storedCategoryRates;

    protected float[][][] partials;

    protected int[][] states;

    protected float[][][] matrices;

    protected int[] currentMatricesIndices;
    protected int[] storedMatricesIndices;
    protected int[] currentPartialsIndices;
    protected int[] storedPartialsIndices;


    /**
     * Constructor
     *
     * @param stateCount number of states
     */
    public FloatGeneralLikelihoodCore(int stateCount) {
        this.stateCount = stateCount;
    }

    /**
     * initializes partial likelihood arrays.
     *
     * @param nodeCount           the number of nodes in the tree
     * @param patternCount        the number of patterns
     * @param matrixCount         the number of matrices (i.e., number of categories)
     */
    public void initialize(int nodeCount, int patternCount, int matrixCount) {

        this.nodeCount = nodeCount;
        this.patternCount = patternCount;
        this.matrixCount = matrixCount;

        cMatrix = new float[stateCount * stateCount * stateCount];
        storedCMatrix = new float[stateCount * stateCount * stateCount];

        eigenValues = new float[stateCount];
        storedEigenValues = new float[stateCount];

        frequencies = new float[stateCount];
        storedFrequencies = new float[stateCount];

        categoryRates = new float[matrixCount];
        storedCategoryRates = new float[matrixCount];

        categoryProportions = new float[matrixCount];
        storedCategoryProportions = new float[matrixCount];

        partialsSize = patternCount * stateCount * matrixCount;

        partials = new float[2][nodeCount][];

        currentMatricesIndices = new int[nodeCount];
        storedMatricesIndices = new int[nodeCount];

        currentPartialsIndices = new int[nodeCount];
        storedPartialsIndices = new int[nodeCount];

        states = new int[nodeCount][];

        for (int i = 0; i < nodeCount; i++) {
            partials[0][i] = new float[partialsSize];
            partials[1][i] = new float[partialsSize];
        }

        matrixSize = stateCount * stateCount;

        matrices = new float[2][nodeCount][matrixCount * matrixSize];
    }

    /**
     * cleans up and deallocates arrays.
     */
    public void finalize() throws Throwable  {
        super.finalize();

        nodeCount = 0;
        patternCount = 0;
        matrixCount = 0;

        partials = null;
        currentPartialsIndices = null;
        storedPartialsIndices = null;
        states = null;
        matrices = null;
        currentMatricesIndices = null;
        storedMatricesIndices = null;
    }


    /**
     * Sets partials for a tip
     */
    public void setTipPartials(int tipIndex, double[] partials) {

        int k = 0;
        for (int i = 0; i < matrixCount; i++) {
            for (int j = 0; j < partials.length; j++) {
                this.partials[0][tipIndex][k] = (float)partials[j];
                k++;
            }
        }
    }

    /**
     * Called when the substitution model has been updated so precalculations
     * can be obtained.
     */
    public void updateSubstitutionModel(SubstitutionModel substitutionModel) {
//        System.arraycopy(substitutionModel.getFrequencyModel().getFrequencies(), 0, frequencies, 0, frequencies.length);
    	// Must down-cast from double to float
    	double[] doubleFreqs = substitutionModel.getFrequencyModel().getFrequencies();
    	for(int i=0; i<frequencies.length; i++)
    		frequencies[i] = (float) doubleFreqs[i];

        double[][] Evec = substitutionModel.getEigenVectors();
        double[][] Ievc = substitutionModel.getInverseEigenVectors();
        int l =0;
        for (int i = 0; i < stateCount; i++) {
{'fraction_non_alphanumeric': 0.08664561664190193, 'fraction_numerical': 0.0067793462109955425, 'mean_word_length': 2.8323843416370105, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 8, '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': '1890069', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2859, 'n_words': 933}