Загрузка...

Нужна срочно помощь. Тема : Весовая матрица

Тема в разделе C/C++ создана пользователем ImaMe5s 13 янв 2023. 199 просмотров

  1. ImaMe5s
    ImaMe5s Автор темы 13 янв 2023 2 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 Автор темы 13 янв 2023 2 18 ноя 2022
    Определить, является ли матрица весовой. Весовой называется квадратная матрица, если все её элементы принадлежат множеству {-1, 0, 1} и произведение этой матрицы на себя транспонированную есть единичная матрица, умноженная на некоторое число w (называемое «вес» матрицы).
    Это условие задания если что.
    --- Сообщение объединено с предыдущим 13 янв 2023
    Вес матрицы необязательно должен ровняться единице
    --- Сообщение объединено с предыдущим 13 янв 2023
    [IMG]
     
  3. NightAngel
    NightAngel 21 янв 2023 7 13 дек 2021
    если актуально, то за определенную плату могу выполнить, отпиши в лс.
     
Top
Загрузка...