-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx2.java
More file actions
143 lines (120 loc) · 4.55 KB
/
Copy pathEx2.java
File metadata and controls
143 lines (120 loc) · 4.55 KB
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
//לואי שרקיה 206706285
import java.util.Arrays;
public class Ex2 {
public static double f(double[] poly, double x) {
// //-----------------------
double sum = 0;
for (int i = poly.length - 1; i >= 0; i--) {
sum += poly[i] * Math.pow(x, i);
System.out.println(sum);
}
return sum;
// //-----------------------
}
public static String poly(double[] poly) {
// //-----------------------
String ans = "";
for (int i = poly.length - 1; i >= 0; i--) {
if (poly[i] != 0 && i != 0) {
ans += poly[i] + "x" + "^" + i + "+";
}
if (poly[i] != 0 && i == 0) {
ans += poly[i];
}
}
int y = ans.length() - 1;
if (ans.charAt(y) == '+')
ans = ans.substring(0, y);
return ans;
// //-----------------------
}
public static double root(double[] p, double x1, double x2, double eps) {
double c = x1;
while ((x2 - x1) >= eps) {
// Find middle point
c = (x1 + x2) / 2;
// Check if middle point is root
if (f(p, c) == 0.0)
break;
// Decide the side to repeat the steps
else if (f(p, c) * f(p, x1) < 0)
x2 = c;
else
x1 = c;
// prints value of c upto 4 decimal places
// System.out.printf("The value of root is : %.4f", c);
}
return c;
}
// //-------------------------------------------------
public static double[] add(double[] p1, double[] p2) {
// -----------------------
// double[] p1 = { 2, 3, 4 };
// double[] p2 = { 2, 3 };
int maxlentharr;
int minlentharr;
if (p1.length > p2.length) {
maxlentharr = p1.length;
minlentharr = p2.length;
} else {
maxlentharr = p2.length;
minlentharr = p1.length;
}
double[] Array = new double[maxlentharr];
for (int i = 0; i < Array.length; i++) {
Array[i] = 0;
}
for (int i = 0; i < maxlentharr; i++) {
Array[i] += p1[i];
}
for (int i = 0; i < minlentharr; i++) {
Array[i] += p2[i];
}
return Array;
}
public static double[] mul(double[] p1, double[] p2) {
int maxlentharr, minlentharr, fin = 0, lin;
if (p1.length > p2.length) {
maxlentharr = p1.length;
minlentharr = p2.length;
} else {
maxlentharr = p2.length;
minlentharr = p1.length;
}
lin = maxlentharr;
double[] Array = new double[maxlentharr + minlentharr];
for (int i = 0; i < Array.length; i++) {
Array[i] = 0;
}
for (int k = 0; k < p1.length; k++) {
for (int j = 0; j < p2.length; j++) {
Array[j + k] += (p1[k] * p2[j]);
}
// System.out.println(Arrays.toString(Array));
}
for (int i = 0; Array[i] == 0; i++) {
// if (Array[i] != 0) {
fin = i;
// break;
// }
}
for (int i = (Array.length) - 1; Array[i] == 0; i--) {
// if (Array[i] != 0) {
lin = i;
// // break;
// }
}
double[] copy = Arrays.copyOfRange(Array, fin, lin);
return copy;
// //-----------------------
}
public static double[] derivative(double[] po) {
double D[] = new double[po.length - 1];
for (int i = 1; i < po.length; i++) {
D[i - 1] = po[i] * i;
}
return D;
}
public static void main(String[] args) {
}
}