2 차원 행렬을 구축하기 위해 vector
을 사용하는 Matrix class
을 작성하려고했습니다. 내 주요 목표는이 클래스를 사용하여 행렬을 행렬에 추가 할 수 있고, 또한 정상적인 행렬 연산을 수행 할 수 있다는 것입니다. 내 현재 문제는 어떻게 추가/뺄셈/두 행렬의 요소로 요소를 곱하면됩니까? this answer 아이디어를 적용하여이 문제를 해결하기 위해 벡터 속성을 사용하고 싶습니다. 그러나 지금까지 나는 성공하지 못했습니다. 어떻게 요소 와이즈 두 벡터 작업에 transform과 plus/minus를 사용할 수 있습니까 ??"transform"및 구조체를 cython에서 벡터에 대한 연산을 구현하는 데 사용
from libcpp.vector cimport vector
import cython
import numpy as np
import ctypes
cimport numpy as np
cimport cython
cdef extern from "<algorithm>" namespace "std" nogil:
OutputIter copy[InputIter,OutputIter](InputIter,InputIter,OutputIter)
#http://en.cppreference.com/w/cpp/algorithm/transform
OutputIter transform[InputIter, OutputIter, UnaryOperation](InputIter,InputIter,OutputIter, UnaryOperation)
cdef struct plus:
T operator[] (T& x, T& y):
return x+y
cdef class Matrix:
cdef vector[double] matrix
cdef int _rows
cdef int _columns
def __cinit__(self, int rows=0, int columns=0, bint Identity=False):
self._rows=rows
self._columns=columns
self.matrix.resize(rows*columns)
if Identity:
self._IdentityMatrix()
property rows:
def __get__(self):
return self._rows
def __set__(self, int x):
self._rows = x
property columns:
def __get__(self):
return self._columns
def __set__(self, int y):
self._columns = y
cpdef double getVal(self, int r, int c):
return self.matrix[r*self._columns+c]
cpdef void setVal(self, int r, int c, double v):
self.matrix[r*self._columns+c] = v
cpdef void AddRow(self, vector[double] row):
cdef int i
if (row.size()!=self._columns):
raise Exception('The size of appended row is wrong!')
else:
self._rows+=1
for i from 0 <= i <self._columns:
self.matrix.push_back(row[i])
cpdef void AddColumn(self, vector[double] col):
cdef int i
if (col.size()!=self._rows):
raise Exception('The size of appended column is wrong!')
else:
for i from 0 <= i <self._rows:
self.matrix.insert(self.matrix.begin()+i+(i+1)*self._columns, col[i])
self._columns+=1
cpdef Matrix transpose(self):
cdef int i,j
cdef Matrix m = Matrix(self._columns,self._rows)
for i from 0 <= i <self._columns:
for j from 0 <= j <self._rows:
m.setVal(i,j,self.getVal(j,i))
return m
cdef void _IdentityMatrix(self):
cdef int i
if (self._rows!=self._columns):
raise Exception('In order to generate identity matrix, the number of rows and columns must be equal')
else:
for i from 0 <= i <self._columns:
self.setVal(i,i,1.0)
cpdef void add(self, Matrix m):
if ((self._rows!=m._rows) or (self._columns!=m._columns)):
raise Exception('In order to add two matrices, they must have equal number of rows and columns')
else:
transform(self.begin(), m.begin(), self.begin(),plus())
이 문제를 해결하기위한 답을 얻게되면 감사하겠습니다.
빠른 포인터 :'plus'는'operator []'보다는'operator()'를 가져야한다고 생각합니다. 템플릿 함수를 만들기보다는'double'을 정의하는 것이 더 쉽습니다. – DavidW
@DaivdW 어떻게해야합니까? double 연산자() (double x, double y) : 더블 연산자 (double x, double y) : return x + y'이 오류가 발생했습니다 : double 연산자 (double x, double y) : ^ - -------------------------------------------------- --------- matrix.py : 16 : 43 : C 함수 정의가 허용되지 않습니다. ' – Dalek
@DavidW 글쎄, [여기]에서'plus '를 사용하는 것은 어떨까요? (http : //en.cppreference. co.kr/w/cpp/utility/functional/plus)? C++ 라이브러리에서 헤더를 가져 오는 방법은 무엇입니까? 다시 한번 감사드립니다 :) – Dalek