library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub rajyan/library

:heavy_check_mark: src/Matrix2D.hpp

Depends on

Verified with

Code

#pragma once

#include "Point2D.hpp"

template<class T>
struct Matrix2D {
    Point2D<T> r0{}, r1{};

    constexpr Matrix2D() = default;
    constexpr Matrix2D(const Point2D<T> &r0_, const Point2D<T> &r1_) noexcept: r0(r0_), r1(r1_) {};
    constexpr explicit Matrix2D(const T &diag) noexcept: r0{diag, 0}, r1{0, diag} {}

    constexpr bool operator==(const Matrix2D &rhs) const noexcept { return r0 == rhs.r0 && r1 == rhs.r1; }
    constexpr bool operator!=(const Matrix2D &rhs) const noexcept { return !(*this == rhs); }

    constexpr Matrix2D operator+(const Matrix2D &rhs) const noexcept { return {r0 + rhs.r0, r1 + rhs.r1}; }
    constexpr Matrix2D operator-(const Matrix2D &rhs) const noexcept { return {r0 - rhs.r0, r1 - rhs.r1}; }
    constexpr Matrix2D operator*(const Matrix2D &rhs) const noexcept {
        const Matrix2D rhs_T = rhs.trans();
        return {{r0 * rhs_T.r0, r0 * rhs_T.r1},
                {r1 * rhs_T.r0, r1 * rhs_T.r1}};
    }
    constexpr Matrix2D &operator+=(const Matrix2D &rhs) noexcept { return *this = *this + rhs; }
    constexpr Matrix2D &operator-=(const Matrix2D &rhs) noexcept { return *this = *this - rhs; }
    constexpr Matrix2D &operator*=(const Matrix2D &rhs) noexcept { return *this = *this * rhs; }
    constexpr Matrix2D operator-() const noexcept { return {-r0, -r1}; }

    constexpr Point2D<T> operator*(const Point2D<T> &b_T) const noexcept { return {r0 * b_T, r1 * b_T}; }

    [[nodiscard]] constexpr Matrix2D trans() const noexcept {
        return {{r0.x, r1.x},
                {r0.y, r1.y}};
    }
    [[nodiscard]] constexpr Matrix2D identity() const noexcept { return Matrix2D{T{1}}; }
    [[nodiscard]] constexpr Matrix2D pow(lint n) const noexcept {
        Matrix2D res{Matrix2D{}.identity()}, tmp{*this};
        while (n > 0) {
            if (n & 1) res *= tmp;
            tmp *= tmp;
            n >>= 1;
        }
        return res;
    }
    [[nodiscard]] constexpr T det() const noexcept { return r0 * r1.nor(); }
};

using mat = Matrix2D<lint>;
#line 2 "src/Matrix2D.hpp"

#line 2 "src/Point2D.hpp"

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;
using lint = long long;

template<class T>
struct Point2D {
    T x{}, y{};

    constexpr Point2D() = default;
    constexpr Point2D(const T &x, const T &y) noexcept: x(x), y(y) {};
    constexpr explicit Point2D(const pair<T, T> &p) noexcept: x(p.first), y(p.second) {};

    constexpr bool operator==(const Point2D &rhs) const noexcept { return x == rhs.x && y == rhs.y; }
    constexpr bool operator!=(const Point2D &rhs) const noexcept { return !(*this == rhs); }
    constexpr bool operator<(const Point2D &rhs) const noexcept { return x < rhs.x || (x == rhs.x && y < rhs.y); }
    constexpr bool operator>(const Point2D &rhs) const noexcept { return rhs < *this; }
    constexpr bool operator<=(const Point2D &rhs) const noexcept { return !(*this > rhs); }
    constexpr bool operator>=(const Point2D &rhs) const noexcept { return !(*this < rhs); }

    constexpr Point2D operator+(const Point2D &rhs) const noexcept { return {x + rhs.x, y + rhs.y}; }
    constexpr Point2D operator-(const Point2D &rhs) const noexcept { return {x - rhs.x, y - rhs.y}; }
    constexpr Point2D operator*(const T &k) const noexcept { return {k * x, k * y}; }
    constexpr Point2D operator/(const T &k) const noexcept { return {x / k, y / k}; }
    constexpr Point2D &operator+=(const Point2D &rhs) noexcept { return *this = *this + rhs; }
    constexpr Point2D &operator-=(const Point2D &rhs) noexcept { return *this = *this - rhs; }
    constexpr Point2D &operator*=(const T &k) noexcept { return *this = *this * k; }
    constexpr Point2D &operator/=(const T &k) noexcept { return *this = *this / k; }
    constexpr Point2D &operator--() noexcept { return *this -= Point2D(1, 1); };
    constexpr Point2D &operator++() noexcept { return *this += Point2D(1, 1); };
    constexpr Point2D operator--(int) noexcept { Point2D res = *this; --*this; return res; };
    constexpr Point2D operator++(int) noexcept { Point2D res = *this; ++*this; return res; };
    constexpr Point2D operator-() const noexcept { return {-x, -y}; }

    constexpr T operator*(const Point2D &rhs) const noexcept { return x * rhs.x + y * rhs.y; }

    [[nodiscard]] constexpr Point2D nor() const noexcept { return {y, -x}; }
    [[nodiscard]] constexpr long double hypot() const noexcept { return ::hypotl(x, y); }
    [[nodiscard]] constexpr bool inGrid(const T &H, const T &W) const noexcept { return 0 <= x && x < H && 0 <= y && y < W; }
    template<class U>
    [[nodiscard]] constexpr U &operator[](vector<vector<U>> &v) const noexcept { return v[x][y]; }

    constexpr friend istream &operator>>(istream &is, Point2D &p) { return is >> p.x >> p.y; }
    constexpr friend ostream &operator<<(ostream &os, const Point2D &p) { return os << p.x << ' ' << p.y; }
};

using pnt = Point2D<lint>;
#line 4 "src/Matrix2D.hpp"

template<class T>
struct Matrix2D {
    Point2D<T> r0{}, r1{};

    constexpr Matrix2D() = default;
    constexpr Matrix2D(const Point2D<T> &r0_, const Point2D<T> &r1_) noexcept: r0(r0_), r1(r1_) {};
    constexpr explicit Matrix2D(const T &diag) noexcept: r0{diag, 0}, r1{0, diag} {}

    constexpr bool operator==(const Matrix2D &rhs) const noexcept { return r0 == rhs.r0 && r1 == rhs.r1; }
    constexpr bool operator!=(const Matrix2D &rhs) const noexcept { return !(*this == rhs); }

    constexpr Matrix2D operator+(const Matrix2D &rhs) const noexcept { return {r0 + rhs.r0, r1 + rhs.r1}; }
    constexpr Matrix2D operator-(const Matrix2D &rhs) const noexcept { return {r0 - rhs.r0, r1 - rhs.r1}; }
    constexpr Matrix2D operator*(const Matrix2D &rhs) const noexcept {
        const Matrix2D rhs_T = rhs.trans();
        return {{r0 * rhs_T.r0, r0 * rhs_T.r1},
                {r1 * rhs_T.r0, r1 * rhs_T.r1}};
    }
    constexpr Matrix2D &operator+=(const Matrix2D &rhs) noexcept { return *this = *this + rhs; }
    constexpr Matrix2D &operator-=(const Matrix2D &rhs) noexcept { return *this = *this - rhs; }
    constexpr Matrix2D &operator*=(const Matrix2D &rhs) noexcept { return *this = *this * rhs; }
    constexpr Matrix2D operator-() const noexcept { return {-r0, -r1}; }

    constexpr Point2D<T> operator*(const Point2D<T> &b_T) const noexcept { return {r0 * b_T, r1 * b_T}; }

    [[nodiscard]] constexpr Matrix2D trans() const noexcept {
        return {{r0.x, r1.x},
                {r0.y, r1.y}};
    }
    [[nodiscard]] constexpr Matrix2D identity() const noexcept { return Matrix2D{T{1}}; }
    [[nodiscard]] constexpr Matrix2D pow(lint n) const noexcept {
        Matrix2D res{Matrix2D{}.identity()}, tmp{*this};
        while (n > 0) {
            if (n & 1) res *= tmp;
            tmp *= tmp;
            n >>= 1;
        }
        return res;
    }
    [[nodiscard]] constexpr T det() const noexcept { return r0 * r1.nor(); }
};

using mat = Matrix2D<lint>;
Back to top page