CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2018
    Posts
    40

    IMPORTANT-call the output of a matrix

    ee
    Last edited by Kmilano; April 26th, 2018 at 03:15 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IMPORTANT-call the output of a matrix

    1D vector(matrix) with one row and 7 column and in line: result += (matrix * decode[b])
    Would be nice, but no. If say matrix is a vector<int> and decode[b] is an int, then what do you expect the result of matrix * decode[b] to be? Do you want each element of matrix to be multiplied by decode[b] or ...

    Mathematically, what you mean by matrix * decode[b] ?

    Do you mean that Matrix refers to the name of the routine in the first code? If so, then you need to call Matrix() as a function in Decode() eg

    Code:
    void Ham::Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
        std::cout << "Decode" << std::endl;
        int decode[7];
    
        std::vector<std::vector<int>> matres (nbits);
        Matrix(matres, nbits);  
        ....
    Then you have the similar mathematical question. What do you mean mathematically by matres * decode[b]? In this case matres is a vector of a vector.

    However, a better mathematical question is given

    vector<vector<int>> matres and int decode[7]

    What is meant mathematically by matres * decode?

    Once that is known, then it can be coded.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IMPORTANT-call the output of a matrix

    Do you mean

    Code:
    void Ham::Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
    	std::cout << "Decode" << std::endl;
    	int decode[7];
    	
    	for (int bits = 0; bits < 7; bits++) {
    		if (codedVector[bits] < (voltage / 4)) { decode[bits] = 0; }
    		else { decode[bits] = 1; }
    	}
    
            std::vector<std::vector<int>> matres(nbits);
            Matrix(matres, nbits);
    	char synd[3];
    	
    	for (int a = 0; a < 3; a++) {
    		char result = 0;
    		for (int b = 0; b < 7; b++) {
    			result += (matres[a] * decode[b]);
    		}
    		synd[a] = result % 2;
    	}
    }
    In this case, the mathematical question is still what is meant by a vector<int> * int? Is each element of the vector multiplied by the it value?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IMPORTANT-call the output of a matrix

    Do you mean this??? (Neither tried nor tested!)

    Code:
    #include <vector>
    #include <limits>
    #include <algorithm>
    #include <iostream>
    #include <numeric>
    
    constexpr size_t nbits = 3 % std::numeric_limits<unsigned long long>::digits;
    
    std::vector<int>& operator*(std::vector<int>& matres, int val)
    {
    	for (auto& m : matres)
    		m *= val;
    
    	return matres;
    }
    
    int operator+=(int val, std::vector<int>& matres)
    {
    	return std::accumulate(matres.begin(), matres.end(), val);
    }
    
    std::ostream& operator<<(std::ostream& os, std::vector<std::vector<int>> matres)
    {
    	for (const auto& row : matres)
    	{
    		for (const auto& v : row)
    			os << v << ' ';
    
    		os << '\n';
    	}
    
    	return os;
    }
    
    void Matrix(std::vector< std::vector<int> > &result, size_t nbits)
    {
    	result.resize(nbits);
    
    	for (unsigned long long number = 1, n = number, ub = 2 << (nbits - 1); number < ub; n = ++number) {
    		for (auto& vec : result)
    		{
    			vec.push_back(n % 2), n >>= 1;
    		}
    	}
    
    	std::reverse(std::begin(result), std::end(result));
    
    	std::cout << "matrix is: " << std::endl << result << std::endl;
    }
    
    int voltage = 7;
    
    void Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
    	std::cout << "Decode" << std::endl;
    	int decode[7];
    
    	for (int bits = 0; bits < 7; bits++) {
    		if (codedVector[bits] < (voltage / 4)) { decode[bits] = 0; } else { decode[bits] = 1; }
    	}
    	char synd[3];
    
    	std::vector<std::vector<int>> matres(nbits);
    	Matrix(matres, nbits);
    
    	for (int a = 0; a < 3; a++) {
    		char result = 0;
    		for (int b = 0; b < 7; b++) {
    			result += (matres[a] * decode[b]);
    		}
    		synd[a] = result % 2;
    	}
    }
    Last edited by 2kaud; April 23rd, 2018 at 11:32 AM. Reason: Added operator<<
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Mar 2018
    Posts
    40

    Re: IMPORTANT-call the output of a matrix

    for a multiplication of 2 matrices if the bits of the first matrix(1D matrix with one row and 7 column) and the bits of each row of second matrix(which is the matrix correspond to the first code(with 3 row and 7 column) multiply together and then append to next , I will reach my proper solution,
    in the following, I attached an image contain an example that I wrote by hand
    Attached Images Attached Images  

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IMPORTANT-call the output of a matrix

    Ok. I understand. I'll knock some code up for this tomorrow (GMT)).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IMPORTANT-call the output of a matrix

    I think this is what you are after. I've switched to array from vector as the sizes are known at compile time.

    Code:
    #include <limits>
    #include <iostream>
    #include <array>
    
    using ub_t = unsigned long long int;
    
    constexpr size_t nbits = 3 % std::numeric_limits<ub_t>::digits;
    constexpr ub_t ub = (2 << (nbits - 1)) - 1;
    
    template<size_t N>
    std::ostream& operator<<(std::ostream& os, std::array<int, N> mat)
    {
    	for (const auto& m : mat)
    		os << m << " ";
    
    	return os;
    }
    
    std::ostream& operator<<(std::ostream& os, std::array<std::array<int, ub>, nbits> mat)
    {
    	for (const auto& row : mat)
    		os << row << std::endl;
    
    	return os;
    }
    
    void generate(std::array<std::array<int, ub>, nbits>& matrix)
    {
    	for (ub_t number = 1, n = number; number <= ub; n = ++number)
    		for (size_t r = 0; r < nbits; ++r)
    			matrix[nbits - r - 1][(size_t)(number - 1)] = n % 2, n >>= 1;
    }
    
    
    const int voltage = 8;
    
    void Decode(std::array<double, ub> &codedVector, std::array<int, ub> &decodedVector) {
    	const double v4 = voltage / 4.0;
    	std::array<int, ub> decode;
    
    
    	for (int bits = 0; bits < ub; ++bits)
    		decode[bits] = codedVector[bits] >= v4;
    
    	std::cout << "decode" << std::endl << decode << std::endl;
    
    	std::array<std::array<int, ub>, nbits> matrix;
    
    	generate(matrix);
    
    	std::cout << "matrix" << std::endl << matrix;
    
    	std::array<int, nbits> synd;
    
    	for (int a = 0; a < nbits; ++a) {
    		int result = 0;
    
    		for (int b = 0; b < ub; ++b)
    			result += matrix[a][b] * decode[b];
    
    		synd[a] = result % 2;
    	}
    
    	std::cout << "synd" << std::endl << synd << std::endl;
    }
    
    int main()
    {
    	std::array<double, ub> cod {5, 1, 6, 7, 1, 1.5, .5};	// Test values. Below 2 is 0.
    	std::array<int, ub> dec;
    
    	Decode(cod, dec);
    }
    giving a test output of

    Code:
    decode
    1 0 1 1 0 0 0
    matrix
    0 0 0 1 1 1 1
    0 1 1 0 0 1 1
    1 0 1 0 1 0 1
    synd
    1 1 0
    as required.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Mar 2018
    Posts
    40

    Re: IMPORTANT-call the output of a matrix

    Hello
    Dear @2kuad, your code works, but unfortunately I cannot use it in my code, because the decode vector generate randomly in another part of my code.

    the structure of the code must follow the code which I attached in #1 (second code).

    you posted a code in #3 like the following:

    Code:
    void Ham::Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
    	std::cout << "Decode" << std::endl;
    	int decode[7];
    	
    	for (int bits = 0; bits < 7; bits++) {
    		if (codedVector[bits] < (voltage / 4)) { decode[bits] = 0; }
    		else { decode[bits] = 1; }
    	}
    
            std::vector<std::vector<int>> matres(nbits);
            Matrix(matres, nbits);
    	char synd[3];
    	
    	for (int a = 0; a < 3; a++) {
    		char result = 0;
    		for (int b = 0; b < 7; b++) {
    			result += (matres[a] * decode[b]);
    		}
    		synd[a] = result % 2;
    	}
    }
    In this code, if instead of [[[result += (matres[a] * decode[b]);]]] I replace [[[result += (matres[a] [b] * decode[b]);]]] then every thing is okay, but could you please explain me the lines which you added to this piece of code(the following lines are added by you):
    I want to be sure about the logic of this lines:

    std::vector<std::vector<int>> matres(nbits);
    Matrix(matres, nbits);

    thank you

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IMPORTANT-call the output of a matrix

    Code:
    std::vector<std::vector<int>> matres(nbits);
    defines matres as a vector of vector of int with an initial number of elements of nbits for the outer vector.

    Code:
    Matrix(matres, nbits);
    Call the function Matrix to generate the contents of the variable matres. nbits is also passed as a parameter. However, if matres is defined as above then this parameter is not needed as matres is sized before the function call as opposed to within the function call. How matres is resized depends somewhat on requirements.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Mar 2018
    Posts
    40

    Re: IMPORTANT-call the output of a matrix

    so I think the content of the first matrix is not used in the second code, in the following code just a 2D vector called matres is defined
    Code:
    void Ham::Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
    	std::cout << "Decode" << std::endl;
    	int decode[7];
    	
    	for (int bits = 0; bits < 7; bits++) {
    		if (codedVector[bits] < (voltage / 4)) { decode[bits] = 0; }
    		else { decode[bits] = 1; }
    	}
    
            std::vector<std::vector<int>> matres(nbits);
            Matrix(matres, nbits);
    	char synd[3];
    	
    	for (int a = 0; a < 3; a++) {
    		char result = 0;
    		for (int b = 0; b < 7; b++) {
    			result += (matres[a] * decode[b]);
    		}
    		synd[a] = result % 2;
    	}
    }

  11. #11
    Join Date
    Mar 2018
    Posts
    40

    Re: IMPORTANT-call the output of a matrix

    @2kuad

    could you please explain which element or variable must be called from the first matrix in first code for multiplying with the 1D matrix in second code??

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IMPORTANT-call the output of a matrix

    From the code in post #10

    Code:
            std::vector<std::vector<int>> matres(nbits);
            Matrix(matres, nbits);
    After this, matres is the 2d matrix.
    decode is the 1d matrix.
    synd is the required resultant matrix.


    From second code in post #1 consider (not tried)

    Code:
            void Ham::Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
            	std::cout << "Decode" << std::endl;
            	int decode[7];
            	
            	for (int bits = 0; bits < 7; bits++) {
            		if (codedVector[bits] < (voltage / 4)) { decode[bits] = 0; }
            		else { decode[bits] = 1; }
            	}
    
            std::vector<std::vector<int>> matres(nbits);
            Matrix(matres, nbits);    //Call Matrix in first code
    
            	char synd[3];
            	
            	for (int a = 0; a < 3; a++) {
            		char result = 0;
            		for (int b = 0; b < 7; b++) {
            			result += (matres[a][b] * decode[b]);
            		}
            		synd[a] = result % 2;
            	}
    
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured