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 ///THIS CODE NEEDS WORK.
23 ///Class: Output of member functions of InBinom
24 //TODO:ADD DLL
25 public class OutBinom(X)  if(is(X : real))	{ //Class for the output of functions involving binomials
26 	private:
27 	uint[] coefficiants;
28 	X[] outvals;
29 	X sum_of_outvals = 0;
30 	X x = 0;
31 	X y = 0;
32 	uint n = 0;
33 	public:
34 		this(const uint[] coefficant, const X[] outvals)	{
35 			this.coefficiants.length = coefficant.length;
36 			this.outvals.length = outvals.length;
37 			for(int i = 0; i < coefficant.length; i++)	{
38 				this.coefficiants[i] = coefficant[i];
39 			}
40 
41 			for(int i = 0; i < outvals.length; i++)	{
42 				this.outvals[i] = outvals[i];
43 				this.sum_of_outvals += outvals[i];
44 			}
45 		}
46 
47 		this(const uint[] coefficiant, const X[] outvals, const X x, const X y, const uint n)	{
48 			this.coefficiants.length = coefficiant.length;
49 			this.outvals.length = outvals.length;
50 			for(int i = 0; i < coefficiant.length; i++)	{
51 				this.coefficiants[i] = coefficiant[i];
52 			}
53 
54 			for(int i = 0; i < outvals.length; i++)	{
55 				this.outvals[i] = outvals[i];
56 				this.sum_of_outvals += outvals[i];
57 			}
58 
59 			this.x = x;
60 			this.y = y;
61 			this.n = n;
62 		}
63 		///The result generated by the binomial theorem.
64 		pure X result() const @property	@safe nothrow {
65 			return this.sum_of_outvals;
66 		}
67 
68 		  ///All of the values that InBinom.BinomEqu() spews out without summing it all.
69 		pure X[] seperatedvals() const @property @safe nothrow	{
70 			return this.outvals.dup;
71 		}
72 		
73 		  ///All of the coefficiants generated by InBionm.BinomEqu() as an array.
74 		pure nothrow uint[] coefficients() const @property @safe	{
75 			return this.coefficiants.dup;
76 		}
77 		
78 		  ///Value of OutBinom.x
79 		pure nothrow X xval() const @property @safe	{
80 			return this.x;
81 		}
82 		
83         ///Value of OutBinom.y.
84 		pure nothrow X yval() const @property @safe	{
85 			return this.y;
86 		}
87 		
88 		  ///Value of OutBinom.n.
89 		pure nothrow uint nval() const @property @safe	{
90 			return this.n;
91 		}	
92 }
93 
94   /**The factorial of a number.
95   Params:
96   f = 	is the number that the factorial is being performed on.
97   Returns: The factorial of f.*/
98 public uint factorial(uint f) pure nothrow @safe	{ //Bug Free
99 	if(f == 0 || f == 1)
100 		return 1;
101 	for(int x = f-1; x > 0; x--)	{
102 		f = f * x;
103 	}
104 	return f;
105 }
106 
107   ///Serves as a set up binomial to perform operations on.
108 public class InBinom(X) if(is(X : real))	{	//Class for the binomials(input)
109 	private:
110 		X x;
111 		X y;
112 		uint n;
113 	public:
114 		this(X x, X y, uint n)	{
115 			this.x = x;
116 			this.y = y;
117 			this.n = n;
118 		}
119 		
120 		  /**Performs the binomial theorem on the object that it is a member of.
121 		  Params:
122 		  none
123 		  Returns: OutBinom!X containing the result of applying the binomial theorem on the object.*/
124 		OutBinom!X BinomEqu()	{ //Implements the Binomial Theorem
125 			uint[] coff;
126 			coff.length = this.n+1;
127 			X[] outval;
128 			outval.length = this.n+1;
129 			for(uint k = 0; k <= this.n;k++)	{
130 				coff[k] = (factorial(this.n));
131 				coff[k]  /= (factorial(this.n - k) * factorial(k));
132 				outval[k] = (coff[k] * ((this.x ^^ (this.n-k)) * (this.y ^^ (k))));
133 			}
134 
135 			auto ret = new OutBinom!(typeof(this.x))(coff,outval);
136 			return ret;
137 		}
138 }
139