-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirivative.java
155 lines (150 loc) · 5.06 KB
/
Dirivative.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Write a description of class Dirivative here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Dirivative
{
double[] coefficients;
int start,stop;
/**
* Constructs a Dirivative of a given polynomial.
* @param p this object is a divative of Polynomial p
*/
public Dirivative(Polynomial p)//Jacob Waters
{
coefficients=new double[p.getCoefficients().length-1];
for(int i=0;i<coefficients.length;i++)
{
coefficients[i]=p.getCoefficients()[i]*(p.getCoefficients().length-i-1);
}
this.start=p.getStart();this.stop=p.getStop();
}
/**
* @return returns the coefficients of this Dirivative I.E. ax^2+bx+c returns a,b,c.
*/
public double[] getCoefficients()//Jacob Waters
{
return coefficients;
}
/**
* Finds all values where f(x)=value
* @param value The value being searched for.
*/
public int[] equals(double value)//deal with imprecision issue. ex y=.0001, looking for 0 //Jacob Waters
{
double heightX1,heightX2;
int[] returns=new int[0];
for(int x=start;x<=stop;x++)
{
if(((heightX1=this.height(x)-value)>0&&((heightX2=this.height(x+1)-value)<0)))
{
if(Math.abs(heightX1)>=Math.abs(heightX2))
{
int[] temp=new int[returns.length+1];
for(int i=0;i<returns.length;i++)
{
temp[i]=returns[i];
}
temp[temp.length-1]=x+1;
returns=temp;
}
else
{
int[] temp=new int[returns.length+1];
for(int i=0;i<returns.length;i++)
{
temp[i]=returns[i];
}
temp[temp.length-1]=x;
returns=temp;
}
}
else if((heightX1=this.height(x)-value)<0&&((heightX2=this.height(x+1)-value)>0))
{
if(Math.abs(heightX1)>=Math.abs(heightX2))
{
int[] temp=new int[returns.length+1];
for(int i=0;i<returns.length;i++)
{
temp[i]=returns[i];
}
temp[temp.length-1]=x+1;
returns=temp;
}
else
{
int[] temp=new int[returns.length+1];
for(int i=0;i<returns.length;i++)
{
temp[i]=returns[i];
}
temp[temp.length-1]=x;
returns=temp;
}
}
else if((heightX1=this.height(x)-value)==0)
{
int[] temp=new int[returns.length+1];
for(int i=0;i<returns.length;i++)
{
temp[i]=returns[i];
}
temp[temp.length-1]=x+1;
returns=temp;
}
}
return returns;
}
/**
* returns the slope at point x on the original function, with emphasis on double pricision.
* It's important to realize that a slope can hold a wide range of values. An int does not suffice.
* @param x the X point on the original function.
* @return the slope at point x of the original Polynomial.
*/
public double height(int x)//Jacob Waters
{
double heightX=0;
for(int a=0;a<coefficients.length;a++)
{
heightX+=coefficients[a]*Math.pow(x,coefficients.length-a-1);
}
return heightX;
}
/**
* Prints a summary of the important data in a Dirivative.
*/
public void print()//Jacob Waters
{
for(int i=0;i<coefficients.length;i++)
{
System.out.println((char)(i+'A')+"= "+coefficients[i]);
}
System.out.println();
for(int i=0;i<coefficients.length;i++)
{
if(i<coefficients.length-2)
{
if(coefficients[i]>0)
System.out.print("+"+coefficients[i]+"(X^"+(coefficients.length-i-1)+")");
else if(coefficients[i]<0)
System.out.print(coefficients[i]+"(X^"+(coefficients.length-i-1)+")");
}
else if(i==coefficients.length-2)
{
if(coefficients[i]>0)
System.out.print("+"+coefficients[i]+"X");
else if(coefficients[i]<0)
System.out.print(coefficients[i]+"X");
}
else
{
if(coefficients[i]>0)
System.out.print("+"+coefficients[i]);
else if(coefficients[i]<0)
System.out.print(coefficients[i]);
}
}
}
}