public class Main {
Polynomial<Reality> a = new Polynomial<Reality>(2, new Reality(5.0));
Polynomial<Reality> b = new Polynomial<Reality>(4, new Reality(7.0));
//Polynomial<NaturalN> a = new Polynomial<NaturalN>(2, new NaturalN(5, 10));
//Polynomial<NaturalN> b = new Polynomial<NaturalN>(4, new NaturalN(7, 10));
Polynomial c = a.add(b);
Polynomial d = a.sub(b);
Polynomial e = c.sub(d);
Polynomial f = a.mul(b);
Polynomial g = d.mulScalar(new Reality(2.0));
Polynomial h = g.derivative();
System.
out.
println(h.
grade());
}
}
import static java.
lang.
Math.
abs;
public class NaturalN implements Scalar<NaturalN> {
this.mod=mod;
this.n=n;
}
@Override
public NaturalN sub(NaturalN a) {
if (aa<0) aa+=mod;
return new NaturalN(aa, mod);
}
@Override
public NaturalN mul(NaturalN a) {
return new NaturalN((n*a.n)%mod, mod);
}
@Override
public NaturalN add(NaturalN a) {
return new NaturalN((n+a.n)%mod, mod);
}
@Override
public NaturalN mulInt
(Integer a
) {
return new NaturalN((n*a)%mod, mod);
}
@Override
public boolean isOne() {
return n == 1;
}
@Override
public boolean isZero() {
return n == 0;
}
@Override
public NaturalN getZero() {
return new NaturalN(0, mod);
}
@Override
public NaturalN getOne() {
return new NaturalN(1, mod);
}
@Override
return "+"+n.toString();
}
}
import java.lang.reflect.Array;
import java.util.*;
public class Polynomial <E extends Scalar<E>> {
public Map
<Integer, E
> m
= new TreeMap
<>();
public E[] elem;
private void alokuj (int rozmiar, E... zbędne) {
Class<?> c = zbędne.getClass().getComponentType();
elem
= (E
[]) Array.
newInstance(c, rozmiar
);
}
Polynomial () {
}
addValue(a, x);
}
private static void check
(Polynomial a, Polynomial b
) throws Exception{
if (a.getValue(0).getClass() != b.getValue(0).getClass())
throw e;
}
this.
m = new TreeMap
<Integer,E
>(m
);
}
if (m.containsKey(a))
return m.get(a);
return elem[0].getZero();
}
public void addValue
(Integer a, E x
) {
if (m.containsKey(a)) {
m.put(a, m.get(a).add(x));
} else m.put(a, x);
//m.put(a, x);
}
public Polynomial add
(Polynomial a
) throws Exception{
Polynomial x = new Polynomial(m);
//if (elem.getClass() != a.elem[0].getClass()) throw e;
for (Map.
Entry<Integer, E
> entry
: copy.
entrySet()) {
/*if (x.m.containsKey(entry.getKey())) {
x.m.put(entry.getKey(), entry.getValue().add((E) x.m.get(entry.getKey())));
} else x.m.put(entry.getKey(), entry.getValue());*/
x.addValue(entry.getKey(), entry.getValue());
}
return x;
}
public Polynomial addScalar
(E a
) throws Exception{
Polynomial x = new Polynomial(m);
x.addValue(0, a);
return x;
}
public Polynomial sub
(Polynomial a
) throws Exception {
Polynomial x = new Polynomial();
//if (elem.getClass() != a.elem.getClass()) throw e;
for (Map.
Entry<Integer, E
> entry
: copy.
entrySet()) {
//entry.getValue().setS(Boolean.logicalXor(true, entry.getValue().isS()));
//x.m.put(entry.getKey(), entry.getValue());
x.m.put(entry.getKey(), entry.getValue().getZero().sub(entry.getValue()));
}
return add(x);
}
public Polynomial subScalar
(E a
) throws Exception {
a = a.getZero().sub(a);
return addScalar(a);
}
public Polynomial mul
(Polynomial a
) throws Exception {
Polynomial x = new Polynomial();
for (Map.
Entry<Integer, E
> obj2
: copy.
entrySet()) {
x.addValue(obj1.getKey()+obj2.getKey(), obj1.getValue().mul(obj2.getValue()));
}
}
return x;
}
public Polynomial mulScalar
(E a
) throws Exception {
Polynomial x = new Polynomial();
x.addValue(obj.getKey(), obj.getValue().mul(a));
}
return x;
}
public Polynomial derivative
() throws Exception {
Polynomial x = new Polynomial();
if (obj.getKey()>0)
x.addValue(obj.getKey()-1, obj.getValue().mulInt(obj.getKey()));
}
return x;
}
x = max (x, obj.getKey());
return x;
}
public Polynomial clone () {
return new Polynomial(m);
}
@Override
StringBuilder s = new StringBuilder();
for (Map.
Entry<Integer, E
> entry
: m.
entrySet()) {
s.append(entry.getValue()+"*x^"+entry.getKey());
}
return s.toString();
}
}
import static java.
lang.
Math.
abs;
public class Reality implements Scalar<Reality> {
public boolean S = true;
Reality () {
this.n=0.0;
}
this.n=n;
}
@Override
public Reality sub(Reality a) {
return new Reality(n-a.n);
}
@Override
public Reality mul(Reality a) {
return new Reality(n*a.n);
}
@Override
public Reality add(Reality a) {
System.
out.
println("dodaje: "+n
+ " " + a
);
return new Reality(n+a.n);
}
@Override
return new Reality(n*a);
}
@Override
public boolean isOne() {
return n == 1.0;
}
@Override
public boolean isZero() {
return n == 0.0;
}
@Override
public Reality getZero() {
return new Reality(0.0);
}
@Override
public Reality getOne() {
return new Reality(1.0);
}
@Override
if (n>=0.0)
return "+"+n.toString();
return "-"+abs(n);
}
}
public interface Scalar <T> {
T sub (T a);
T mul (T a);
T add (T a);
boolean isOne();
boolean isZero();
T getZero ();
T getOne ();
}