static public void multiply(ref dense_matrix result, dense_matrix left, sparse_matrix right) { int left_height = left.rows, left_width = left.columns; if (left_width != right._height) { throw new ArgumentException("Operand size mismatch"); } Dictionary <int, double> right_row_ref; double[] left_row_ref, result_row_ref; double cur_element; result.clear(left_height, right._width); Dictionary <int, double>[] right_contents = right._contents; for (int cur_row = 0; cur_row < left_height; ++cur_row) { left_row_ref = left[cur_row]; result_row_ref = result[cur_row]; for (int sum_index = 0; sum_index < left_width; ++sum_index) { cur_element = left_row_ref[sum_index]; right_row_ref = right_contents[sum_index]; foreach (KeyValuePair <int, double> cur_horizontal in right_row_ref) { result_row_ref[cur_horizontal.Key] += cur_element * cur_horizontal.Value; } } } }
static public void multiply(ref dense_matrix result, sparse_matrix left, dense_matrix right) { int right_height = right.rows, right_width = right.columns; if (left._width != right_height) { throw new ArgumentException("Operand size mismatch"); } Dictionary <int, double> left_row_ref; double[] result_row_ref, right_row_ref; double cur_element; result.clear(left._height, right_width); int left_height = left._height; Dictionary <int, double>[] left_contents = left._contents; for (int cur_row = 0; cur_row < left_height; ++cur_row) { left_row_ref = left_contents[cur_row]; result_row_ref = result[cur_row]; foreach (KeyValuePair <int, double> cur_diagonal in left_row_ref) { cur_element = cur_diagonal.Value; right_row_ref = right[cur_diagonal.Key]; for (int cur_column = 0; cur_column < right_width; ++cur_column) { result_row_ref[cur_column] += cur_element * right_row_ref[cur_column]; } } } }
/* * public void set_to_identity(int size = 0) * { * clear(size); * for (int cur_element = 0; cur_element < _width; ++cur_element) * _contents[cur_element][cur_element] = 1.0; * } * * public void copy_from(dense_matrix b) * { * if (_width != b._width || _height != b._height) * clear(b._height, b._width); * * double[] cur_row_ref, b_row_ref; * for (int cur_row = 0; cur_row < _height; ++cur_row) * { * cur_row_ref = _contents[cur_row]; * b_row_ref = b._contents[cur_row]; * for (int cur_column = 0; cur_column < _width; ++cur_column) * cur_row_ref[cur_column] = b_row_ref[cur_column]; * } * } */ public void column_vector_from(dense_matrix b, int column) { if (_width != 1 || _height != b._height) { clear(b._height, 1); } int height = _height; double[][] contents = _contents, source = b._contents; for (int cur_row = 0; cur_row < height; ++cur_row) { contents[cur_row][0] = source[cur_row][column]; } }
public static void subtract(dense_matrix minuend, sparse_matrix subtrahend) { if (minuend.columns != subtrahend._width || minuend.rows != subtrahend._height) { throw new ArgumentException("Attempt to subtract matrix of different size"); } double[] minuend_row_ref; Dictionary <int, double> subtrahend_row_ref; int height = subtrahend._height; Dictionary <int, double>[] subtrahend_contents = subtrahend._contents; for (int cur_row = 0; cur_row < height; ++cur_row) { minuend_row_ref = minuend[cur_row]; subtrahend_row_ref = subtrahend_contents[cur_row]; foreach (KeyValuePair <int, double> cur_element in subtrahend_row_ref) { minuend_row_ref[cur_element.Key] -= cur_element.Value; } } }