[C/C++] 행렬의 연산 : 상등

2024. 5. 2. 23:21·Computer Science/선형대수 with C++

상등

: 두 행렬의 크기가 같고 각각 대응하는 성분이 모두 같은 두 행렬

아래 두 행렬은 상등이다.


C++로 구현하기

메소드 오버라이딩을 통해 행렬의 상등 연산을 구현해보자.

#include <iostream>

#include "matrix.h"

using namespace std;

int main() {
    Matrix m1(3, 4);
    Matrix m2(3, 4);
    Matrix m3(3, 3);

    if (m1 == m2) printf("m1과 m2는 같은 행렬\n");
    else printf("m1과 m2는 다른 행렬\n");

    if (m2 == m3) printf("m2과 m3는 같은 행렬\n");
    else printf("m2과 m3는 다른 행렬\n");

    return 0;
}

...
class Matrix {
    int **mat;
    int row;
    int col;

public:
    ...
    int operator==(Matrix &m) {
        if (row != m.getRow() || col != m.getCol()) return 0;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (mat[i][j] != m.getElement(i, j))
                    return 0;
            }
        }

        return 1;
    }
	...
};
...

깃허브 이미지 링크

저작자표시 (새창열림)

'Computer Science > 선형대수 with C++' 카테고리의 다른 글

[C/C++] 전치 행렬  (0) 2024.05.02
[C/C++] 단위행렬, 영행렬  (0) 2024.05.02
[C/C++] 행렬  (0) 2024.05.02
'Computer Science/선형대수 with C++' 카테고리의 다른 글
  • [C/C++] 전치 행렬
  • [C/C++] 단위행렬, 영행렬
  • [C/C++] 행렬
Study with Me!
Study with Me!
Study with Me!
  • Study with Me!
    Seongmo
    Study with Me!
  • 전체
    오늘
    어제
    • Computer (147)
      • Computer Science (61)
        • Data Structure (51)
        • Algorithm (6)
        • 선형대수 with C++ (4)
      • Backend (11)
        • 백엔드 취업을 위해.. (1)
        • Spring (10)
        • Database (0)
        • Testing (0)
        • Infra & DevOps (0)
      • Arm Architecture (1)
        • Register (0)
        • Assembly Instruction (1)
      • Linux (32)
        • Linux Kernel (4)
        • 라이브러리 함수 구현하기 (0)
        • 쉘, 쉘 명령어 구현하기 (15)
        • Ubuntu (13)
      • Cloud Infrastructure (8)
        • Kubernetes (7)
        • OpenStack Magnum (1)
      • AWS (3)
      • Baekjoon (18)
      • Tools (6)
        • Git & Github (5)
        • Vim (1)
      • 개발 환경 (7)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    STL
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Study with Me!
[C/C++] 행렬의 연산 : 상등
상단으로

티스토리툴바