Commit 79f2bc7c authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Upload New File

parent 8decbd0a
Loading
Loading
Loading
Loading

Trinion.h

0 → 100644
+112 −0
Original line number Diff line number Diff line
//
// Created by Ari on 12/15/24.
//

#ifndef FINALEXAMPROJECTTWO_TRINION_H
#define FINALEXAMPROJECTTWO_TRINION_H
#include <ostream>
using namespace std;

/**
 * Represents a Trinion number, which is a number that can be expressed
 * as a+bi+cj, where i^2=j^2=-1 and ij=ji=1+i+j.
 * @tparam T The base type of coefficients a,b, and c for the Trinian.
 */
template <typename T>
class Trinion {
 public:
  // CONSTRUCTORS
  Trinion<T>():scalarCoeff(0), iCoeff(0), jCoeff(0) {}

  Trinion<T>(
      const T thePlainCoeff,
      const T theICoeff,
      const T theJCoeff
  ) : scalarCoeff(thePlainCoeff), iCoeff(theICoeff), jCoeff(theJCoeff) {}

  Trinion<T>(const Trinion<T> &second) :
      scalarCoeff(second.scalarCoeff),
      iCoeff(second.iCoeff),
      jCoeff(second.jCoeff) {}

  // OPERATORS
  /**
 * @param second The addend.
 * @return The sum of this Trinion with [second]
 */
  Trinion<T>& operator+(const Trinion<T> &second) const {
    auto *result =
        new Trinion<T>(
            scalarCoeff + second.scalarCoeff,
            iCoeff + second.iCoeff,
            jCoeff + second.jCoeff
        );
    return *result;
  }

  /**
 * @param second The multiplier
 * @return The product of this Trinion with [second]
 */
  Trinion<T> &operator*(const Trinion<T> &second) const {
    T newPlain =
        scalarCoeff * second.scalarCoeff
            - (iCoeff - jCoeff) * (second.iCoeff - second.jCoeff);
    T newI =
        second.iCoeff * (scalarCoeff + jCoeff)
            + iCoeff * (second.scalarCoeff + second.jCoeff);
    T newJ =
        second.jCoeff * (scalarCoeff + iCoeff)
            + jCoeff * (second.scalarCoeff + second.iCoeff);
    Trinion<T> *result = new Trinion<T>(newPlain, newI, newJ);
    return *result;
  }

  /**
 * Sets this to [second].
 * @param second The Trinion to copy into this object.
 * @return *this, after it has been set to [second].
 */
  Trinion<T> &operator=(const Trinion<T> &second) {
    scalarCoeff = second.scalarCoeff;
    iCoeff = second.iCoeff;
    jCoeff = second.jCoeff;
    return *this;
  }

  /**
   * @param second The Trinion to compare.
   * @return true iff this object has the same coefficients as [second]
   */
  bool operator==(const Trinion<T> &second) const {
    return (scalarCoeff == second.scalarCoeff && iCoeff == second.iCoeff && jCoeff == second.jCoeff);
  }

  /**
   * @param exp An exponent.
   * @return This Trinion raised to the exp-th power
   * @example (1+i)^0 == 1
   * @example 2 ^ 9 == 512
   * @example (2i) ^ 3 == -8i
   * @example (1 + i +1.5j) ^ 2 == 0.75 + 5i + 6j
   */
  Trinion<T> &operator^(unsigned int exp);

  // FRIENDS
  /**
   * @return A stream to which ths coefficients of [tri] has been added.
   */
  friend ostream &operator<<(ostream &os, const Trinion<T> &tri) {
    os << tri.scalarCoeff << " + "
    << tri.iCoeff << "i" << " + "
    << tri.jCoeff << "j";
    return os;
  }

  // FIELDS
  T scalarCoeff;
  T iCoeff;
  T jCoeff;
};

#endif //FINALEXAMPROJECTTWO_TRINION_H
 No newline at end of file