<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

using ComputerAlgebra;
using ComputerAlgebra.Plotting;
using ComputerAlgebra.LinqCompiler;

using LinqExprs = System.Linq.Expressions;
using LinqExpr = System.Linq.Expressions.Expression;

namespace Demo
{
    // This program demonstrates how the ComputerAlgebra namespace can provide solutions to 
    // problems with the flexibility of runtime computed solutions to algebraic problems,
    // while maintaining the performance of natively coded solutions.

    // This example builds a simulation of the competitive Lotka-Volterra equations, for more 
    // information, see http://en.wikipedia.org/wiki/Competitive_Lotka%E2%80%93Volterra_equations

    /// <summary>
    /// This class describes an ecosystem for a set of species.
    /// </summary>
    class PopulationSystem
    {
        private double[] _x;
        /// <summary>
        /// The current population x of the system.
        /// </summary>
        public double[] x { get { return _x; } }

        /// <summary>
        /// The number of species in this system.
        /// </summary>
        public int N { get { return _x.Length; } }

        private double[,] _A;
        /// <summary>
        /// The community matrix A of the system.
        /// </summary>
        public double[,] A { get { return _A; } }

        private double[] _r;
        /// <summary>
        /// The intrinsic birth/death rates r of the system.
        /// </summary>
        public double[] r { get { return _r; } }


        /// <summary>
        /// Construct a new population system with N species.
        /// </summary>
        /// <param name="N"></param>
        public PopulationSystem(int N)
        {
            _A = new double[N, N];
            _r = new double[N];
            _x = new double[N];
        }

        /// <summary>
        /// Construct a new populatuion system with the given initial population.
        /// </summary>
        /// <param name="x0"></param>
        public PopulationSystem(double[] x0) : this(x0.Length)
        {
            for (int i = 0; i < x0.Length; ++i)
                _x[i] = x0[i];
        }
    };

    class Program
    {
        // Run the simulation of the population system S for N timesteps of length dt.
        // The population at each timestep is stored in the rows of Data.
        static void SimulateNative(int N, double dt, double[,] Data, PopulationSystem S)
        {
            // Loop over the sample range requested.
            for (int n = 1; n < N; ++n)
            {
                // Loop over the species and compute the population change.
                for (int i = 0; i < S.N; ++i)
                {
                    double dx_dt = 1.0;
                    for (int j = 0; j < S.N; ++j)
                        dx_dt -= S.A[i, j] * Data[n - 1, j];
                    dx_dt *= S.r[i] * Data[n - 1, i];

                    Data[n, i] = Data[n - 1, i] + dt * dx_dt;
                }
            }
        }
        
        // Define a function for running the simulation of a population system S with timestep
        // dt. The number of timesteps and the data buffer are parameters of the defined function.
        static Func<int, double[,], int> DefineSimulate(double dt, PopulationSystem S)
        {
            CodeGen code = new CodeGen();

            // Define a parameter for the current population x, and define mappings to the 
            // expressions defined above.
            LinqExpr N = code.Decl<int>(Scope.Parameter, "N");
            LinqExpr Data = code.Decl<double[,]>(Scope.Parameter, "Data");

            // Loop over the sample range requested. Note that this loop is a 'runtime' loop,
            // while the rest of the loops nested in the body of this loop are 'compile time' loops.
            LinqExpr n = code.DeclInit<int>("n", 1);
            code.For(
                () => { }, 
                LinqExpr.LessThan(n, N), 
                () => code.Add(LinqExpr.PostIncrementAssign(n)), 
                () =>
            {
                // Define expressions representing the population of each species.
                List<Expression> x = new List<Expression>();
                for (int i = 0; i < S.N; ++i)
                {
                    // Define a variable xi.
                    Expression xi = "x" + i.ToString();
                    x.Add(xi);
                    // xi = Data[n, i].
                    code.DeclInit(xi, LinqExpr.ArrayAccess(Data, LinqExpr.Subtract(n, LinqExpr.Constant(1)), LinqExpr.Constant(i)));
                }

                for (int i = 0; i < S.N; ++i)
                {
                    // This list is the elements of the sum representing the i'th 
                    // row of f, i.e. r_i + (A*x)_i.
                    Expression dx_dt = 1;
                    List<Expression> terms = new List<Expression>();
                    for (int j = 0; j < S.N; ++j)
                        dx_dt -= S.A[i, j] * x[j];

                    // Define dx_i/dt = x_i * f_i(x), as per the Lotka-Volterra equations.
                    dx_dt *= x[i] * S.r[i];

                    // Euler's method for x(t) is: x(t) = x(t - h) + h * x'(t - h).
                    Expression integral = x[i] + dt * dx_dt;

                    // Data[n, i] = Data[n - 1, i] + dt * dx_dt;<commit_msg>Removed old code<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

using ComputerAlgebra;
using ComputerAlgebra.Plotting;
using ComputerAlgebra.LinqCompiler;

using LinqExprs = System.Linq.Expressions;
using LinqExpr = System.Linq.Expressions.Expression;

namespace Demo
{
    // This program demonstrates how the ComputerAlgebra namespace can provide solutions to 
    // problems with the flexibility of runtime computed solutions to algebraic problems,
    // while maintaining the performance of natively coded solutions.

    // This example builds a simulation of the competitive Lotka-Volterra equations, for more 
    // information, see http://en.wikipedia.org/wiki/Competitive_Lotka%E2%80%93Volterra_equations

    /// <summary>
    /// This class describes an ecosystem for a set of species.
    /// </summary>
    class PopulationSystem
    {
        private double[] _x;
        /// <summary>
        /// The current population x of the system.
        /// </summary>
        public double[] x { get { return _x; } }

        /// <summary>
        /// The number of species in this system.
        /// </summary>
        public int N { get { return _x.Length; } }

        private double[,] _A;
        /// <summary>
        /// The community matrix A of the system.
        /// </summary>
        public double[,] A { get { return _A; } }

        private double[] _r;
        /// <summary>
        /// The intrinsic birth/death rates r of the system.
        /// </summary>
        public double[] r { get { return _r; } }


        /// <summary>
        /// Construct a new population system with N species.
        /// </summary>
        /// <param name="N"></param>
        public PopulationSystem(int N)
        {
            _A = new double[N, N];
            _r = new double[N];
            _x = new double[N];
        }

        /// <summary>
        /// Construct a new populatuion system with the given initial population.
        /// </summary>
        /// <param name="x0"></param>
        public PopulationSystem(double[] x0) : this(x0.Length)
        {
            for (int i = 0; i < x0.Length; ++i)
                _x[i] = x0[i];
        }
    };

    class Program
    {
        // Run the simulation of the population system S for N timesteps of length dt.
        // The population at each timestep is stored in the rows of Data.
        static void SimulateNative(int N, double dt, double[,] Data, PopulationSystem S)
        {
            // Loop over the sample range requested.
            for (int n = 1; n < N; ++n)
            {
                // Loop over the species and compute the population change.
                for (int i = 0; i < S.N; ++i)
                {
                    double dx_dt = 1.0;
                    for (int j = 0; j < S.N; ++j)
                        dx_dt -= S.A[i, j] * Data[n - 1, j];
                    dx_dt *= S.r[i] * Data[n - 1, i];

                    Data[n, i] = Data[n - 1, i] + dt * dx_dt;
                }
            }
        }
        
        // Define a function for running the simulation of a population system S with timestep
        // dt. The number of timesteps and the data buffer are parameters of the defined function.
        static Func<int, double[,], int> DefineSimulate(double dt, PopulationSystem S)
        {
            CodeGen code = new CodeGen();

            // Define a parameter for the current population x, and define mappings to the 
            // expressions defined above.
            LinqExpr N = code.Decl<int>(Scope.Parameter, "N");
            LinqExpr Data = code.Decl<double[,]>(Scope.Parameter, "Data");

            // Loop over the sample range requested. Note that this loop is a 'runtime' loop,
            // while the rest of the loops nested in the body of this loop are 'compile time' loops.
            LinqExpr n = code.DeclInit<int>("n", 1);
            code.For(
                () => { }, 
                LinqExpr.LessThan(n, N), 
                () => code.Add(LinqExpr.PostIncrementAssign(n)), 
                () =>
            {
                // Define expressions representing the population of each species.
                List<Expression> x = new List<Expression>();
                for (int i = 0; i < S.N; ++i)
                {
                    // Define a variable xi.
                    Expression xi = "x" + i.ToString();
                    x.Add(xi);
                    // xi = Data[n, i].
                    code.DeclInit(xi, LinqExpr.ArrayAccess(Data, LinqExpr.Subtract(n, LinqExpr.Constant(1)), LinqExpr.Constant(i)));
                }

                for (int i = 0; i < S.N; ++i)
                {
                    // This list is the elements of the sum representing the i'th 
                    // row of f, i.e. r_i + (A*x)_i.
                    Expression dx_dt = 1;
                    for (int j = 0; j < S.N; ++j)
                        dx_dt -= S.A[i, j] * x[j];

                    // Define dx_i/dt = x_i * f_i(x), as per the Lotka-Volterra equations.
                    dx_dt *= x[i] * S.r[i];

                    // Euler's method for x(t) is: x(t) = x(t - h) + h * x'(t - h).
                    Expression integral = x[i] + dt * dx_dt;

                    // Data[n, i] = Data[n - 1, i] + dt * dx_dt;
{'fraction_non_alphanumeric': 0.11575857640799853, 'fraction_numerical': 0.004769767015226564, 'mean_word_length': 1.4881332724783205, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 59, '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': '4911889', 'n_tokens_mistral': 3148, 'n_tokens_neox': 2969, 'n_words': 1035}