1 /*binom.d by Ruby The Roobster*/
2 /* Version 0.2.5 Release*/
3 /*Module for handling binomials in the D Programming Languge 2.0*/
4 /*This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
16 /** Copyright: 2021, Ruby The Roobster*/
17 /**Author: Ruby The Roobster, rubytheroobster@yandex.com*/
18 /**Date: October 1, 2021*/
19 /** License:  GPL-3.0**/
20 
21 module dutils.binom;
22 ///THIS CODE NEEDS WORK.
23 ///Class: Output of member functions of InBinom
24 //TODO:ADD DLL
25 //TODO: FIX AND MAKE IT WORK WITH THE MATH LIB
26 public class OutBinom(X)  if(is(X : real))	{ //Class for the output of functions involving binomials
27 	private:
28 	uint[] coefficiants;
29 	X[] outvals;
30 	X sum_of_outvals = 0;
31 	X x = 0;
32 	X y = 0;
33 	uint n = 0;
34 	public:
35 		this(const uint[] coefficant, const X[] outvals)	{
36 			this.coefficiants.length = coefficant.length;
37 			this.outvals.length = outvals.length;
38 			for(int i = 0; i < coefficant.length; i++)	{
39 				this.coefficiants[i] = coefficant[i];
40 			}
41 
42 			for(int i = 0; i < outvals.length; i++)	{
43 				this.outvals[i] = outvals[i];
44 				this.sum_of_outvals += outvals[i];
45 			}
46 		}
47 
48 		this(const uint[] coefficiant, const X[] outvals, const X x, const X y, const uint n)	{
49 			this.coefficiants.length = coefficiant.length;
50 			this.outvals.length = outvals.length;
51 			for(int i = 0; i < coefficiant.length; i++)	{
52 				this.coefficiants[i] = coefficiant[i];
53 			}
54 
55 			for(int i = 0; i < outvals.length; i++)	{
56 				this.outvals[i] = outvals[i];
57 				this.sum_of_outvals += outvals[i];
58 			}
59 
60 			this.x = x;
61 			this.y = y;
62 			this.n = n;
63 		}
64 		///The result generated by the binomial theorem.
65 		pure X result() const @property	@safe nothrow {
66 			return this.sum_of_outvals;
67 		}
68 
69 		  ///All of the values that InBinom.BinomEqu() spews out without summing it all.
70 		pure X[] seperatedvals() const @property @safe nothrow	{
71 			return this.outvals.dup;
72 		}
73 		
74 		  ///All of the coefficiants generated by InBionm.BinomEqu() as an array.
75 		pure nothrow uint[] coefficients() const @property @safe	{
76 			return this.coefficiants.dup;
77 		}
78 		
79 		  ///Value of OutBinom.x
80 		pure nothrow X xval() const @property @safe	{
81 			return this.x;
82 		}
83 		
84         ///Value of OutBinom.y.
85 		pure nothrow X yval() const @property @safe	{
86 			return this.y;
87 		}
88 		
89 		  ///Value of OutBinom.n.
90 		pure nothrow uint nval() const @property @safe	{
91 			return this.n;
92 		}	
93 }
94 
95   /**The factorial of a number.
96   Params:
97   f = 	is the number that the factorial is being performed on.
98   Returns: The factorial of f.*/
99 public uint factorial(uint f) pure nothrow @safe	{ //Bug Free
100 	if(f == 0 || f == 1)
101 		return 1;
102 	for(int x = f-1; x > 0; x--)	{
103 		f = f * x;
104 	}
105 	return f;
106 }
107 
108   ///Serves as a set up binomial to perform operations on.
109 public class InBinom(X) if(is(X : real))	{	//Class for the binomials(input)
110 	private:
111 		X x;
112 		X y;
113 		uint n;
114 	public:
115 		this(X x, X y, uint n)	{
116 			this.x = x;
117 			this.y = y;
118 			this.n = n;
119 		}
120 		
121 		  /**Performs the binomial theorem on the object that it is a member of.
122 		  Params:
123 		  none
124 		  Returns: OutBinom!X containing the result of applying the binomial theorem on the object.*/
125 		OutBinom!X BinomEqu()	{ //Implements the Binomial Theorem
126 			uint[] coff;
127 			coff.length = this.n+1;
128 			X[] outval;
129 			outval.length = this.n+1;
130 			for(uint k = 0; k <= this.n;k++)	{
131 				coff[k] = (factorial(this.n));
132 				coff[k]  /= (factorial(this.n - k) * factorial(k));
133 				outval[k] = (coff[k] * ((this.x ^^ (this.n-k)) * (this.y ^^ (k))));
134 			}
135 
136 			auto ret = new OutBinom!(typeof(this.x))(coff,outval);
137 			return ret;
138 		}
139 }
140