Skip to content

Commit

Permalink
feat(math): add operator<< to Group and Field to enable printin…
Browse files Browse the repository at this point in the history
…g `Matrix`
  • Loading branch information
chokobole committed May 8, 2024
1 parent 36b475b commit 8fcca55
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions tachyon/math/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ tachyon_cc_unittest(
":sign",
"//tachyon/base/buffer:vector_buffer",
"//tachyon/base/containers:container_util",
"//tachyon/base/strings:string_number_conversions",
"//tachyon/math/elliptic_curves/msm/test:variable_base_msm_test_set",
"//tachyon/math/elliptic_curves/short_weierstrass/test:sw_curve_config",
"//tachyon/math/finite_fields/test:finite_field_test",
Expand Down
7 changes: 7 additions & 0 deletions tachyon/math/base/field.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef TACHYON_MATH_BASE_FIELD_H_
#define TACHYON_MATH_BASE_FIELD_H_

#include <ostream>
#include <utility>

#include "tachyon/math/base/ring.h"
Expand Down Expand Up @@ -32,6 +33,12 @@ class Field : public AdditiveGroup<F>, public MultiplicativeGroup<F> {
}
};

template <typename F>
std::ostream& operator<<(std::ostream& os, const Field<F>& f) {
const F& derived = static_cast<const F&>(f);
return os << derived.ToString();
}

} // namespace tachyon::math

#endif // TACHYON_MATH_BASE_FIELD_H_
13 changes: 13 additions & 0 deletions tachyon/math/base/groups.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define TACHYON_MATH_BASE_GROUPS_H_

#include <limits>
#include <ostream>
#include <tuple>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -158,6 +159,12 @@ class MultiplicativeGroup : public MultiplicativeSemigroup<G> {
}
};

template <typename G>
std::ostream& operator<<(std::ostream& os, const MultiplicativeGroup<G>& g) {
const G& derived = static_cast<const G&>(g);
return os << derived.ToString();
}

// AdditiveGroup is a group with the group operation '+'.
// AdditiveGroup supports subtraction and negation, inheriting the
// properties of AdditiveSemigroup.
Expand Down Expand Up @@ -203,6 +210,12 @@ class AdditiveGroup : public AdditiveSemigroup<G> {
}
};

template <typename G>
std::ostream& operator<<(std::ostream& os, const AdditiveGroup<G>& g) {
const G& derived = static_cast<const G&>(g);
return os << derived.ToString();
}

} // namespace tachyon::math

#endif // TACHYON_MATH_BASE_GROUPS_H_
11 changes: 11 additions & 0 deletions tachyon/math/base/groups_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "tachyon/math/base/groups.h"

#include <string>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "tachyon/base/containers/container_util.h"
#include "tachyon/base/strings/string_number_conversions.h"
#include "tachyon/math/finite_fields/test/gf7.h"

namespace tachyon::math {
Expand All @@ -20,6 +23,8 @@ TEST(GroupsTest, Div) {

bool operator==(const Int& other) const { return value_ == other.value_; }

std::string ToString() const { return base::NumberToString(value_); }

private:
int value_ = 0;
};
Expand All @@ -44,6 +49,8 @@ TEST(GroupsTest, InverseOverride) {
return denominator_ == other.denominator_;
}

std::string ToString() const { return base::NumberToString(denominator_); }

private:
int denominator_ = 0;
};
Expand Down Expand Up @@ -104,6 +111,8 @@ TEST(GroupsTest, Sub) {

bool operator==(const Int& other) const { return value_ == other.value_; }

std::string ToString() const { return base::NumberToString(value_); }

private:
int value_ = 0;
};
Expand All @@ -130,6 +139,8 @@ TEST(GroupsTest, SubOverAdd) {

bool operator==(const Int& other) const { return value_ == other.value_; }

std::string ToString() const { return base::NumberToString(value_); }

private:
int value_ = 0;
};
Expand Down

0 comments on commit 8fcca55

Please sign in to comment.