Загрузка...

Need urgent help. Subject : Weight Matrix

Thread in C/C++ created by ImaMe5s Jan 13, 2023. 198 views

  1. ImaMe5s
    ImaMe5s Topic starter Jan 13, 2023 2 Nov 18, 2022
    #include <iostream>
    #include <iomanip> // setw

    using namespace std;

    bool is_weight_matrix(float const matrix[], size_t rows, size_t cols, float &weight) {
    if(rows != cols) return false;

    weight = 0;
    for (int i = 0; i < rows; i++) {
    for(size_t j = 0; j < cols; j++) {
    weight += matrix[i * cols + j] * matrix[i * cols + j];
    }
    }
    for(size_t i = 0; i < rows; i++) {
    for(size_t j = 0; j < cols; j++) {
    if(i == j) {
    if(matrix[i * cols + j] != weight) return false;
    } else {
    // check for A*At == (cij)
    float cij = 0;
    for(size_t k = 0; k < cols; k++) {
    cij += weight;
    }
    if(cij != 0) {
    return false;
    }
    }
    }
    }
    return true;
    }
    // Helper function for displaying errors
    int test_is_weight_matrix(bool answer, float const matrix[], size_t rows, size_t cols, float expectedWeight)
    {
    float weight;
    if (answer != is_weight_matrix(matrix, rows, cols, weight))
    {
    if (answer == true && weight == expectedWeight) {
    clog << "Weight calculated: "<< weight << "Expected weight: " << expectedWeight << " Passed" << endl;
    return 1;
    } else {
    clog << "Weight calculated: "<< weight << "Expected weight: " << expectedWeight << " Failed" << endl;
    return 1;
    }
    }
    return 0;
    }

    // The test function
    int test_is_weight_matrix()
    {
    float matrix[4][4] {
    {1, 0, 0, 1},
    {0, 1, -1, 0},
    {0, 1, 1, 0},
    {-1, 0, 0, 1}
    };

    if(4 != 4) {
    clog << "Matrix is not square, failed" << endl;
    return 1;
    }
    float weight = 1;
    for(int i = 0; i < 4; i++) {
    weight *= matrix;
    }
    int e = test_is_weight_matrix(true, matrix[0], 4, 4, weight);

    // Other test cases ...

    return e;
    }
    int main()
    {
    int errors = test_is_weight_matrix();
    if (errors == 0)
    cout << "All tests passed\n";
    else
    cout << "Errors: " << errors << endl;
    return 0;
    }

    Вывод:
    Weight calculated: 8Expected weight: 1 Failed
    Errors: 1

    Неправильно считает предполагаемый вес матрицы, может кто пофиксить программу?
     
  2. ImaMe5s
    ImaMe5s Topic starter Jan 13, 2023 2 Nov 18, 2022
    Определить, является ли матрица весовой. Весовой называется квадратная матрица, если все её элементы принадлежат множеству {-1, 0, 1} и произведение этой матрицы на себя транспонированную есть единичная матрица, умноженная на некоторое число w (называемое «вес» матрицы).
    Это условие задания если что.
    The post was merged to previous Jan 13, 2023
    Вес матрицы необязательно должен ровняться единице
    The post was merged to previous Jan 13, 2023
    [IMG]
     
  3. NightAngel
    NightAngel Jan 21, 2023 7 Dec 13, 2021
    если актуально, то за определенную плату могу выполнить, отпиши в лс.
     
Top
Loading...