How to access to subsequence of a valarray considering it as a 2D matrix in C++
I'm learning C++, so please be patient with me.
I have a std::valarray
in which there are double
elements and I consider it as a 2D matrix.
class Matrix {
valarray<double> elems;
int r, c;
public:
/* type? operator(int r) { return ? } */
//...
}
I want to overload the operator
, so that I can get a row of the matrix, and after that, I want have the m[r][c]
access operator.
Is there any way to get a row, as a sequence of double
using std::slice
in the valarray
, so that if I change a value, it is changed also in the matrix?
I've read this definition in valarray:
std::slice_array<T> operator( std::slice slicearr );
My operator
must have std::slice_array<double>&
as returned type?
Thanks.
c++
add a comment |
I'm learning C++, so please be patient with me.
I have a std::valarray
in which there are double
elements and I consider it as a 2D matrix.
class Matrix {
valarray<double> elems;
int r, c;
public:
/* type? operator(int r) { return ? } */
//...
}
I want to overload the operator
, so that I can get a row of the matrix, and after that, I want have the m[r][c]
access operator.
Is there any way to get a row, as a sequence of double
using std::slice
in the valarray
, so that if I change a value, it is changed also in the matrix?
I've read this definition in valarray:
std::slice_array<T> operator( std::slice slicearr );
My operator
must have std::slice_array<double>&
as returned type?
Thanks.
c++
What is proxy class in C++
– Swordfish
Nov 19 '18 at 15:56
I asked a similar question back in 2016! :D
– Paula_plus_plus
Nov 19 '18 at 16:45
add a comment |
I'm learning C++, so please be patient with me.
I have a std::valarray
in which there are double
elements and I consider it as a 2D matrix.
class Matrix {
valarray<double> elems;
int r, c;
public:
/* type? operator(int r) { return ? } */
//...
}
I want to overload the operator
, so that I can get a row of the matrix, and after that, I want have the m[r][c]
access operator.
Is there any way to get a row, as a sequence of double
using std::slice
in the valarray
, so that if I change a value, it is changed also in the matrix?
I've read this definition in valarray:
std::slice_array<T> operator( std::slice slicearr );
My operator
must have std::slice_array<double>&
as returned type?
Thanks.
c++
I'm learning C++, so please be patient with me.
I have a std::valarray
in which there are double
elements and I consider it as a 2D matrix.
class Matrix {
valarray<double> elems;
int r, c;
public:
/* type? operator(int r) { return ? } */
//...
}
I want to overload the operator
, so that I can get a row of the matrix, and after that, I want have the m[r][c]
access operator.
Is there any way to get a row, as a sequence of double
using std::slice
in the valarray
, so that if I change a value, it is changed also in the matrix?
I've read this definition in valarray:
std::slice_array<T> operator( std::slice slicearr );
My operator
must have std::slice_array<double>&
as returned type?
Thanks.
c++
c++
edited Nov 19 '18 at 15:51
Swordfish
9,34811336
9,34811336
asked Nov 19 '18 at 15:36
SamSam
357
357
What is proxy class in C++
– Swordfish
Nov 19 '18 at 15:56
I asked a similar question back in 2016! :D
– Paula_plus_plus
Nov 19 '18 at 16:45
add a comment |
What is proxy class in C++
– Swordfish
Nov 19 '18 at 15:56
I asked a similar question back in 2016! :D
– Paula_plus_plus
Nov 19 '18 at 16:45
What is proxy class in C++
– Swordfish
Nov 19 '18 at 15:56
What is proxy class in C++
– Swordfish
Nov 19 '18 at 15:56
I asked a similar question back in 2016! :D
– Paula_plus_plus
Nov 19 '18 at 16:45
I asked a similar question back in 2016! :D
– Paula_plus_plus
Nov 19 '18 at 16:45
add a comment |
1 Answer
1
active
oldest
votes
I don't think std::slice
and std::slice_array
is what you're looking for, especially the latter is nothing but a helper type with a very limited public interface. You can instead return an proxy object. Here's a possible example of how to implement that.
class Matrix {
/* ... */
class RowProxy {
public:
RowProxy(std::valarray<double>& elems, int c, int row) :
elems(elems), c(c), row(row) {}
double& operator(int j)
{
return elems[row*c + j];
}
private:
std::valarray<double>& elems;
int row;
int c;
};
RowProxy operator(int i)
{
return RowProxy(elems, c, i);
}
};
This way, you can access the data with two operator
.
Matrix m(2, 4); // Assuming the ctor initializes elemens with row*column
m[0][0] = 1.234;
m[1][0] = 2.234;
m[1][3] = -52.023;
Note that both Matrix
and RowProxy
are missing overloads and proper handling for const
-ness, and variable names are poor. Also, you might want to think about an out-of-bounds error handling strategy. But it may serve as a starting point for your implementation.
ok, thanks! In this way I can access the element with m[r][c]. I would ask you, if I want to return an object like a Matrix_row() that contain the "reference" to elements in Matrix object, with the syntax m, how can I do? Is it possible?
– Sam
Nov 19 '18 at 17:08
1
@Sam This is exactly the proxy object. You can use it like this:auto row = m[0]
. Then, index the row asrow[0]
. This is exactly the same asm[0][0]
.
– lubgr
Nov 19 '18 at 19:13
oooh now I got it, thanks!! Just a question. You speak about error handling strategy, It's a good practise to use the valarray exception in constructor without checking size? I mean, in the constructor I pass int r, int c and I create a valarray with r * c elements, but if r * c is < 0 valarray throw an exception. It's a good practise to use that exception or it is better that I check size before create valarray?
– Sam
Nov 20 '18 at 10:44
1
@Sam I think it's fine to do that. Should ideally be documented somewhere. Note, that there is another error that might occur: accessing the array with an out-of-bounds index. This is quite common, you can have a look howstd::vector
handles that - there are two functions,std::vector::at
, which throws on out-of-bounds, andstd::vector::operator
which result in UB when called with an out-of-bounds index.
– lubgr
Nov 21 '18 at 19:46
Ok thanks!! I will do it.
– Sam
Nov 21 '18 at 20:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378000%2fhow-to-access-to-subsequence-of-a-valarray-considering-it-as-a-2d-matrix-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't think std::slice
and std::slice_array
is what you're looking for, especially the latter is nothing but a helper type with a very limited public interface. You can instead return an proxy object. Here's a possible example of how to implement that.
class Matrix {
/* ... */
class RowProxy {
public:
RowProxy(std::valarray<double>& elems, int c, int row) :
elems(elems), c(c), row(row) {}
double& operator(int j)
{
return elems[row*c + j];
}
private:
std::valarray<double>& elems;
int row;
int c;
};
RowProxy operator(int i)
{
return RowProxy(elems, c, i);
}
};
This way, you can access the data with two operator
.
Matrix m(2, 4); // Assuming the ctor initializes elemens with row*column
m[0][0] = 1.234;
m[1][0] = 2.234;
m[1][3] = -52.023;
Note that both Matrix
and RowProxy
are missing overloads and proper handling for const
-ness, and variable names are poor. Also, you might want to think about an out-of-bounds error handling strategy. But it may serve as a starting point for your implementation.
ok, thanks! In this way I can access the element with m[r][c]. I would ask you, if I want to return an object like a Matrix_row() that contain the "reference" to elements in Matrix object, with the syntax m, how can I do? Is it possible?
– Sam
Nov 19 '18 at 17:08
1
@Sam This is exactly the proxy object. You can use it like this:auto row = m[0]
. Then, index the row asrow[0]
. This is exactly the same asm[0][0]
.
– lubgr
Nov 19 '18 at 19:13
oooh now I got it, thanks!! Just a question. You speak about error handling strategy, It's a good practise to use the valarray exception in constructor without checking size? I mean, in the constructor I pass int r, int c and I create a valarray with r * c elements, but if r * c is < 0 valarray throw an exception. It's a good practise to use that exception or it is better that I check size before create valarray?
– Sam
Nov 20 '18 at 10:44
1
@Sam I think it's fine to do that. Should ideally be documented somewhere. Note, that there is another error that might occur: accessing the array with an out-of-bounds index. This is quite common, you can have a look howstd::vector
handles that - there are two functions,std::vector::at
, which throws on out-of-bounds, andstd::vector::operator
which result in UB when called with an out-of-bounds index.
– lubgr
Nov 21 '18 at 19:46
Ok thanks!! I will do it.
– Sam
Nov 21 '18 at 20:40
add a comment |
I don't think std::slice
and std::slice_array
is what you're looking for, especially the latter is nothing but a helper type with a very limited public interface. You can instead return an proxy object. Here's a possible example of how to implement that.
class Matrix {
/* ... */
class RowProxy {
public:
RowProxy(std::valarray<double>& elems, int c, int row) :
elems(elems), c(c), row(row) {}
double& operator(int j)
{
return elems[row*c + j];
}
private:
std::valarray<double>& elems;
int row;
int c;
};
RowProxy operator(int i)
{
return RowProxy(elems, c, i);
}
};
This way, you can access the data with two operator
.
Matrix m(2, 4); // Assuming the ctor initializes elemens with row*column
m[0][0] = 1.234;
m[1][0] = 2.234;
m[1][3] = -52.023;
Note that both Matrix
and RowProxy
are missing overloads and proper handling for const
-ness, and variable names are poor. Also, you might want to think about an out-of-bounds error handling strategy. But it may serve as a starting point for your implementation.
ok, thanks! In this way I can access the element with m[r][c]. I would ask you, if I want to return an object like a Matrix_row() that contain the "reference" to elements in Matrix object, with the syntax m, how can I do? Is it possible?
– Sam
Nov 19 '18 at 17:08
1
@Sam This is exactly the proxy object. You can use it like this:auto row = m[0]
. Then, index the row asrow[0]
. This is exactly the same asm[0][0]
.
– lubgr
Nov 19 '18 at 19:13
oooh now I got it, thanks!! Just a question. You speak about error handling strategy, It's a good practise to use the valarray exception in constructor without checking size? I mean, in the constructor I pass int r, int c and I create a valarray with r * c elements, but if r * c is < 0 valarray throw an exception. It's a good practise to use that exception or it is better that I check size before create valarray?
– Sam
Nov 20 '18 at 10:44
1
@Sam I think it's fine to do that. Should ideally be documented somewhere. Note, that there is another error that might occur: accessing the array with an out-of-bounds index. This is quite common, you can have a look howstd::vector
handles that - there are two functions,std::vector::at
, which throws on out-of-bounds, andstd::vector::operator
which result in UB when called with an out-of-bounds index.
– lubgr
Nov 21 '18 at 19:46
Ok thanks!! I will do it.
– Sam
Nov 21 '18 at 20:40
add a comment |
I don't think std::slice
and std::slice_array
is what you're looking for, especially the latter is nothing but a helper type with a very limited public interface. You can instead return an proxy object. Here's a possible example of how to implement that.
class Matrix {
/* ... */
class RowProxy {
public:
RowProxy(std::valarray<double>& elems, int c, int row) :
elems(elems), c(c), row(row) {}
double& operator(int j)
{
return elems[row*c + j];
}
private:
std::valarray<double>& elems;
int row;
int c;
};
RowProxy operator(int i)
{
return RowProxy(elems, c, i);
}
};
This way, you can access the data with two operator
.
Matrix m(2, 4); // Assuming the ctor initializes elemens with row*column
m[0][0] = 1.234;
m[1][0] = 2.234;
m[1][3] = -52.023;
Note that both Matrix
and RowProxy
are missing overloads and proper handling for const
-ness, and variable names are poor. Also, you might want to think about an out-of-bounds error handling strategy. But it may serve as a starting point for your implementation.
I don't think std::slice
and std::slice_array
is what you're looking for, especially the latter is nothing but a helper type with a very limited public interface. You can instead return an proxy object. Here's a possible example of how to implement that.
class Matrix {
/* ... */
class RowProxy {
public:
RowProxy(std::valarray<double>& elems, int c, int row) :
elems(elems), c(c), row(row) {}
double& operator(int j)
{
return elems[row*c + j];
}
private:
std::valarray<double>& elems;
int row;
int c;
};
RowProxy operator(int i)
{
return RowProxy(elems, c, i);
}
};
This way, you can access the data with two operator
.
Matrix m(2, 4); // Assuming the ctor initializes elemens with row*column
m[0][0] = 1.234;
m[1][0] = 2.234;
m[1][3] = -52.023;
Note that both Matrix
and RowProxy
are missing overloads and proper handling for const
-ness, and variable names are poor. Also, you might want to think about an out-of-bounds error handling strategy. But it may serve as a starting point for your implementation.
answered Nov 19 '18 at 16:03
lubgrlubgr
10.6k21845
10.6k21845
ok, thanks! In this way I can access the element with m[r][c]. I would ask you, if I want to return an object like a Matrix_row() that contain the "reference" to elements in Matrix object, with the syntax m, how can I do? Is it possible?
– Sam
Nov 19 '18 at 17:08
1
@Sam This is exactly the proxy object. You can use it like this:auto row = m[0]
. Then, index the row asrow[0]
. This is exactly the same asm[0][0]
.
– lubgr
Nov 19 '18 at 19:13
oooh now I got it, thanks!! Just a question. You speak about error handling strategy, It's a good practise to use the valarray exception in constructor without checking size? I mean, in the constructor I pass int r, int c and I create a valarray with r * c elements, but if r * c is < 0 valarray throw an exception. It's a good practise to use that exception or it is better that I check size before create valarray?
– Sam
Nov 20 '18 at 10:44
1
@Sam I think it's fine to do that. Should ideally be documented somewhere. Note, that there is another error that might occur: accessing the array with an out-of-bounds index. This is quite common, you can have a look howstd::vector
handles that - there are two functions,std::vector::at
, which throws on out-of-bounds, andstd::vector::operator
which result in UB when called with an out-of-bounds index.
– lubgr
Nov 21 '18 at 19:46
Ok thanks!! I will do it.
– Sam
Nov 21 '18 at 20:40
add a comment |
ok, thanks! In this way I can access the element with m[r][c]. I would ask you, if I want to return an object like a Matrix_row() that contain the "reference" to elements in Matrix object, with the syntax m, how can I do? Is it possible?
– Sam
Nov 19 '18 at 17:08
1
@Sam This is exactly the proxy object. You can use it like this:auto row = m[0]
. Then, index the row asrow[0]
. This is exactly the same asm[0][0]
.
– lubgr
Nov 19 '18 at 19:13
oooh now I got it, thanks!! Just a question. You speak about error handling strategy, It's a good practise to use the valarray exception in constructor without checking size? I mean, in the constructor I pass int r, int c and I create a valarray with r * c elements, but if r * c is < 0 valarray throw an exception. It's a good practise to use that exception or it is better that I check size before create valarray?
– Sam
Nov 20 '18 at 10:44
1
@Sam I think it's fine to do that. Should ideally be documented somewhere. Note, that there is another error that might occur: accessing the array with an out-of-bounds index. This is quite common, you can have a look howstd::vector
handles that - there are two functions,std::vector::at
, which throws on out-of-bounds, andstd::vector::operator
which result in UB when called with an out-of-bounds index.
– lubgr
Nov 21 '18 at 19:46
Ok thanks!! I will do it.
– Sam
Nov 21 '18 at 20:40
ok, thanks! In this way I can access the element with m[r][c]. I would ask you, if I want to return an object like a Matrix_row() that contain the "reference" to elements in Matrix object, with the syntax m, how can I do? Is it possible?
– Sam
Nov 19 '18 at 17:08
ok, thanks! In this way I can access the element with m[r][c]. I would ask you, if I want to return an object like a Matrix_row() that contain the "reference" to elements in Matrix object, with the syntax m, how can I do? Is it possible?
– Sam
Nov 19 '18 at 17:08
1
1
@Sam This is exactly the proxy object. You can use it like this:
auto row = m[0]
. Then, index the row as row[0]
. This is exactly the same as m[0][0]
.– lubgr
Nov 19 '18 at 19:13
@Sam This is exactly the proxy object. You can use it like this:
auto row = m[0]
. Then, index the row as row[0]
. This is exactly the same as m[0][0]
.– lubgr
Nov 19 '18 at 19:13
oooh now I got it, thanks!! Just a question. You speak about error handling strategy, It's a good practise to use the valarray exception in constructor without checking size? I mean, in the constructor I pass int r, int c and I create a valarray with r * c elements, but if r * c is < 0 valarray throw an exception. It's a good practise to use that exception or it is better that I check size before create valarray?
– Sam
Nov 20 '18 at 10:44
oooh now I got it, thanks!! Just a question. You speak about error handling strategy, It's a good practise to use the valarray exception in constructor without checking size? I mean, in the constructor I pass int r, int c and I create a valarray with r * c elements, but if r * c is < 0 valarray throw an exception. It's a good practise to use that exception or it is better that I check size before create valarray?
– Sam
Nov 20 '18 at 10:44
1
1
@Sam I think it's fine to do that. Should ideally be documented somewhere. Note, that there is another error that might occur: accessing the array with an out-of-bounds index. This is quite common, you can have a look how
std::vector
handles that - there are two functions, std::vector::at
, which throws on out-of-bounds, and std::vector::operator
which result in UB when called with an out-of-bounds index.– lubgr
Nov 21 '18 at 19:46
@Sam I think it's fine to do that. Should ideally be documented somewhere. Note, that there is another error that might occur: accessing the array with an out-of-bounds index. This is quite common, you can have a look how
std::vector
handles that - there are two functions, std::vector::at
, which throws on out-of-bounds, and std::vector::operator
which result in UB when called with an out-of-bounds index.– lubgr
Nov 21 '18 at 19:46
Ok thanks!! I will do it.
– Sam
Nov 21 '18 at 20:40
Ok thanks!! I will do it.
– Sam
Nov 21 '18 at 20:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378000%2fhow-to-access-to-subsequence-of-a-valarray-considering-it-as-a-2d-matrix-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is proxy class in C++
– Swordfish
Nov 19 '18 at 15:56
I asked a similar question back in 2016! :D
– Paula_plus_plus
Nov 19 '18 at 16:45