Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NTL/
.DS_Store
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CXX = clang++
# Added Homebrew include path
CXXFLAGS = -O3 -std=c++17 -I./NTL/include -I/opt/homebrew/include
# Added Homebrew library path
LDFLAGS = -L./NTL/src -L/opt/homebrew/lib -lntl -lgmp

Comment on lines +2 to +6
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Makefile references hardcoded Homebrew paths (/opt/homebrew/include and /opt/homebrew/lib) which are specific to macOS ARM architecture. This will break on Linux and Intel Mac systems. Consider using pkg-config or detecting the system to make the build more portable.

Suggested change
# Added Homebrew include path
CXXFLAGS = -O3 -std=c++17 -I./NTL/include -I/opt/homebrew/include
# Added Homebrew library path
LDFLAGS = -L./NTL/src -L/opt/homebrew/lib -lntl -lgmp
PKG_CONFIG ?= pkg-config
PKG_CFLAGS_NTL := $(shell $(PKG_CONFIG) --cflags ntl 2>/dev/null)
PKG_LIBS_NTL := $(shell $(PKG_CONFIG) --libs ntl 2>/dev/null)
PKG_CFLAGS_GMP := $(shell $(PKG_CONFIG) --cflags gmp 2>/dev/null)
PKG_LIBS_GMP := $(shell $(PKG_CONFIG) --libs gmp 2>/dev/null)
CXXFLAGS = -O3 -std=c++17 -I./NTL/include $(PKG_CFLAGS_NTL) $(PKG_CFLAGS_GMP)
LDFLAGS = -L./NTL/src $(PKG_LIBS_NTL) $(PKG_LIBS_GMP)

Copilot uses AI. Check for mistakes.
TARGET = phase1
SRC = phase1.cpp

all: $(TARGET)

$(TARGET): $(SRC)
$(CXX) $(CXXFLAGS) $(SRC) $(LDFLAGS) -o $(TARGET)

clean:
rm -f $(TARGET)
Binary file added conv_impl.cpp
Binary file not shown.
Binary file added phase1
Binary file not shown.
147 changes: 147 additions & 0 deletions phase1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include<NTL/ZZ_p.h>
#include<NTL/tools.h>
#include<NTL/vec_ZZ.h>
#include<NTL/vec_ZZ_p.h>
#include<NTL/vector.h>
#include<iostream>
#include<sstream>
#include<iomanip>
#include<string>

constexpr const long PRIME_BIT = 60;
constexpr const long ERR_BOUND = 80;
double total_time{};
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The total_time variable should not be a global variable with default initialization. It would be better to declare it locally in main() and initialize it explicitly to 0.0 for clarity.

Copilot uses AI. Check for mistakes.

using namespace std;
using namespace NTL;

std::vector<std::pair<long,long double>>global;
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global variable should follow better naming conventions. Consider using a more descriptive name like "benchmark_results" or "timing_results" instead of just "global".

Copilot uses AI. Check for mistakes.
/* O(n^2) compute for toeplitz matrix with input vector Benchmarking against Cooley method */
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'toeplitz' to 'Toeplitz'.

Suggested change
/* O(n^2) compute for toeplitz matrix with input vector Benchmarking against Cooley method */
/* O(n^2) compute for Toeplitz matrix with input vector Benchmarking against Cooley method */

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment: "toeplitz" is misspelled as "toeplitz". The correct spelling is "Toeplitz" (capitalized as it's a proper name - Otto Toeplitz).

Suggested change
/* O(n^2) compute for toeplitz matrix with input vector Benchmarking against Cooley method */
/* O(n^2) compute for Toeplitz matrix with input vector Benchmarking against Cooley method */

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment states this is "O(n^2) compute" but the actual complexity is O(n²) which is correctly implemented. However, "compute" is grammatically incomplete - it should be "computation" or "algorithm".

Suggested change
/* O(n^2) compute for toeplitz matrix with input vector Benchmarking against Cooley method */
/* O(n^2) computation for toeplitz matrix with input vector Benchmarking against Cooley method */

Copilot uses AI. Check for mistakes.
/* from 1d -> 2d we use V[(n-1) + ( j- i) * xj)] j: 0 -> n */
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formula explanation is unclear. The formula shows (n-1) + (j-i) * xj but the actual code uses (n-1) + (j-i) without the * xj term. The documentation should accurately reflect the implementation.

Suggested change
/* from 1d -> 2d we use V[(n-1) + ( j- i) * xj)] j: 0 -> n */
/* from 1d -> 2d we use V[(n - 1) + (j - i)] for j: 0 -> n-1 */

Copilot uses AI. Check for mistakes.

Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing documentation: The mul_norm function lacks a proper documentation comment explaining what it does, its parameters, return value, and the algorithm used. This is especially important for academic/research code.

Suggested change
/**
* Perform dense Toeplitz matrix–vector multiplication in O(n^2) time.
*
* The Toeplitz matrix T is encoded as a one-dimensional vector `tope_mat`
* of length 2*n - 1 over ZZ_p, containing all distinct diagonals:
* - `tope_mat[n - 1]` holds the main diagonal entry T[0,0],
* - `tope_mat[n - 1 + (j - i)]` holds the entry T[i,j] for any 0 <= i,j < n.
*
* For each output index i, the function computes
* res[i] = sum_{j=0}^{n-1} T[i,j] * input[j]
* using the Toeplitz indexing formula
* v = (n - 1) + (j - i),
* so that T[i,j] = tope_mat[v].
*
* @param input length-n vector over ZZ_p to be multiplied.
* @param tope_mat length-(2*n - 1) vector encoding the Toeplitz matrix T.
* @param n dimension of the (implicit) Toeplitz matrix and input/output vectors.
* @return length-n vector `res` = T * input over ZZ_p.
*/

Copilot uses AI. Check for mistakes.
vec_ZZ_p mul_norm(const vec_ZZ_p &input, const vec_ZZ_p &tope_mat, long n){
vec_ZZ_p res;
res.SetLength(n);

for(long i{};i<n;++i){
ZZ_p sum{};
for(long j{};j < n;++j){
long v = (n - 1) + (j - i);
ZZ_p prod;
mul(prod, tope_mat[v], input[j]);
add(sum, sum, prod);
}
res[i] = sum;
}
return res;
}

#if 0
/* optimized mutliplation of tope_mat * input vector. O(n log n). Would be benchmark against mul_norm */
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment: "mutliplation" should be "multiplication".

Suggested change
/* optimized mutliplation of tope_mat * input vector. O(n log n). Would be benchmark against mul_norm */
/* optimized multiplication of tope_mat * input vector. O(n log n). Would be benchmark against mul_norm */

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar error in comment: "Would be benchmark" should be "Will be benchmarked" or "To be benchmarked".

Suggested change
/* optimized mutliplation of tope_mat * input vector. O(n log n). Would be benchmark against mul_norm */
/* optimized mutliplation of tope_mat * input vector. O(n log n). Will be benchmarked against mul_norm */

Copilot uses AI. Check for mistakes.
vec_ZZ_p cooley_mul(const vec_ZZ_p &input, const vec_ZZ_p &tope_mat, long n ){
vec_zz_p res;
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commented out cooley_mul function contains a typo: "vec_zz_p" should be "vec_ZZ_p" (capital Z's) to match the NTL library's naming convention.

Suggested change
vec_zz_p res;
vec_ZZ_p res;

Copilot uses AI. Check for mistakes.

for(long i{};i<n;++i){

}
return res;
}

#endif

/* Function the benchmark the time and the performance */
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar error in comment: "Function the benchmark" should be "Function to benchmark".

Suggested change
/* Function the benchmark the time and the performance */
/* Function to benchmark the time and the performance */

Copilot uses AI. Check for mistakes.
/* generic so we can pass any type of Multiplication Function */

Comment on lines +52 to +54
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing documentation: The routine_check function lacks proper documentation explaining its purpose as a benchmarking function, what the template parameter represents, and what gets stored in the global vector.

Suggested change
/* Function the benchmark the time and the performance */
/* generic so we can pass any type of Multiplication Function */
/**
* Benchmark a Toeplitz matrix–vector multiplication routine.
*
* This function measures the execution time of a multiplication routine
* (provided as a callable) that multiplies the Toeplitz matrix `toep_mat`
* by the input vector `input` of dimension `n`.
*
* @tparam Func A callable type taking `(const vec_ZZ_p&, const vec_ZZ_p&, long)`
* and returning a `vec_ZZ_p`. It is expected to implement a
* Toeplitz matrix–vector multiplication, e.g. `mul_norm`.
* @param toep_mat The Toeplitz matrix encoded as a 1D vector of length `2*n-1`.
* @param input The input vector of length `n` to be multiplied.
* @param n The dimension of the (square) Toeplitz matrix and input vector.
* @param mul_function The multiplication routine to benchmark.
*
* The elapsed time for the call to `mul_function` is recorded in the global
* vector `global` as a pair `(n, elapsed_time)`, where `elapsed_time` is
* measured in seconds using `GetTime()`. A human-readable summary line is
* also printed to `std::cout`.
*/

Copilot uses AI. Check for mistakes.
template<typename Func>
void routine_check(const vec_ZZ_p &toep_mat, const vec_ZZ_p &input, long n, Func mul_function){

long double curr_time = GetTime();
vec_ZZ_p mat = mul_function(input,toep_mat, n);
long double final_time = GetTime() - curr_time;
global.push_back({n,final_time});
std::cout << "[ time: " << final_time << "]" << std::endl;
}


Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing documentation: The print_matrix function lacks documentation explaining its parameters and that it prints a formatted Toeplitz matrix visualization.

Suggested change
/* Prints a formatted visualization of an n x n Toeplitz matrix.
*
* Parameters:
* toep_mat - One-dimensional representation of the Toeplitz matrix,
* expected to have length 2*n - 1 and indexed as
* toep_mat[(n - 1) + (j - i)] for matrix entry (i, j).
* n - Dimension of the square Toeplitz matrix to be displayed.
*
* The matrix is printed as a table with fixed-width columns to stdout
* for verification and debugging purposes.
*/

Copilot uses AI. Check for mistakes.
void print_matrix(const vec_ZZ_p &toep_mat, long n) {
const int width = 30;

auto print_separator = [&]() {
std::cout << "+";
for(long i = 0; i < n; ++i)
std::cout << std::string(width, '-') << "+";
std::cout << "\n";
};

std::cout << "\n--- Toeplitz Matrix Verification (n=" << n << ") ---\n";

print_separator();

for(long i = 0; i < n; ++i) {
std::cout << "|";

for(long j = 0; j < n; ++j) {
long index = (n-1) + (j-i);

std::ostringstream oss;
oss << toep_mat[index];
std::string s = oss.str();

std::cout << std::setw(width) << s << "|";
}

std::cout << "\n";
print_separator();
}
}

int main(){

ZZ p;
GenPrime(p ,PRIME_BIT ,ERR_BOUND);
ZZ_p::init(p);
long t = 8;
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name "t" on line 103 is declared but never used in the code. This should be removed or properly utilized.

Suggested change
long t = 8;

Copilot uses AI. Check for mistakes.
SetSeed(to_ZZ(time(nullptr)));
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The random seed is set after generating a random bound for n on line 17. This means n will always use the same default seed, producing the same value across runs. Move SetSeed to before line 17 to ensure n is also properly randomized.

Copilot uses AI. Check for mistakes.

long start,step,end;

std::cout << "Please Enter the Start value: " << std::endl;
std::cin >> start;
std::cout << "Please Enter the Step value: " << std::endl;
std::cin >> step;
std::cout << "Please Enter the End value: " << std::endl;
std::cin >> end;


while(start != end){

vec_ZZ_p input;
input.SetLength(start);

for(long i{};i< start; ++i){
random(input[i]);
}
vec_ZZ_p toep_mat;

toep_mat.SetLength(2*start-1);

for(long i{};i<2*start-1;++i){
random(toep_mat[i]);
}
print_matrix(toep_mat, start);
routine_check(toep_mat,input, start, mul_norm);
start += step;
}

std::cout << "\n";

std::cout << "FINAL SUMMARY ---------------------------------- " << std::endl;
for(auto&[n,final_time]:global){
total_time += final_time;
std::cout << "For n: " << n << " " << " Time taken: " << final_time << std::endl;
}
std::cout << "\n";
std::cout << "Total time taken: " << " " << total_time << std::endl;

return 0;
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated Toeplitz matrix and input vector are not used after creation - they're discarded when the program exits. If this is intentional for phase 1, consider adding a comment explaining this is a stub implementation. Otherwise, add the intended operations (multiplication, convolution, etc.) or at least output the generated data for verification.

Copilot uses AI. Check for mistakes.
}