행렬곱셈을 수행하는 함수를 작성하려고 하는데 타입에 대한 이해가 되지 않습니다.

cjscokdd의 이미지

#define a11 a->p[0]
#define a12 a->p[1]
#define a21 a->p[2]
#define a22 a->p[3]
#define b11 b->p[0]
#define b12 b->p[1]
#define b21 b->p[2]
#define b22 b->p[3]
#define c11 c->p[0]
#define c12 c->p[1]
#define c21 c->p[2]
#define c22 c->p[3]
#define d11 d->p[0]
#define d12 d->p[1]
#define d21 d->p[2]
#define d22 d->p[3]
 
 
typedef union _matrix {
    double **d;
    union _matrix **p;
} *matrix;

Strassen행렬곱을 표현하는 알고리즘을 작성하려고 하는데요, 이미 작성된 다른 소스를 검토하는 중에
각 행렬의 부분행렬 a11,a12, a21, a22등을 a->p[0], a->p[1], a->p[2], a->p[3]와 같이 나타내며,
행렬 타입을 선언하는데 왜 union을 사용하는지, 타입이름이 *matrix와 같이 나타내었는지 이해가 잘 되지 않아 질문드립니다.

klara의 이미지

'왜'냐는 질문에 대답할 수 있는건 만든 사람뿐일 듯합니다.
그걸 구현하는 방법은 하나가 아니니까요.

그리고 여기서 typedef한 타입이름은 *matrix가 아니라 matrix입니다.
union _matrix* 를 matrix로 정의한 것입니다.

raymundo의 이미지

http://math.ewha.ac.kr/~jylee/SciComp/sc-crandall/strassen.h

http://stackoverflow.com/questions/9539082/how-to-use-this-c-code-to-multiply-two-matrices-using-strassens-algorithm

/*
 * A matrix is defined to be a pointer to a ``union _matrix'', which
 * is (if the size is <= BREAK) a matrix of numbers, or else is an
 * array of four submatrices of half size. 
 */

일단,
타입 이름이 *matrix가 아니라,
타입 이름은 matrix 인데, 이 타입의 실제 정의는 union _matrix * 인 거고요.

union _matrix 가 유니온인 이유는 (주석문을 보아하니) 그 안에서 d 필드를 써서 접근할 때는 double 의 2차원 배열(더 정확히는, 동적으로 할당된 1차원 배열들을 가리키는 포인터들의 1차원 배열)로 접근할 수도 있고, p 필드를 써서 접근할 때는 또다른 union _matrix 들의 2차원 배열(이 때도 더 정확히는 동적으로 할당된...)로서 접근할 수도 있게 한 거네요. 어떤 때는 실제 원소의 배열이고 어떤 때는 서브행렬의 배열로 쓸 것인지는 나름의 조건(size와 BREAK의 비교)이 있고.

그러면 위 define의 경우,
a12 라고 쓰면, 실제로는 a 변수(이 변수가 가리키는 대상은 적절히 초기화되어 있을 것이고)에 대해 p[1]을 접근하니까, 결과적으로
a 행렬 안에 있는 네 개의 서브행렬 중 두번째 행렬의 포인터를 얻게 되죠.

알고리즘 자체에 대해서야 질문하신 분이 아실 테니까 이 정도면...

좋은 하루 되세요!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.