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, michaeleverestc79@gmail.com*/
18 /**Date: October 1, 2021*/
19 /** License:  GPL-3.0**/
20 
21 module dutils.binom;
22 
23 /**Class: Output of member functions of InBinom*/
24 public class OutBinom(X)  if(is(X : real))	{ //Class for the output of functions involving binomials
25 	private:
26 	uint[] coefficiants;
27 	X[] outvals;
28 	X sum_of_outvals = 0;
29 	X x = 0;
30 	X y = 0;
31 	uint n = 0;
32 	public:
33 		this(const uint[] coefficant, const X[] outvals)	{
34 			this.coefficiants.length = coefficant.length;
35 			this.outvals.length = outvals.length;
36 			for(int i = 0; i < coefficant.length; i++)	{
37 				this.coefficiants[i] = coefficant[i];
38 			}
39 
40 			for(int i = 0; i < outvals.length; i++)	{
41 				this.outvals[i] = outvals[i];
42 				this.sum_of_outvals += outvals[i];
43 			}
44 		}
45 
46 		this(const uint[] coefficiant, const X[] outvals, const X x, const X y, const uint n)	{
47 			this.coefficiants.length = coefficiant.length;
48 			this.outvals.length = outvals.length;
49 			for(int i = 0; i < coefficiant.length; i++)	{
50 				this.coefficiants[i] = coefficiant[i];
51 			}
52 
53 			for(int i = 0; i < outvals.length; i++)	{
54 				this.outvals[i] = outvals[i];
55 				this.sum_of_outvals += outvals[i];
56 			}
57 
58 			this.x = x;
59 			this.y = y;
60 			this.n = n;
61 		}
62 		/**
63 		  * OutBinom.result returns the result generated by the binomial theroem.
64 		  * Params:
65 		  * none
66 		  * Returns:
67 		  * Result generated by calling InBinom.BinomEqu()
68 		*/
69 		pure X result() const @property	@safe nothrow {
70 			return this.sum_of_outvals;
71 		}
72 		/**
73 		  * OutBinom.seperatedVals returns all of the values that InBinom.BinomEqu() spews out without summing it all.
74 		  * Params:
75 		  * none
76 		  * Returns:
77 		  * All the values generated by InBinom.BinomEqu() beffore summing.
78 		*/
79 		pure X[] seperatedvals() const @property @safe nothrow	{
80 			return this.outvals.dup;
81 		}
82 		/**
83 		  * OutBinom.coefficients returns all of the coefficiants generated by InBionm.BinomEqu() as an array.
84 		  * Params:
85 		  * none
86 		  * Returns:
87 		  * All of the coefficients generated by InBinom.BinomEqu() as an array.
88 		*/
89 		pure nothrow uint[] coefficients() const @property @safe	{
90 			return this.coefficiants.dup;
91 		}
92 		/**
93 		  * OutBinom.xval returns the value of x.
94 		  * Params:
95 		  * none
96 		  * Returns:
97 		  * Value of 'OutBinom.x.'
98 		*/
99 		pure nothrow X xval() const @property @safe	{
100 			return this.x;
101 		}
102 		/**
103 		  * OutBinom.yval returns the value of y.
104 		  * Params:
105 		  * none
106 		  * Returns:
107 		  * Value of 'OutBinom.y.'
108 		*/
109 		pure nothrow X yval() const @property @safe	{
110 			return this.y;
111 		}
112 		/**
113 		  * OutBinom.nval returns the value of n.
114 		  * Params:
115 		  * none
116 		  * Returns:
117 		  * Value of 'OutBinom.n' as in 'n' Choose 'k.'
118 		*/
119 		pure nothrow uint nval() const @property @safe	{
120 			return this.n;
121 		}	
122 }
123 /**********************************************************************
124   * This function returns the factorial of a number.
125   * Params:
126   *	f = 	is the number that the factorial is being performed on.
127   * Returns: The factorial of f.
128 */
129 public uint factorial(const uint f) pure nothrow @safe	{ //Bug Free
130 	if(f == 0 || f == 1)
131 		return 1;
132 	for(int x = f-1; x > 0; x--)	{
133 		f = f * x;
134 	}
135 	return f;
136 }
137 /***************************************************************
138   * Class:  Serves as a set up binomial to perform operations on.*/
139 public class InBinom(X) if(is(X : real))	{	//Class for the binomials(input)
140 	private:
141 		X x;
142 		X y;
143 		uint n;
144 	public:
145 		this(X x, X y, uint n)	{
146 			this.x = x;
147 			this.y = y;
148 			this.n = n;
149 		}
150 		/********************************************************************************************
151 		  * BinomEqu perofrms the binomial theorem on the object that it is a member of.
152 		  * Params:
153 		  * none
154 		  * Returns: OutBinom!X containing the result of applying the binomial theorem on the object.
155 		*/
156 		OutBinom!X BinomEqu()	{ //Implements the Binomial Theorem
157 			uint[] coff;
158 			coff.length = this.n+1;
159 			X[] outval;
160 			outval.length = this.n+1;
161 			for(uint k = 0; k <= this.n;k++)	{
162 				coff[k] = (factorial(this.n));
163 				coff[k]  /= (factorial(this.n - k) * factorial(k));
164 				outval[k] = (coff[k] * ((this.x ^^ (this.n-k)) * (this.y ^^ (k))));
165 			}
166 
167 			auto ret = new OutBinom!(typeof(this.x))(coff,outval);
168 			return ret;
169 		}
170 }