Skip to content

Commit 5dfe508

Browse files
fix issue #108 add cast function
1 parent ee8d37a commit 5dfe508

13 files changed

+413
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Features:
33

44
* [#110](http://github.com/Nelson-numerical-software/nelson/issues/110): add isproperty & ismethod to all handle types.
55
* Add "isHandleProperty", "isHandleMethod", "getHandleCategory" C++ API Methods.
6+
* [#108](http://github.com/Nelson-numerical-software/nelson/issues/108): cast function: converts variable to a different data type.
67

78

89
## 0.2.4 (2018-04-30)

modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
<ItemGroup>
171171
<ClCompile Include="..\cpp\absBuiltin.cpp" />
172172
<ClCompile Include="..\cpp\andBuiltin.cpp" />
173+
<ClCompile Include="..\cpp\castBuiltin.cpp" />
173174
<ClCompile Include="..\cpp\ceilBuiltin.cpp" />
174175
<ClCompile Include="..\cpp\colonBuiltin.cpp" />
175176
<ClCompile Include="..\cpp\complexBuiltin.cpp" />
@@ -251,6 +252,7 @@
251252
<ItemGroup>
252253
<ClInclude Include="..\include\absBuiltin.hpp" />
253254
<ClInclude Include="..\include\andBuiltin.hpp" />
255+
<ClInclude Include="..\include\castBuiltin.hpp" />
254256
<ClInclude Include="..\include\ceilBuiltin.hpp" />
255257
<ClInclude Include="..\include\colonBuiltin.hpp" />
256258
<ClInclude Include="..\include\complexBuiltin.hpp" />

modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj.filters

+6
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@
183183
<ClCompile Include="dllMain.c">
184184
<Filter>Source Files</Filter>
185185
</ClCompile>
186+
<ClCompile Include="..\cpp\castBuiltin.cpp">
187+
<Filter>Source Files</Filter>
188+
</ClCompile>
186189
</ItemGroup>
187190
<ItemGroup>
188191
<Text Include="..\..\CMakeLists.txt" />
@@ -364,5 +367,8 @@
364367
<ClInclude Include="..\include\vertcatBuiltin.hpp">
365368
<Filter>Header Files</Filter>
366369
</ClInclude>
370+
<ClInclude Include="..\include\castBuiltin.hpp">
371+
<Filter>Header Files</Filter>
372+
</ClInclude>
367373
</ItemGroup>
368374
</Project>

modules/elementary_functions/builtin/cpp/Gateway.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include "absBuiltin.hpp"
7272
#include "repmatBuiltin.hpp"
7373
#include "remBuiltin.hpp"
74+
#include "castBuiltin.hpp"
7475
//=============================================================================
7576
using namespace Nelson;
7677
//=============================================================================
@@ -134,6 +135,7 @@ static const nlsGateway gateway[] =
134135
{ "abs", Nelson::ElementaryFunctionsGateway::absBuiltin, 1, 1 },
135136
{ "repmat", Nelson::ElementaryFunctionsGateway::repmatBuiltin, 1, -1 },
136137
{ "rem", Nelson::ElementaryFunctionsGateway::remBuiltin, 1, 2 },
138+
{ "cast", Nelson::ElementaryFunctionsGateway::castBuiltin, 1, 3 },
137139
};
138140
//=============================================================================
139141
NLSGATEWAYFUNC(gateway)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//=============================================================================
2+
// Copyright (c) 2016-2018 Allan CORNET (Nelson)
3+
//=============================================================================
4+
// LICENCE_BLOCK_BEGIN
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 2 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
// LICENCE_BLOCK_END
18+
//=============================================================================
19+
#include "castBuiltin.hpp"
20+
#include "Error.hpp"
21+
#include "Cast.hpp"
22+
#include "StringToClass.hpp"
23+
#include "OverloadFunction.hpp"
24+
//=============================================================================
25+
using namespace Nelson;
26+
//=============================================================================
27+
ArrayOfVector Nelson::ElementaryFunctionsGateway::castBuiltin(Evaluator* eval, int nLhs, const ArrayOfVector& argIn)
28+
{
29+
ArrayOfVector retval;
30+
if (argIn.size() < 2 || argIn.size() > 3)
31+
{
32+
Error(eval, ERROR_WRONG_NUMBERS_INPUT_ARGS);
33+
}
34+
if (nLhs > 1)
35+
{
36+
Error(eval, ERROR_WRONG_NUMBERS_OUTPUT_ARGS);
37+
}
38+
bool bSuccess = false;
39+
retval = OverloadFunction(eval, nLhs, argIn, bSuccess);
40+
if (!bSuccess)
41+
{
42+
bool isSparse = false;
43+
Class destinationClass;
44+
if (argIn.size() == 2)
45+
{
46+
ArrayOf param2 = argIn[1];
47+
std::wstring dest = param2.getContentAsWideString();
48+
if (eval->getOverloadState())
49+
{
50+
Context *context = eval->getContext();
51+
FunctionDef *funcDef = nullptr;
52+
if (context->lookupFunction(dest, funcDef))
53+
{
54+
if ((funcDef->type() == NLS_BUILT_IN_FUNCTION) || (funcDef->type() == NLS_MACRO_FUNCTION))
55+
{
56+
bSuccess = true;
57+
ArrayOfVector argInCopy;
58+
argInCopy.push_back(argIn[0]);
59+
return funcDef->evaluateFunction(eval, argInCopy, nLhs);
60+
}
61+
}
62+
}
63+
destinationClass = StringToClass(dest);
64+
}
65+
else
66+
{
67+
ArrayOf param2 = argIn[1];
68+
std::wstring like = param2.getContentAsWideString();
69+
if (like != L"like")
70+
{
71+
Error(eval, ERROR_WRONG_ARGUMENT_2_VALUE);
72+
}
73+
destinationClass = argIn[2].getDataClass();
74+
isSparse = argIn[2].isSparse();
75+
}
76+
retval.push_back(Cast(argIn[0], destinationClass, isSparse));
77+
}
78+
return retval;
79+
}
80+
//=============================================================================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//=============================================================================
2+
// Copyright (c) 2016-2018 Allan CORNET (Nelson)
3+
//=============================================================================
4+
// LICENCE_BLOCK_BEGIN
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 2 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
// LICENCE_BLOCK_END
18+
//=============================================================================
19+
#pragma once
20+
//=============================================================================
21+
#include "ArrayOf.hpp"
22+
#include "Evaluator.hpp"
23+
//=============================================================================
24+
namespace Nelson {
25+
namespace ElementaryFunctionsGateway {
26+
ArrayOfVector castBuiltin(Evaluator* eval, int nLhs, const ArrayOfVector& argIn);
27+
}
28+
}//=============================================================================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xmldoc>
3+
<copyright>SAME AS NELSON SOFTWARE</copyright>
4+
5+
<language>en_US</language>
6+
<keyword>cast</keyword>
7+
<short_description>Converts variable to a different data type</short_description>
8+
9+
<syntax>
10+
<syntax_item>R = cast(V, type_destination)</syntax_item>
11+
<syntax_item>R = cast(V, 'like', W)</syntax_item>
12+
13+
</syntax>
14+
15+
<param_input>
16+
<param_input_item>
17+
<param_name>V</param_name>
18+
<param_description>a variable</param_description>
19+
</param_input_item>
20+
<param_input_item>
21+
<param_name>type_destination</param_name>
22+
<param_description>a string: name of destination data type.</param_description>
23+
</param_input_item>
24+
<param_input_item>
25+
<param_name>W</param_name>
26+
<param_description>a variable</param_description>
27+
</param_input_item>
28+
</param_input>
29+
30+
<param_output>
31+
32+
<param_output_item>
33+
<param_name>R</param_name>
34+
<param_description>a variable with new data type.</param_description>
35+
</param_output_item>
36+
</param_output>
37+
38+
<description>
39+
<p><b>cast</b> converts variable to a different data type.</p>
40+
<p><b>R = cast(V, 'like', W)</b> converts varible V to sparsity and same data type than W.</p>
41+
</description>
42+
43+
44+
<used_function></used_function>
45+
<bibliography></bibliography>
46+
47+
<examples>
48+
49+
<example_item>
50+
<example_item_type>nelson</example_item_type>
51+
<example_item_description></example_item_description>
52+
<example_item_data><![CDATA[r = cast([3.6 1.2 -2.4], 'like', int64(3))
53+
r = cast([3.6 1.2 -2.4], 'int64')]]>
54+
</example_item_data>
55+
</example_item>
56+
57+
58+
</examples>
59+
60+
<see_also>
61+
<see_also_item>
62+
<link linkend="${types}class">class</link>
63+
</see_also_item>
64+
</see_also>
65+
66+
<history>
67+
<history_item>
68+
<history_version>1.0.0</history_version>
69+
<history_description>initial version</history_description>
70+
</history_item>
71+
</history>
72+
73+
<authors>
74+
<author_item>Allan CORNET</author_item>
75+
</authors>
76+
</xmldoc>
77+
78+

modules/elementary_functions/src/c/nlsElementary_functions.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
<ItemGroup>
167167
<ClCompile Include="..\cpp\AbsoluteValue.cpp" />
168168
<ClCompile Include="..\cpp\And.cpp" />
169+
<ClCompile Include="..\cpp\Cast.cpp" />
169170
<ClCompile Include="..\cpp\ComplexConjugate.cpp" />
170171
<ClCompile Include="..\cpp\ComplexConstructor.cpp" />
171172
<ClCompile Include="..\cpp\ComplexTranspose.cpp" />
@@ -201,6 +202,7 @@
201202
<ItemGroup>
202203
<ClInclude Include="..\include\AbsoluteValue.hpp" />
203204
<ClInclude Include="..\include\And.hpp" />
205+
<ClInclude Include="..\include\Cast.hpp" />
204206
<ClInclude Include="..\include\ComplexConjugate.hpp" />
205207
<ClInclude Include="..\include\ComplexConstructor.hpp" />
206208
<ClInclude Include="..\include\ComplexTranspose.hpp" />

modules/elementary_functions/src/c/nlsElementary_functions.vcxproj.filters

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
<ClCompile Include="..\cpp\TruncateFunctions.cpp">
115115
<Filter>Source Files</Filter>
116116
</ClCompile>
117+
<ClCompile Include="..\cpp\Cast.cpp">
118+
<Filter>Source Files</Filter>
119+
</ClCompile>
117120
</ItemGroup>
118121
<ItemGroup>
119122
<ClInclude Include="..\include\AbsoluteValue.hpp">
@@ -218,6 +221,9 @@
218221
<ClInclude Include="..\include\TruncateFunctions.hpp">
219222
<Filter>Header Files</Filter>
220223
</ClInclude>
224+
<ClInclude Include="..\include\Cast.hpp">
225+
<Filter>Header Files</Filter>
226+
</ClInclude>
221227
</ItemGroup>
222228
<ItemGroup>
223229
<Text Include="..\..\CMakeLists.txt" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//=============================================================================
2+
// Copyright (c) 2016-2018 Allan CORNET (Nelson)
3+
//=============================================================================
4+
// LICENCE_BLOCK_BEGIN
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 2 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
// LICENCE_BLOCK_END
18+
//=============================================================================
19+
#include "Cast.hpp"
20+
#include "ClassName.hpp"
21+
//=============================================================================
22+
namespace Nelson {
23+
//=============================================================================
24+
ArrayOf Cast(ArrayOf arrayIn, Class destinationClass, bool isSparse)
25+
{
26+
ArrayOf res;
27+
res = arrayIn;
28+
Class originClass = res.getDataClass();
29+
if (originClass == NLS_SCOMPLEX || originClass == NLS_DCOMPLEX)
30+
{
31+
if (destinationClass == NLS_DOUBLE)
32+
{
33+
destinationClass = NLS_DCOMPLEX;
34+
}
35+
if (destinationClass == NLS_SINGLE)
36+
{
37+
destinationClass = NLS_SCOMPLEX;
38+
}
39+
}
40+
if ((originClass == NLS_SCOMPLEX || originClass == NLS_DCOMPLEX) &&
41+
(destinationClass != NLS_SCOMPLEX && destinationClass != NLS_DCOMPLEX))
42+
{
43+
throw Exception(_W("Invalid conversion from complex."));
44+
}
45+
res.promoteType(destinationClass);
46+
if (isSparse)
47+
{
48+
res.makeSparse();
49+
}
50+
return res;
51+
}
52+
//=============================================================================
53+
}
54+
//=============================================================================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//=============================================================================
2+
// Copyright (c) 2016-2018 Allan CORNET (Nelson)
3+
//=============================================================================
4+
// LICENCE_BLOCK_BEGIN
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 2 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
// LICENCE_BLOCK_END
18+
//=============================================================================
19+
#pragma once
20+
//=============================================================================
21+
#include "ArrayOf.hpp"
22+
#include "nlsElementary_functions_exports.h"
23+
//=============================================================================
24+
namespace Nelson {
25+
NLSELEMENTARY_FUNCTIONS_IMPEXP ArrayOf Cast(ArrayOf arrayIn, Class destinationClass, bool isSparse);
26+
}
27+
//=============================================================================

0 commit comments

Comments
 (0)