Author |
Topic  |
|
couturier
France
291 Posts |
Posted - 01/22/2018 : 06:00:37 AM
|
Origin Ver. and Service Release (Select Help-->About Origin): 2018 Operating System: win10
Hi all,
I have a basic question about number formats. Consider the following OC function:
void test_func(vector& input, vector& output)
{
output = input;
}
This works fine as long as input column is double but fails if col is any other numeric format (real, long, ...) How can I make it work whatever the input format ?
Thanks |
|
Chris D
428 Posts |
Posted - 01/22/2018 : 10:09:05 AM
|
Hi Antoine,
It is because the default type for vector is double. That is:
void test_func(vector& input, vector& output);
is same as:
void test_func(vector<double>& input, vector<double>& output);
One option is to use VectorBase types (see https://www.originlab.com/doc/OriginC/ref/vectorbase). Check out the example below. Notice, the effect when you run it.
#include <Origin.h>
void test_func(vectorbase& input, vectorbase& output)
{
output = input;
}
void run_test()
{
vector<double> vd = {1.1,2.2,3.9};
vector<string> vn;
test_func(vd, vn);
// Put breakpoint here below to inspect.
int nn = 1;
}
Note, you still have to be careful what you do in the function to make sure it can be done with an instance of the VectorBase class. For example, if you pass a string vector as the 2nd param, it will compile but the direct assignment will throw an error.
I hope this helps.
Thanks, Chris Drozdowski Originlab Technical Support
|
Edited by - Chris D on 01/22/2018 10:09:53 AM |
 |
|
couturier
France
291 Posts |
Posted - 01/22/2018 : 10:59:11 AM
|
Thanks a lot Chris You made my day :-) |
 |
|
couturier
France
291 Posts |
Posted - 05/28/2018 : 07:05:49 AM
|
Hi,
I have some further questions regarding this topic. A lot of OC functions won't allow vectorbase as argument (all the ocmath_*, fft_smooth ...).
For example:
void test_derivative(vectorbase vx, vectorbase& vy)
{
int nRet = ocmath_derivative(vx, vy, vx.GetSize());
} won't compile.
As a workaround, I can rewrite as
void test_derivative(vectorbase vx, vectorbase& vy)
{
vector v1, v2;
v1=vx;
v2=vy;
int nRet = ocmath_derivative(v1, v2, vx.GetSize());
vy=v2;
} but that makes 3 copies that can eat some time in case of big vectors. Is there any trick ?
another thing I don't understand:
void testsqr(vectorbase vin, vectorbase& vout)
{
vin*=vin;
vout=vin;
} will output Error, Incompatible variable types in expression What's wrong ?
thanks for the help |
 |
|
yuki_wu
896 Posts |
Posted - 05/29/2018 : 04:51:27 AM
|
Hi,
#1 To support other data types besides double, we could use vectorbase like Chris’s suggestion. To use the function ocmath_derivative, we could only transfer the vectorbase to the vector as what you did.
The only thing I can think of to improve is to change the function to:
void test_derivative(const vectorbase& vx, vectorbase& vy) It may help to save the memory and processing time.
#2 It seems that you miss & in vectorbase vin, add the & and it should work fine.
void testsqr(vectorbase& vin, vectorbase& vout)
{
vin*=vin;
vout=vin;
} Hope it helps.
Regards, Yuki OriginLab
|
Edited by - yuki_wu on 05/29/2018 04:54:13 AM |
 |
|
couturier
France
291 Posts |
Posted - 05/29/2018 : 06:04:05 AM
|
Hi Yuki,
thanks for your answer
#1 Could you explain what the const will add, regarding memory and speed, in argument declaration ? This is something I'm definitely not used to.
#2 The & omission was on purpose, because I want to leave vin untouched. In your example,, vin will be replaced by its square.
void testsqr(vector vin, vector& vout) { vin*=vin; vout=vin; }
works fine, so why it doesn't work with vectorbase ? |
 |
|
yuki_wu
896 Posts |
Posted - 05/30/2018 : 01:52:14 AM
|
Hi,
1# In the original form,
void test_derivative(vectorbase vx, vectorbase& vy) accepts the parameter vx passed to it which is in fact a copy. Therefore, it should take some time and memory to generate this copy.
Instead, in the new form:
void test_derivative(const vectorbase& vx, vectorbase& vy) will cause the parameter vx to passed without copying(in addition of an “&”) but stop it from then being altered (const).
2# Similar, we could change the function like:
void testsqr(const vectorbase& vin, vectorbase& vout)
{
vout = vin * vin;
} Regards, Yuki OriginLab
|
Edited by - yuki_wu on 05/30/2018 01:52:35 AM |
 |
|
Castiel
343 Posts |
Posted - 05/30/2018 : 02:09:27 AM
|
quote: Originally posted by couturier
Hi,
I have some further questions regarding this topic. A lot of OC functions won't allow vectorbase as argument (all the ocmath_*, fft_smooth ...).
For example:
void test_derivative(vectorbase vx, vectorbase& vy)
{
int nRet = ocmath_derivative(vx, vy, vx.GetSize());
} won't compile.
vectorbase is an ABC (Abstract Base Class). It cannot be used that way.
ocmath_derivative(const double*, double*, uint, DWORD) suggests that you must pass double*/vector<double>/vector to it, not vectorbase. If the data is in float/long/... type casting is inevitable.
quote:
As a workaround, I can rewrite as
void test_derivative(vectorbase vx, vectorbase& vy)
{
vector v1, v2;
v1=vx;
v2=vy;
int nRet = ocmath_derivative(v1, v2, vx.GetSize());
vy=v2;
} but that makes 3 copies that can eat some time in case of big vectors. Is there any trick ?
Type casting and copies of vx and vy are inevitable if you want to support vx and vy in formats other than double precision floating-point. The above code may produce no error during compile-time. Does it really work? Doubted. As function parameter, it should be vectorbase& instead of vectorbase (vectorbase is but an ABC).
quote:
another thing I don't understand:
void testsqr(vectorbase vin, vectorbase& vout)
{
vin*=vin;
vout=vin;
} will output Error, Incompatible variable types in expression What's wrong ?
thanks for the help
Operator *= is not defined for vectorbase. Once again, as function parameter, it should be vectorbase& instead of vectorbase (vectorbase is but an ABC).
#####
#### _\_ ________
##=-[.].]| \
#( _\ | |------|
# __| | ||||||||
\ _/ | ||||||||
.--'--'-. | | ____ |
/ __ `|__|[o__o]|
_(____nm_______ /____\____
|
 |
|
couturier
France
291 Posts |
Posted - 05/30/2018 : 05:42:42 AM
|
Thanks a lot, Yuki and Castiel, for the clear explanations. |
 |
|
|
Topic  |
|
|
|