algorithm,matrix,fft,polynomials , Polynomial multiplication in M2(R)?

Polynomial multiplication in M2(R)?


Tag: algorithm,matrix,fft,polynomials

I was trying to implement a FFT-based multiplication algorithm in M2(R). Basically an algorithm that gets as an input two polynoms with elements given as matrices, and builds the product polynom. However, even though the algorithm should work, as it looks exactly identical to a version I wrote earlier on regular number, it doesn't. The coefficients are always off by a little.

I have not found any articles on the roots of unity in M2(C), but I have found(on paper) that choosing eps = ((cos(2PI/n) , i sin(2PI/n)) , ( i sin(2PI/n) , cos(2PI/n))), I get a nice cycle.

Is there something wrong in my approach?

Here is the code:

struct FFT {

    PolyC To, Aux[17][2], Res[17][2], AC, BC, ResC, ResD, ArgA, ArgB;

    void fft(PolyC V, var depth, var n, PolyC To, MatC step) {
        if(n == 1) {
            To[0] = V[0];
        } else {

            MatC eps = matCHelper.I2;

            //We "split" the poly in 2
            for(var i=0; i<n; i++)
                Aux[depth+1][i&1][i>>1] = V[i];

            //We recursively apply FFT to the components
            fft(Aux[depth+1][0], depth+1, n/2, Res[depth+1][0], step*step);
            fft(Aux[depth+1][1], depth+1, n/2, Res[depth+1][1], step*step);

            //We compute the result for the n roots
            for(var i=0; i<n/2; i++) {
                To[i] = Res[depth+1][0][i] + eps * Res[depth+1][1][i];
                To[n/2+i] = Res[depth+1][0][i] - eps * Res[depth+1][1][i];
                eps = eps * step;

    void FFTMultiply(Poly Res, Poly A, Poly B, var n1, var n2) {

        var M;
        for(M = 1; M <= 2*n1 || M <= 2*n2; M <<= 1);

        for(var i=0; i<n1; i++) ArgA[i] = A[i];
        for(var i=n1; i<M; i++) ArgA[i] = matCHelper.O2;

        for(var i=0; i<n2; i++) ArgB[i] = B[i];
        for(var i=n2; i<M; i++) ArgB[i] = matCHelper.O2;

        MatC step( Complex(cos(2*PI/M), 0) , Complex(0, sin(2*PI/M)),
                   Complex(0, sin(2*PI/M)) , Complex(cos(2*PI/M), 0) );

        fft(ArgA, 0, M, AC, step);
        fft(ArgB, 0, M, BC, step);

        for(var i=0; i<M; i++) {
            RezC[i] = AC[i] * BC[i];

        step.b = -step.b;
        step.c = -step.c;

        fft(RezC, 0, M, RezD, step);

        for(var i=0; i<M; i++) {
            // Now I divided everything by M and copied every element of ResD to Res modulo some number


You can not expect this method to work if your coefficient matrices do not commute with your matrix step. To get it working correctly, use the diagonal matrix corresponding to multiplication with the scalar exp(i*2*PI/M).


Analytical solution for Linear Regression using Python vs. Julia

Using example from Andrew Ng's class (finding parameters for Linear Regression using normal equation): With Python: X = np.array([[1, 2104, 5, 1, 45], [1, 1416, 3, 2, 40], [1, 1534, 3, 2, 30], [1, 852, 2, 1, 36]]) y = np.array([[460], [232], [315], [178]]) θ = ((np.linalg.inv( print(θ) Result: [[...

find indices x,y of a matrix of specific values in python

I convert a list of integers to a two dimensinal array like this: data = numpy.array( l ) shape = ( 10, 30 ) data = data.reshape( shape ) I try to get the indices x,y of the matrix of the values that are bigger than some threshold and lower...

algorithm to get all combinations of splitting up N items into K bins

Assuming I have a list of elements [1,2,3,4,] and a number of bins (let's assume 2 bins), I want to come up with a list of all combinations of splitting up items 1-4 into the 2 bins. Solution should look something like this [{{1}, {2,3,4}}, {{2}, {1,3,4}}, {{3}, {1,2,4}}, {{4},...

Reverse ^ operator for decryption

I'm trying to reverse the following code in order to provide a function which takes the buffer and decrypts it. void crypt_buffer(unsigned char *buffer, size_t size, char *key) { size_t i; int j; j = 0; for(i = 0; i < size; i++) { if(j >= KEY_SIZE) j = 0;...

Algorithm for [inclusive/exclusive]_scan in parallel proposal N3554

Proposal N3554 (A Parallel Algorithms Library) for C++14, proposes (among other things), what seem to be parallel versions of the current std::partial_sum, e.g.: template< class ExecutionPolicy, class InputIterator, class OutputIterator, class BinaryOperation> OutputIterator inclusive_scan( ExecutionPolicy &&exec, InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); With the explanation Effects: For each...

Recursive algorithm that returns a bool when checking if array[i] == i (must be O(log n))

I'm trying to write a recursive algorithm that returns true if at least one array[i] == i. And false if there is no array[i] = i. Also, the parameters needed are (int * arr, int start, int end). So i'll be traversing the array with a pointer. For example: int...

Matlab - Multiply specific entries by a scalar in multidimensional matrix

I'm having problems multiplying specific values within my multidimensional matrix by a scalar. My matrix has the following dimension: size(comDatabe) = 5 10 3 397 10 The third dimension is an x-y-z coordinate frame. Something went wrong and now my y-axis is defined upside down for one subject (#8 out...

Creating a matrix based on a function in R

I have a symmetric matrix (dimension: 12,000 X 12,000) named A and I want to create another one based on a formula, which depends on the elements position. To explain: I want to create the D matrix (based on the values from A) using the formula: Dij = 1 -...

Ranking with time weighting

I am looking for a basic algorithm that gives more weigh to the recent reviews. So, the output value of the algorithm is mutable. For example, two reviews with exactly the same score, will have a different ranking based on the timestamp of the creation. Review_1 Score 10 creation 10/5/2014...

Inserting One Row Each Time in a Sequence from Matrix into Another Matrix After Every nth Row in Matlab

I have matrix A and matrix B. Matrix A is 100*3. Matrix B is 10*3. I need to insert one row from matrix B each time in a sequence into matrix A after every 10th row. The result would be Matrix A with 110*3. How can I do this in...

Storing columns on disk and reading rows

So I have a C++ code that finds the inverse of a square matrix. The inverse is full and it would be bad to keep it all in memory because I am working with hundreds of thousands of columns. My code generates the columns one by one. After finding the...

strcmp performance in C

int myStrCmp (const char *s1, const char *s2) { const unsigned char *p1 = (const unsigned char *)s1; const unsigned char *p2 = (const unsigned char *)s2; while (*p1 != '\0') { if (*p2 == '\0') return 1; if (*p2 > *p1) return -1; if (*p1 > *p2) return 1;...

Should checking loop conditions be counted towards total number of comparisons?

I have implemented three different sorting algorithms and now I want to confirm that my approach of counting the total number of comparisons is correct. In my mind, the number of comparisons shouldn't be tied to the conditional branches because if the condition isn't met, the comparison was still made...

Numpy and dot products of multiple vector pairs: how can it be done?

I want to get dot product of N vector pairs (a_vec[i, :], b_vec[i, :]). a_vec has shape [N, 3], bvec has the same shape (N 3D vectors). I know that it can be easily done in cycle via function. But cannot it be done somehow simpler and faster?...

How to give mathemarical proof or support my answer through reasoning as a general case?

You are managing a software project that involves building a computer-assisted instrument for medical surgery. The exact placement of the surgical knife is dependent on a number of different parameters, usually at least 25, sometimes more. Your programmer has developed two algorithms for positioning the cutting tool, and is seeking...

How to reduce time to find the n-th place from consecutive digits number for less than 1 second

I'm following the programming test, and there are questions like this From this consecutive digits: 123456789101112131415161718192021.... For example The 10th digit is 1 The 11th digit is 0 and so on What is the 1,000,000th digit? What is the 1,000,000,000th digit? What is the 1,000,000,000,000th digit? Your solution should run...

Modify a matrix in ocaml

I want to write a function that is modify every elements inside a matrix. But I have some problem when compiling. Warning 10: this expression should have type unit. I think because all function in ocaml need to return to a value or unit, so if I implement two tasks...

How to set first column to a constant value of an empty np.zeros numPy matrix?

I'm working on setting some boundary conditions for a water table model, and I am able to set the entire first row to a constant value, but not the entire first column. I am using np.zeros((11,1001)) to make an empty matrix. Does anyone know why I am successful at defining...

Interpolation inside a matrix. Matlab

I have a matrix looks like: 0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 1 0 1 0 0 0 1 0 4 0 0 0 0 0 3 0 0 6 0 0 4...

Best way to extract ODD digits from a binary number

Given a 64 bit number, I need to extract every other bit from it, and convert it into a number: decimal: 357 binary: 0000 0001 0110 0101 odd bits: 0 0 0 1 1 0 1 1 decimal: 27 Any idea of a good algorithmic way to do it? And...

Dynamic programming: how to design algorithm for when there are two factors to consider?

I have the following problem and I only have a slight idea about it: Consider a tape storage problem. Given n files of length l1,...,ln and frequencies with which they are accessed f1,...,fn, where sum of all frequencies is 1 and 0<fi<1. "Optimal" means to minimize the average retrieval time...

Longest Increasing Subsequence code in O(N)?

Someone asked me a question Find the longest alphabetically increasing or equal string composed of those letters. Note that you are allowed to drop unused characters. So ghaaawxyzijbbbklccc returns aaabbbccc. Is an O(n) solution possible? and I implemented it code [in python] s = 'ghaaawxyzijbbbklccc' lst = [[] for i...

How do I apply multiple matrices in spriteBatch.Draw()?

I'm making a game in C# and XNA 4.0. In some levels I need a camera matrix to make the level appear to scroll left/right. Additionally, I also need a scaling matrix to scale the graphics when the settings are used to change the window size. Both of my matrices...

Recursive solution doesn't iterate correctly

I'm working through a toy problem in Ruby: how to produce all possible 10-digit phone numbers where each successive number is adjacent to the last on the keypad. I've represented the adjacent relationships between numbers, and have a recursive function, but my method isn't iterating through the whole solution space....

Unable to convert between 1d and 2d array values

I realize this problem and solutions are all over StackOverflow like here however I'm still unable to make this work. Most of the examples say that I just need to multiply the row by the width and add the column meaning the location (4, 3) in a 4x4 square grid...

Algorithmic big o order of growth code

I'm doing an online course and i'm stuck on this question. I know there are similar questions but they don't help me. What is the order of growth of the worst case running time of the following code fragment as a function of N? int sum = 0; for (int...

Travels using graph

Someone can help me to think in the better way to adapt Dijkstra's Algorithm in these conditions? All I thought didn't was good. Example of input: GP4578 MADRID 01:00 PORTO 02:00 IK6587 PORTO 03:00 VALENCIA 05:00 05:30 TENERIFE 08:00 AB5874 VALENCIA 05:40 BERLIM 10:00 "VALENCIA 05:00 05:30" This is a...

Project Euler # 5; this solution works - but why?

The project Euler problem 5 is stated as : "2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?" Here's...

I can't fix camera to any object in a 2D Matrix

Problem description: I am trying to develop some 2D game with terrain collision in C# using XNA framework. I have already completed simple terrain generating and object, for example car, can move on this terrain. But now, I get a problem with camera fix to that car. I tried to...

Does there exist an algorithm for iterating through all strings that conform to a particular regex?

I'm making a script to try and hack into an account whose login password is at least 8 characters long and includes at least 1 number, 1 special character and 1 capital letter. I will use brute force. Is there a compact, elegant and efficient way to iterate through every...

Find the shortest path sum in a matrix. Is Dijkstra not optimal for this case?

I am trying to solve the following problem from project euler (please take a look at description and the example in the link, but here is the short explanation). in the matrix, find the minimal path sum from the top left to the bottom right, by moving left, right, up,...

Identify that a string could be a datetime object

If I knew the format in which a string represents date-time information, then I can easily use datetime.datetime.strptime(s, fmt). However, without knowing the format of the string beforehand, would it be possible to determine whether a given string contains something that could be parsed as a datetime object with the...

3 X 3 magic square recursively

I'm trying to find all possible solutions to the 3X3 magic square. There should be exactly 8 solutions. My code gets them all but there are a lot of repeats. I'm having a hard time tracking the recursive steps to see why I'm getting all the repeats. // This program...

Add a matrix of 2x2 into a vector in c++

I am trying to fill a vector with a matrix of values in c++. I'm not very self confident with this procedure (I don't know well about pointers and I don't know if I need it here) however I am trying this int auxMat[gray.rows][gray.cols]; vector<int> collectionSum; collectionSum.push_back(auxMat); When I try...

Computing time complexity of the sparse matrix (2)

I have a data set (D) of (nxd) where n=number of rows and d= number of dimensions, I create a similarity matrix (S)(nxn) by comparing each row of the data set (D) and then convert it into a sparse matrix (tx3) where t is the number of non-zero elements of...

Join matrices with same values in different vectors in MATLAB

I am trying to merge two matrices that share same values in multiple columns. The following matrices should exemplify my problem and provide a MWE. However, my data is very long size(500000, 4), therefore I am searching an efficient way to merge them. The data consists of option data where...

Getting Wrong Answer in “Longest non regular parentheses sub-sequence ”codechef june cook off

I attended a programming competition(it has ended now). I don't know why my solution is giving WA, I read the editorial, saw other people solution but unable to find a flaw in my solution. Obviously I am missing somewhere. Please help! Question Link My Solution My approach If the pattern...

Determining if a graph has a cycle without using DFS

I came around one of those questions in my exams: Topologocial sorting using Kahn's Algorithm requires the graph to be DAG (Directed Acyclic Graph). How can we determine if a graph contains no cycles without using DFS/BFS first? I am trying to answer that for too long now and I...

complexity of generating a sparse matrix

I have a symmetric matrix S(n*n) where approximately 70% of the data are 0. Symmetric matrix I want to convert the symmetric matrix into a sparse matrix with t rows. What would be the time complexity of generating a sparse matrix from the original symmetric matrix? Is it O(n^2), because...

What is this algorithm mapping coordinates to numbers called?

I'm writing a program for visualizing crystals. As a part of the program, I have to generate all different basic points in a lattice structure. For those that aren't familiar with crystallography, you can find the most general cases of these structures here: The problem was that I wanted...

Looking for a particular algorithm for numerical integration

Consider the following differential equation f(x) = g'(x) I have a build a code that spits out values of the function f(x) for the variable x, where x goes from 0 to very large. Now, I'm looking for a scheme that will analyse these values of f(x) in order to...

Javascript: Detailed differences between !variable and variable == false?

I accidentally got undefined == true and undefined == false that both of them returns false. But !undefined returns true. And this is the question: What's the algorithmic difference(s) between !someVariable and someVariable == false? If i want to explain it more, type undefined == false ? 't' : 'f'...

Understanding Big-Ω (Big-Omega) notation

I was doing some reading on logarithms and the rate of growth of the running time of algorithms. I have, however, a problem understanding the Big-Ω (Big-Omega) notation. I know that we use it for 'asymptotic lower bounds', and that we can express the idea that an algorithm takes at...

How to solve for matrix in Matlab?

How can I solve , where and and in the least squares sense in matlab? So I'd like to have the minimizing as output....

Knapsack with unbounded items

I am familiar with the 0-1 knapsack problem and when you are given a certain number of copies from each item but I can figure out how to solve it when you are given infinite copies of each item using dynamic programming. I am trying to solve it by hand...

What is an efficient way of multiplying massive polynomials in Java?

I have a function that defines a very long polynomial useful in operations with matrices. The formula multiplies binomials of the form (1+xi*yj) , where i and j are subscripted and correspond to the rows and columns of the matrix in question, respectively. The polynomial that results is the product...

Create a Triangular Matrix from a Vector performing sequential operations

I have been trying to solve the following problem. Suppose I have the following vector: aux1<-c(0,0,0,4,5,0,7,0,0,10,11,12) where the numbers represent the number of the row. I want to calculate the distance between the differents elements of this vector fixing the first component, then the second and so on. If the...

Binary Search - Best and worst case

In an exam I see a question: Which one of the following is true? For a binary search, the best-case occurs when the target item is in the beginning of the search list. For a binary search, the best-case occurs when the target is at the end of the search...

Explain the time complexity of these grouping functions

Here I have 800 derived classes of Base and a list of 8000000 objects of these types, which can be of any order. The goal is to separate the list into the 800 types as efficiently as possible. Here I have written two functions to do that. The first is...

Disconnect all vertices in a graph - Algorithm

I am looking for an algorithm that finds minimal subset of vertices such that by removing this subset (and edges connecting these vertices) from graph all other vertices become unconnected (i.e. the graph won't have any edges). Is there such algorithm? If not: Could you recommend some kind of heuristics...