Facebook
From das, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 250
  1. public class Main {
  2.     public static void main (String[] argv) throws Exception {
  3.         Polynomial<Reality> a = new Polynomial<Reality>(2, new Reality(5.0));
  4.         Polynomial<Reality> b = new Polynomial<Reality>(4, new Reality(7.0));
  5.  
  6.         //Polynomial<NaturalN> a = new Polynomial<NaturalN>(2, new NaturalN(5, 10));
  7.         //Polynomial<NaturalN> b = new Polynomial<NaturalN>(4, new NaturalN(7, 10));
  8.  
  9.  
  10.         System.out.println(a);
  11.         System.out.println(b);
  12.  
  13.         Polynomial c = a.add(b);
  14.  
  15.         System.out.println(c);
  16.  
  17.         Polynomial d = a.sub(b);
  18.  
  19.         System.out.println(d);
  20.  
  21.         Polynomial e = c.sub(d);
  22.  
  23.         System.out.println(e);
  24.  
  25.         Polynomial f = a.mul(b);
  26.  
  27.         System.out.println(f);
  28.  
  29.         Polynomial g = d.mulScalar(new Reality(2.0));
  30.  
  31.         System.out.println(g);
  32.  
  33.         Polynomial h = g.derivative();
  34.  
  35.         System.out.println(h);
  36.         System.out.println(h.grade());
  37.     }
  38. }
  39.  
  40.  
  41. import static java.lang.Math.abs;
  42.  
  43. public class NaturalN implements Scalar<NaturalN> {
  44.     public final Integer n;
  45.     public final Integer mod;
  46.  
  47.     NaturalN (Integer n, Integer mod) {
  48.         this.mod=mod;
  49.         this.n=n;
  50.     }
  51.  
  52.     @Override
  53.     public NaturalN sub(NaturalN a) {
  54.         Integer aa = (n-a.n)%mod;
  55.         if (aa<0) aa+=mod;
  56.         return new NaturalN(aa, mod);
  57.     }
  58.  
  59.     @Override
  60.     public NaturalN mul(NaturalN a) {
  61.         return new NaturalN((n*a.n)%mod, mod);
  62.     }
  63.  
  64.     @Override
  65.     public NaturalN add(NaturalN a) {
  66.         return new NaturalN((n+a.n)%mod, mod);
  67.     }
  68.  
  69.     @Override
  70.     public NaturalN mulInt(Integer a) {
  71.         return new NaturalN((n*a)%mod, mod);
  72.     }
  73.  
  74.     @Override
  75.     public boolean isOne() {
  76.         return n == 1;
  77.     }
  78.  
  79.     @Override
  80.     public boolean isZero() {
  81.         return n == 0;
  82.     }
  83.  
  84.     @Override
  85.     public NaturalN getZero() {
  86.         return new NaturalN(0, mod);
  87.     }
  88.  
  89.     @Override
  90.     public NaturalN getOne() {
  91.         return new NaturalN(1, mod);
  92.     }
  93.  
  94.     @Override
  95.     public String toString() {
  96.         return "+"+n.toString();
  97.     }
  98. }
  99.  
  100.  
  101. import java.lang.reflect.Array;
  102. import java.util.*;
  103.  
  104. import static java.lang.StrictMath.max;
  105.  
  106. public class Polynomial <E extends Scalar<E>> {
  107.     public Map<Integer, E> m = new TreeMap<>();
  108.     public E[] elem;
  109.     private void alokuj (int rozmiar, E... zbędne) {
  110.         Class<?> c = zbędne.getClass().getComponentType();
  111.         elem = (E[]) Array.newInstance(c, rozmiar);
  112.     }
  113.  
  114.     static Exception e = new Exception("Nie zgodne typy");
  115.  
  116.     Polynomial () {
  117.  
  118.     }
  119.  
  120.     Polynomial (Integer a, E x) {
  121.         addValue(a, x);
  122.     }
  123.  
  124.     private static void  check (Polynomial a, Polynomial b) throws Exception{
  125.         if (a.getValue(0).getClass() != b.getValue(0).getClass())
  126.             throw e;
  127.     }
  128.  
  129.     Polynomial (Map <Integer, E> m) {
  130.         this.m = new TreeMap<Integer,E>(m);
  131.     }
  132.  
  133.     public E getValue (Integer a) {
  134.         if (m.containsKey(a))
  135.             return m.get(a);
  136.         return elem[0].getZero();
  137.     }
  138.  
  139.     public void addValue (Integer a, E x) {
  140.         if (m.containsKey(a)) {
  141.             m.put(a, m.get(a).add(x));
  142.         } else m.put(a, x);
  143.         //m.put(a, x);
  144.     }
  145.  
  146.     public Polynomial add (Polynomial a) throws Exception{
  147.         Polynomial x = new Polynomial(m);
  148.         //if (elem.getClass() != a.elem[0].getClass()) throw e;
  149.         Map <Integer, E> copy = (Map<Integer, E>) a.m;
  150.         for (Map.Entry<Integer, E> entry : copy.entrySet()) {
  151.             /*if (x.m.containsKey(entry.getKey())) {
  152.                 x.m.put(entry.getKey(), entry.getValue().add((E) x.m.get(entry.getKey())));
  153.             } else x.m.put(entry.getKey(), entry.getValue());*/
  154.             x.addValue(entry.getKey(), entry.getValue());
  155.         }
  156.         return x;
  157.     }
  158.  
  159.     public Polynomial addScalar (E a) throws Exception{
  160.         Polynomial x = new Polynomial(m);
  161.         x.addValue(0, a);
  162.         return x;
  163.     }
  164.  
  165.     public Polynomial sub (Polynomial a) throws Exception {
  166.         Polynomial x = new Polynomial();
  167.         //if (elem.getClass() != a.elem.getClass()) throw e;
  168.         Map<Integer, E> copy = (Map<Integer, E>) a.m;
  169.         for (Map.Entry<Integer, E> entry : copy.entrySet()) {
  170.             //entry.getValue().setS(Boolean.logicalXor(true, entry.getValue().isS()));
  171.             //x.m.put(entry.getKey(), entry.getValue());
  172.             x.m.put(entry.getKey(), entry.getValue().getZero().sub(entry.getValue()));
  173.         }
  174.         return add(x);
  175.     }
  176.  
  177.     public Polynomial subScalar (E a) throws Exception {
  178.         a = a.getZero().sub(a);
  179.         return addScalar(a);
  180.     }
  181.  
  182.     public Polynomial mul (Polynomial a) throws Exception {
  183.         Polynomial x = new Polynomial();
  184.         Map<Integer, E> copy = (Map<Integer, E>)a.m;
  185.         for (Map.Entry<Integer, E> obj1 : m.entrySet()) {
  186.             for (Map.Entry<Integer, E> obj2 : copy.entrySet()) {
  187.                 x.addValue(obj1.getKey()+obj2.getKey(), obj1.getValue().mul(obj2.getValue()));
  188.             }
  189.         }
  190.         return x;
  191.     }
  192.  
  193.     public Polynomial mulScalar (E a) throws Exception {
  194.         Polynomial x = new Polynomial();
  195.         for (Map.Entry<Integer, E> obj : m.entrySet()) {
  196.             x.addValue(obj.getKey(), obj.getValue().mul(a));
  197.         }
  198.         return x;
  199.     }
  200.  
  201.     public Polynomial derivative () throws Exception {
  202.         Polynomial x = new Polynomial();
  203.         for (Map.Entry<Integer, E> obj : m.entrySet()) {
  204.             if (obj.getKey()>0)
  205.                 x.addValue(obj.getKey()-1, obj.getValue().mulInt(obj.getKey()));
  206.         }
  207.         return x;
  208.     }
  209.  
  210.     public Integer grade () {
  211.         Integer x=0;
  212.         for (Map.Entry<Integer, E> obj : m.entrySet())
  213.             x = max (x, obj.getKey());
  214.         return x;
  215.     }
  216.  
  217.  
  218.     public Polynomial clone () {
  219.         return new Polynomial(m);
  220.     }
  221.  
  222.     @Override
  223.     public String toString() {
  224.         StringBuilder s = new StringBuilder();
  225.         String plus = "";
  226.         for (Map.Entry<Integer, E> entry : m.entrySet()) {
  227.             s.append(entry.getValue()+"*x^"+entry.getKey());
  228.         }
  229.         return s.toString();
  230.     }
  231. }
  232.  
  233.  
  234.  
  235. import static java.lang.Math.abs;
  236.  
  237. public class Reality implements Scalar<Reality> {
  238.  
  239.     public boolean S = true;
  240.  
  241.     public Double n;
  242.  
  243.     Reality () {
  244.         this.n=0.0;
  245.     }
  246.  
  247.  
  248.     Reality (Double n) {
  249.         this.n=n;
  250.     }
  251.  
  252.  
  253.  
  254.     @Override
  255.     public Reality sub(Reality a) {
  256.         return new Reality(n-a.n);
  257.     }
  258.  
  259.     @Override
  260.     public Reality mul(Reality a) {
  261.         return new Reality(n*a.n);
  262.     }
  263.  
  264.     @Override
  265.     public Reality add(Reality a) {
  266.         System.out.println("dodaje: "+n + " " + a);
  267.         return new Reality(n+a.n);
  268.     }
  269.  
  270.     @Override
  271.     public Reality mulInt(Integer a) {
  272.         return new Reality(n*a);
  273.     }
  274.  
  275.     @Override
  276.     public boolean isOne() {
  277.         return n == 1.0;
  278.     }
  279.  
  280.     @Override
  281.     public boolean isZero() {
  282.         return n == 0.0;
  283.     }
  284.  
  285.     @Override
  286.     public Reality getZero() {
  287.         return new Reality(0.0);
  288.     }
  289.  
  290.     @Override
  291.     public Reality getOne() {
  292.         return new Reality(1.0);
  293.     }
  294.  
  295.     @Override
  296.     public String toString() {
  297.         if (n>=0.0)
  298.             return "+"+n.toString();
  299.         return "-"+abs(n);
  300.     }
  301.  
  302. }
  303.  
  304.  
  305.  
  306. public interface Scalar <T> {
  307.  
  308.     T sub (T a);
  309.     T mul (T a);
  310.     T add (T a);
  311.     T mulInt (Integer a);
  312.  
  313.     boolean isOne();
  314.     boolean isZero();
  315.     T getZero ();
  316.     T getOne ();
  317.  
  318.     String toString();
  319. }
  320.  
  321.