Skip to content

Commit

Permalink
Lecture 9
Browse files Browse the repository at this point in the history
  • Loading branch information
mapio committed Oct 30, 2024
1 parent 456995e commit 1f9543e
Show file tree
Hide file tree
Showing 34 changed files with 934 additions and 0 deletions.
167 changes: 167 additions & 0 deletions src/main/java/it/unimi/di/prog2/e09/Poly.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
Copyright 2024 Massimo Santini
This file is part of "Programmazione 2 @ UniMI" teaching material.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This material is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this file. If not, see <https://www.gnu.org/licenses/>.
*/

package it.unimi.di.prog2.e09;

import it.unimi.di.prog2.h08.impl.NegativeExponentException;

/**
* {@code Poly}s are immutable polynomials with integer coefficients.
*
* <p>A typical {@code Poly} is \( p = c_0 + c_1 x + c_2 x^2 + \cdots + c_n x^n \).
*/
public class Poly { // we don't extend Cloneable, see EJ 3.13

// Fields

/** The array of coefficients, the {@code terms[i]} is the coefficient of \( x^i \). */
private final int[] terms;

/** The degree of the polynomial. */
private final int deg;

// Constructors

/** Initializes this to be the zero polynomial, that is \( p = 0 \). */
public Poly() {
terms = new int[1];
deg = 0;
}

/**
* Initializes this to be the polynomial \(p = cx^n\).
*
* @param c the coefficient.
* @param n the degree.
* @throws NegativeExponentException if {@code n} &lt; 0.
*/
public Poly(int c, int n) throws NegativeExponentException {
if (n < 0)
throw new NegativeExponentException("Can't create a monomial with negative exponent");
if (c == 0) deg = 0;
else deg = n;
terms = new int[deg + 1];
terms[deg] = c;
}

/**
* Initializes a polynomial of given degree (with all coefficients equal to 0).
*
* @param n the degree, must be non negative.
*/
private Poly(int n) {
deg = n;
terms = new int[deg + 1];
}

// Methods

/**
* A factory method returning a monomial. (see EJ 2.1)
*
* @param c the coefficient.
* @param n the degree.
* @throws NegativeExponentException if {@code n} &lt; 0.
* @return the monomial, if {@code n} &gt;= 0.
*/
public static Poly monomialWithCoeffAndDegree(int c, int n) {
return new Poly(c, n);
}

/**
* Returns the degree of this polynomial.
*
* @return the largest exponent with a non-zero coefficient; returns 0 if this is the zero {@code
* Poly}.
*/
public int degree() {
return deg;
}

/**
* Returns the coefficient of the term of given exponent.
*
* @param d the exponent of the term to consider.
* @return the coefficient of the considered term.
*/
public int coeff(int d) {
if (d < 0 || d > deg) return 0;
else return terms[d];
}

/**
* Performs polynomial addition.
*
* <p>If \( p \) is this polynomial, returns \( p + q \).
*
* @param q the polynomial to add to this one.
* @return the sum among this and the given polynomial.
* @throws NullPointerException if {@code q} is {@code null}.
*/
public Poly add(Poly q) throws NullPointerException {
return null; // add missing implementation
}

/**
* Performs polynomial multiplication.
*
* <p>If \( p \) is this polynomial, returns \( p q \).
*
* @param q the polynomial to multiply by this one.
* @return the product among this and the given polynomial.
* @throws NullPointerException if {@code q} is {@code null}.
*/
public Poly mul(Poly q) throws NullPointerException {
if (q == null) throw new NullPointerException();
if ((q.deg == 0 && q.terms[0] == 0) || (deg == 0 && terms[0] == 0)) return new Poly();
Poly r = new Poly(deg + q.deg);
for (int i = 0; i <= deg; i++)
for (int j = 0; j <= q.deg; j++) r.terms[i + j] = r.terms[i + j] + terms[i] * q.terms[j];
return r;
}

/**
* Performs polynomial subtraction.
*
* <p>If \( p \) is this polynomial, returns \( p - q \).
*
* @param q the polynomial to subtract from this one.
* @return the subtraction among this and the given polynomial.
* @throws NullPointerException if {@code q} is {@code null}.
*/
public Poly sub(Poly q) throws NullPointerException {
if (q == null) throw new NullPointerException();
return add(q.minus());
}

/**
* Returns the negate polynomial.
*
* <p>If \( p \) is this polynomial, returns \( -p \).
*
* @return this polynomial multiplied by \( -1 \).
*/
public Poly minus() {
Poly r = new Poly(deg);
for (int i = 0; i <= deg; i++) r.terms[i] = -terms[i];
return r;
}
}
56 changes: 56 additions & 0 deletions src/main/java/it/unimi/di/prog2/e09/PolyClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2024 Massimo Santini
This file is part of "Programmazione 2 @ UniMI" teaching material.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This material is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this file. If not, see <https://www.gnu.org/licenses/>.
*/

package it.unimi.di.prog2.e09;

/** A class to test some methods of {@link Poly}. */
public class PolyClient {

/** . */
private PolyClient() {}

/**
* Tests some methods of {@link Poly}.
*
* <p>Starting from term \( t_0 = x + 1 \) reads a list of \( t_i \) of terms from the standard
* input, given as a (coefficient, degree) pairs, and computes the polynomial \( t_0 \cdot t_1
* \cdot t_2 \cdots \), emitting in the standard output the pairs "coefficient degree" for every
* term in the result (in increasing order of degree).
*
* @param args not used.
*/

/*- Uncomment the main method once you have implemented the add method in Poly class
public static void main(String[] args) {
Poly result = null;
Poly xp1 = new Poly(1, 1).add(new Poly(-1, 0));
try (Scanner s = new Scanner(System.in)) {
while (s.hasNextInt()) {
Poly term = new Poly(s.nextInt(), s.nextInt());
if (result == null) result = term;
else result = result.mul(xp1).add(term);
}
for (int d = 0; d <= result.degree(); d++) System.out.println(result.coeff(d) + " " + d);
}
}
*/
}
141 changes: 141 additions & 0 deletions src/main/java/it/unimi/di/prog2/e09/SparsePoly.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
Copyright 2024 Massimo Santini
This file is part of "Programmazione 2 @ UniMI" teaching material.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This material is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this file. If not, see <https://www.gnu.org/licenses/>.
*/

package it.unimi.di.prog2.e09;

import it.unimi.di.prog2.h08.impl.NegativeExponentException;
import java.util.List;

/**
* {@code SparsePoly}s are immutable polynomials with integer coefficients such that the number of
* nonzero coefficient is small with respect to the degree.
*
* <p>A typical {@code Poly} is \( p = c_0 + c_1 x + c_2 x^2 + \cdots + c_n x^n \).
*/
public class SparsePoly {

/**
* A record holding a term of the polynomial.
*
* @param coeff the coefficient.
* @param degree the degree.
*/
public record Term(int coeff, int degree) {
/**
* Builds a term.
*
* @throws NegativeExponentException if if {@code degree} &lt; 0.
*/
public Term { // using the compact constructor
if (degree < 0)
throw new NegativeExponentException("A term cannot have a negative exponent.");
}
}

/** The array of terms (in increasing non-zero degree). */
private final List<Term> terms;

/** Initializes this to be the zero polynomial, that is \( p = 0 \). */
public SparsePoly() {
terms = null; // replace this with the actual implementation
}

/**
* Initializes this to be the polynomial \(p = cx^n\).
*
* @param c the coefficient.
* @param n the degree.
* @throws NegativeExponentException if {@code n} &lt; 0.
*/
public SparsePoly(int c, int n) throws NegativeExponentException {
terms = null; // replace this with the actual implementation
}

/**
* Returns the coefficient of the term of given exponent.
*
* @param d the exponent of the term to consider.
* @return the coefficient of the considered term.
*/
public int coeff(int d) {
return 0; // replace this with the actual implementation
}

/**
* Returns the degree of this polynomial.
*
* @return the largest exponent with a non-zero coefficient; returns 0 if this is the zero {@code
* Poly}.
*/
public int degree() {
return 0; // replace this with the actual implementation
}

/**
* Performs polynomial addition.
*
* <p>If \( p \) is this polynomial, returns \( p + q \).
*
* @param q the polynomial to add to this one.
* @return the sum among this and the given polynomial.
* @throws NullPointerException if {@code q} is {@code null}.
*/
public SparsePoly add(SparsePoly q) throws NullPointerException {
return null; // replace this with the actual implementation
}

/**
* Performs polynomial multiplication.
*
* <p>If \( p \) is this polynomial, returns \( p q \).
*
* @param q the polynomial to multiply by this one.
* @return the product among this and the given polynomial.
* @throws NullPointerException if {@code q} is {@code null}.
*/
public SparsePoly mul(SparsePoly q) throws NullPointerException {
return null; // replace this with the actual implementation
}

/**
* Performs polynomial subtraction.
*
* <p>If \( p \) is this polynomial, returns \( p - q \).
*
* @param q the polynomial to subtract from this one.
* @return the subtraction among this and the given polynomial.
* @throws NullPointerException if {@code q} is {@code null}.
*/
public SparsePoly sub(SparsePoly q) throws NullPointerException {
return null; // replace this with the actual implementation
}

/**
* Returns the negate polynomial.
*
* <p>If \( p \) is this polynomial, returns \( -p \).
*
* @return this polynomial multiplied by \( -1 \).
*/
public SparsePoly minus() {
return null; // replace this with the actual implementation
}
}
Loading

0 comments on commit 1f9543e

Please sign in to comment.