public class Main { public static void main (String[] argv) throws Exception { Polynomial a = new Polynomial(2, new Reality(5.0)); Polynomial b = new Polynomial(4, new Reality(7.0)); //Polynomial a = new Polynomial(2, new NaturalN(5, 10)); //Polynomial b = new Polynomial(4, new NaturalN(7, 10)); System.out.println(a); System.out.println(b); Polynomial c = a.add(b); System.out.println(c); Polynomial d = a.sub(b); System.out.println(d); Polynomial e = c.sub(d); System.out.println(e); Polynomial f = a.mul(b); System.out.println(f); Polynomial g = d.mulScalar(new Reality(2.0)); System.out.println(g); Polynomial h = g.derivative(); System.out.println(h); System.out.println(h.grade()); } } import static java.lang.Math.abs; public class NaturalN implements Scalar { public final Integer n; public final Integer mod; NaturalN (Integer n, Integer mod) { this.mod=mod; this.n=n; } @Override public NaturalN sub(NaturalN a) { Integer aa = (n-a.n)%mod; 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 public String toString() { return "+"+n.toString(); } } import java.lang.reflect.Array; import java.util.*; import static java.lang.StrictMath.max; public class Polynomial > { public Map 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); } static Exception e = new Exception("Nie zgodne typy"); Polynomial () { } Polynomial (Integer a, E x) { addValue(a, x); } private static void check (Polynomial a, Polynomial b) throws Exception{ if (a.getValue(0).getClass() != b.getValue(0).getClass()) throw e; } Polynomial (Map m) { this.m = new TreeMap(m); } public E getValue (Integer a) { 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; Map copy = (Map) a.m; for (Map.Entry 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; Map copy = (Map) a.m; for (Map.Entry 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(); Map copy = (Map)a.m; for (Map.Entry obj1 : m.entrySet()) { for (Map.Entry 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(); for (Map.Entry obj : m.entrySet()) { x.addValue(obj.getKey(), obj.getValue().mul(a)); } return x; } public Polynomial derivative () throws Exception { Polynomial x = new Polynomial(); for (Map.Entry obj : m.entrySet()) { if (obj.getKey()>0) x.addValue(obj.getKey()-1, obj.getValue().mulInt(obj.getKey())); } return x; } public Integer grade () { Integer x=0; for (Map.Entry obj : m.entrySet()) x = max (x, obj.getKey()); return x; } public Polynomial clone () { return new Polynomial(m); } @Override public String toString() { StringBuilder s = new StringBuilder(); String plus = ""; for (Map.Entry entry : m.entrySet()) { s.append(entry.getValue()+"*x^"+entry.getKey()); } return s.toString(); } } import static java.lang.Math.abs; public class Reality implements Scalar { public boolean S = true; public Double n; Reality () { this.n=0.0; } Reality (Double n) { 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 public Reality mulInt(Integer a) { 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 public String toString() { if (n>=0.0) return "+"+n.toString(); return "-"+abs(n); } } public interface Scalar { T sub (T a); T mul (T a); T add (T a); T mulInt (Integer a); boolean isOne(); boolean isZero(); T getZero (); T getOne (); String toString(); }