Skip to content

Commit

Permalink
fix #373 'sign' builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Mar 18, 2021
1 parent 2b189ea commit d3fef96
Show file tree
Hide file tree
Showing 18 changed files with 620 additions and 46 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

## Features:

- [#373](http://github.com/Nelson-numerical-software/nelson/issues/373): `sign` builtin.

- `MException` comes default exception in Nelson.

- `try, catch` extended to manage `MException`.
Expand Down
Expand Up @@ -239,6 +239,7 @@
<ClCompile Include="..\cpp\roundBuiltin.cpp" />
<ClCompile Include="..\cpp\shortcutandBuiltin.cpp" />
<ClCompile Include="..\cpp\shortcutorBuiltin.cpp" />
<ClCompile Include="..\cpp\signBuiltin.cpp" />
<ClCompile Include="..\cpp\sizeBuiltin.cpp" />
<ClCompile Include="..\cpp\sortBuiltin.cpp" />
<ClCompile Include="..\cpp\sqrtBuiltin.cpp" />
Expand Down Expand Up @@ -352,6 +353,7 @@
<ClInclude Include="..\include\roundBuiltin.hpp" />
<ClInclude Include="..\include\shortcutandBuiltin.hpp" />
<ClInclude Include="..\include\shortcutorBuiltin.hpp" />
<ClInclude Include="..\include\signBuiltin.hpp" />
<ClInclude Include="..\include\sizeBuiltin.hpp" />
<ClInclude Include="..\include\sortBuiltin.hpp" />
<ClInclude Include="..\include\sqrtBuiltin.hpp" />
Expand Down
Expand Up @@ -270,6 +270,9 @@
<ClCompile Include="..\cpp\trilBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\signBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\CMakeLists.txt" />
Expand Down Expand Up @@ -538,5 +541,8 @@
<ClInclude Include="..\include\trilBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\signBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions modules/elementary_functions/builtin/cpp/Gateway.cpp
Expand Up @@ -107,6 +107,7 @@
#include "isscalarBuiltin.hpp"
#include "triuBuiltin.hpp"
#include "trilBuiltin.hpp"
#include "signBuiltin.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
Expand Down Expand Up @@ -287,6 +288,9 @@ static const nlsGateway gateway[] = {
CPP_BUILTIN_WITH_EVALUATOR },
{ "tril", (void*)Nelson::ElementaryFunctionsGateway::trilBuiltin, 1, 2,
CPP_BUILTIN_WITH_EVALUATOR },
{ "sign", (void*)Nelson::ElementaryFunctionsGateway::signBuiltin, 1, 1,
CPP_BUILTIN_WITH_EVALUATOR },

};
//=============================================================================
NLSGATEWAYFUNC(gateway)
Expand Down
56 changes: 56 additions & 0 deletions modules/elementary_functions/builtin/cpp/signBuiltin.cpp
@@ -0,0 +1,56 @@
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see <http://www.gnu.org/licenses/>.
// LICENCE_BLOCK_END
//=============================================================================
#include "signBuiltin.hpp"
#include "Error.hpp"
#include "OverloadFunction.hpp"
#include "OverloadRequired.hpp"
#include "Sign.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
ArrayOfVector
Nelson::ElementaryFunctionsGateway::signBuiltin(
Evaluator* eval, int nLhs, const ArrayOfVector& argIn)
{
ArrayOfVector retval;
bool bSuccess = false;
nargincheck(argIn, 1, 1);
nargoutcheck(nLhs, 0, 1);
if (eval->mustOverloadBasicTypes()) {
retval = OverloadFunction(eval, nLhs, argIn, "sign", bSuccess);
}
if (!bSuccess) {
bool needToOverload = false;
ArrayOf res = Sign(argIn[0], needToOverload);
if (needToOverload) {
retval = OverloadFunction(eval, nLhs, argIn, "sign");
} else {
retval << res;
}
}
return retval;
}
//=============================================================================
39 changes: 39 additions & 0 deletions modules/elementary_functions/builtin/include/signBuiltin.hpp
@@ -0,0 +1,39 @@
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see <http://www.gnu.org/licenses/>.
// LICENCE_BLOCK_END
//=============================================================================
#pragma once
//=============================================================================
#include "ArrayOf.hpp"
#include "Evaluator.hpp"
//=============================================================================
namespace Nelson {
//=============================================================================
namespace ElementaryFunctionsGateway {
ArrayOfVector
signBuiltin(Evaluator* eval, int nLhs, const ArrayOfVector& argIn);
}
//=============================================================================
} // namespace Nelson
//=============================================================================
70 changes: 70 additions & 0 deletions modules/elementary_functions/help/en_US/xml/sign.xml
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<xmldoc>
<copyright>SAME AS NELSON SOFTWARE</copyright>

<language>en_US</language>
<keyword>sign</keyword>
<short_description>Find the sign function of a number.</short_description>

<syntax>
<syntax_item>R = sign(M)</syntax_item>
</syntax>

<param_input>
<param_input_item>
<param_name>M</param_name>
<param_description>a variable</param_description>
</param_input_item>
</param_input>

<param_output>
<param_output_item>
<param_name>R</param_name>
<param_description>result of sign.</param_description>
</param_output_item>
</param_output>

<description>
<p><b>sign</b> find the sign function of a number.</p>
<p>-1 if the corresponding element of M is less than 0.</p>
<p>0 if the corresponding element of M equals 0.</p>
<p>1 if the corresponding element of M is greater than 0.</p>
<p>If input argument is a complex number, <b>sign</b> computes <b>M ./ abs(M)</b>.</p>
</description>


<used_function></used_function>
<bibliography></bibliography>

<examples>

<example_item>
<example_item_type>nelson</example_item_type>
<example_item_description></example_item_description>
<example_item_data>
<![CDATA[V = [-1 0 15 NaN Inf];
sign(V)]]>
</example_item_data>
</example_item>
</examples>

<see_also>
<see_also_item>
<link linkend="${elementary_functions}conj">conj</link>
</see_also_item>
<see_also_item>
<link linkend="${elementary_functions}abs">abs</link>
</see_also_item>
</see_also>

<history>
<history_item>
<history_version>1.0.0</history_version>
<history_description>initial version</history_description>
</history_item>
</history>

<authors>
<author_item>Allan CORNET</author_item>
</authors>
</xmldoc>
Expand Up @@ -206,6 +206,7 @@
<ClCompile Include="..\cpp\LowerTrianglePart.cpp" />
<ClCompile Include="..\cpp\Maximum.cpp" />
<ClCompile Include="..\cpp\Minimum.cpp" />
<ClCompile Include="..\cpp\Sign.cpp" />
<ClCompile Include="..\cpp\Sort.cpp" />
<ClCompile Include="..\cpp\SortCellHelpers.cpp" />
<ClCompile Include="..\cpp\SortStringHelpers.cpp" />
Expand Down Expand Up @@ -338,6 +339,7 @@
<ClInclude Include="..\include\RelationOperator.hpp" />
<ClInclude Include="..\include\Remainder.hpp" />
<ClInclude Include="..\include\RightDivide.hpp" />
<ClInclude Include="..\include\Sign.hpp" />
<ClInclude Include="..\include\Sort.hpp" />
<ClInclude Include="..\include\Sqrt.hpp" />
<ClInclude Include="..\include\StringArrayAddition.hpp" />
Expand Down
Expand Up @@ -243,6 +243,9 @@
<ClCompile Include="..\cpp\LowerTrianglePart.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\Sign.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\AbsoluteValue.hpp">
Expand Down Expand Up @@ -512,6 +515,9 @@
<ClInclude Include="..\include\LowerTrianglePart.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\Sign.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\CMakeLists.txt" />
Expand Down

0 comments on commit d3fef96

Please sign in to comment.