<commit_before>/*
 * DIPlib 3.0
 * This file contains the declaration and definition of arithmetic, trigonometric and similar monadic operators.
 *
 * (c)2017, <NAME>.
 * Based on original DIPlib code: (c)1995-2014, Delft University of Technology.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


// This file is meant to be included only by `include/diplib/math.h` and `src/math/monadic_operators.cpp`
// Do not include this file elsewhere! Things will break!

// Note that this file generates function declarations when included by `include/diplib/math.h`,
// and function definitions when included by `src/math/monadic_operators.cpp`.

#if !defined(DIP_MATH_H) || defined(DIP__MONADIC_OPERATORS_PRIVATE)
#error Do not include the 'diplib/monadic_operators.private' file directly!
#endif

#define DIP__MONADIC_OPERATORS_PRIVATE

#ifndef DIP__MONADIC_OPERATOR_FLEX

#define DIP__MONADIC_OPERATOR_FLEX( functionName_, functionLambda_, inputDomain_ ) \
   DIP_EXPORT void functionName_( Image const& in, Image& out ); \
   inline Image functionName_( Image const& in ) { Image out; functionName_( in, out ); return out; }

#define DIP__MONADIC_OPERATOR_FLOAT( functionName_, functionLambda_, inputDomain_ ) \
   DIP_EXPORT void functionName_( Image const& in, Image& out ); \
   inline Image functionName_( Image const& in ) { Image out; functionName_( in, out ); return out; }

#define DIP__MONADIC_OPERATOR_FLOAT_WITH_PARAM( functionName_, paramType_, paramName_, functionLambda_, inputDomain_ ) \
   DIP_EXPORT void functionName_( Image const& in, Image& out, paramType_ paramName_ ); \
   inline Image functionName_( Image const& in, paramType_ paramName_ ) { Image out; functionName_( in, out, paramName_ ); return out; }

#define DIP__MONADIC_OPERATOR_BIN( functionName_, functionLambda_, inputDomain_, defaultValue_ ) \
   DIP_EXPORT void functionName_( Image const& in, Image& out ); \
   inline Image functionName_( Image const& in ) { Image out; functionName_( in, out ); return out; }

#endif

namespace dip {

/// \addtogroup math_arithmetic
/// \{

/// \brief Computes the square of the modulus of each sample.
DIP__MONADIC_OPERATOR_FLOAT( SquareModulus,
                             []( auto its ) { return dipm__SquareModulus( *its[ 0 ] ); },
                             DataType::Class_NonBinary )

/// \brief Computes the phase (angle on complex plane, through `std::arg`) of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Phase, []( auto its ) { return std::arg( *its[ 0 ] ); }, DataType::Class_NonBinary )

/// \brief Computes the nearest integer to each sample (rounds).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Round, []( auto its ) { return std::round( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the smallest integer larger or equal to each sample (rounds up).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Ceil, []( auto its ) { return std::ceil( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the largest integer smaller or equal to each sample (rounds down).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Floor, []( auto its ) { return std::floor( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the truncated value of each sample (rounds towards zero).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Truncate, []( auto its ) { return std::trunc( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the fractional value of each sample (`out = in - dip::Truncate(in)`).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Fraction, []( auto its ) { return dipm__Fraction( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the reciprocal of each sample: out = in == 0 ? 0 : 1/in.
DIP__MONADIC_OPERATOR_FLEX( Reciprocal, []( auto its ) { return dipm__Reciprocal( *its[ 0 ] ); }, DataType::Class_NonBinary )   // x==0 ? 0 : 1/x

/// \brief Computes the square root of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Sqrt, []( auto its ) { return std::sqrt( *its[ 0 ] ); }, DataType::Class_Real )

/// \brief Computes the base e exponent (natural exponential) of each sample.
DIP__MONADIC_OPERATOR_FLEX( Exp, []( auto its ) { return std::exp( *its[ 0 ] ); }, DataType::Class_NonBinary )

/// \brief Computes the base 2 exponent of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Exp2, []( auto its ) { return std::exp2( *its[ 0 ] ); }, DataType::Class_Real )

/// \brief Computes the base 10 exponent of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Exp10, []( auto its ) { return std::pow( decltype( *its[ 0 ] )( 10 ), *its[ 0 ] ); }, DataType::Class_Real )

/// \brief Computes the natural logarithm (base e logarithm) of each sample.
DIP__MONADIC_OPERATOR_FLEX( Ln, []( auto its ) { return std::log( *its[ 0 ] ); }, DataType::Class_NonBinary )

/// \brief Computes the base 2 logarithm of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Log2, []( auto its ) { return std::log( *its[ 0 ] ); }, DataType::Class_Real )

/// \brief Computes the base 10 logarithm of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Log10, []( auto its ) { return std::log10( *its[ 0 ] ); }, DataType::Class_Real )
<commit_msg>Added monadic operator `dip::Square`. `dip::Sqrt` applies to complex input also.<commit_after>/*
 * DIPlib 3.0
 * This file contains the declaration and definition of arithmetic, trigonometric and similar monadic operators.
 *
 * (c)2017, <NAME>.
 * Based on original DIPlib code: (c)1995-2014, Delft University of Technology.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


// This file is meant to be included only by `include/diplib/math.h` and `src/math/monadic_operators.cpp`
// Do not include this file elsewhere! Things will break!

// Note that this file generates function declarations when included by `include/diplib/math.h`,
// and function definitions when included by `src/math/monadic_operators.cpp`.

#if !defined(DIP_MATH_H) || defined(DIP__MONADIC_OPERATORS_PRIVATE)
#error Do not include the 'diplib/monadic_operators.private' file directly!
#endif

#define DIP__MONADIC_OPERATORS_PRIVATE

#ifndef DIP__MONADIC_OPERATOR_FLEX

#define DIP__MONADIC_OPERATOR_FLEX( functionName_, functionLambda_, inputDomain_ ) \
   DIP_EXPORT void functionName_( Image const& in, Image& out ); \
   inline Image functionName_( Image const& in ) { Image out; functionName_( in, out ); return out; }

#define DIP__MONADIC_OPERATOR_FLOAT( functionName_, functionLambda_, inputDomain_ ) \
   DIP_EXPORT void functionName_( Image const& in, Image& out ); \
   inline Image functionName_( Image const& in ) { Image out; functionName_( in, out ); return out; }

#define DIP__MONADIC_OPERATOR_FLOAT_WITH_PARAM( functionName_, paramType_, paramName_, functionLambda_, inputDomain_ ) \
   DIP_EXPORT void functionName_( Image const& in, Image& out, paramType_ paramName_ ); \
   inline Image functionName_( Image const& in, paramType_ paramName_ ) { Image out; functionName_( in, out, paramName_ ); return out; }

#define DIP__MONADIC_OPERATOR_BIN( functionName_, functionLambda_, inputDomain_, defaultValue_ ) \
   DIP_EXPORT void functionName_( Image const& in, Image& out ); \
   inline Image functionName_( Image const& in ) { Image out; functionName_( in, out ); return out; }

#endif

namespace dip {

/// \addtogroup math_arithmetic
/// \{

/// \brief Computes the square of the modulus of each sample.
DIP__MONADIC_OPERATOR_FLOAT( SquareModulus,
                             []( auto its ) { return dipm__SquareModulus( *its[ 0 ] ); },
                             DataType::Class_NonBinary )

/// \brief Computes the phase (angle on complex plane, through `std::arg`) of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Phase, []( auto its ) { return std::arg( *its[ 0 ] ); }, DataType::Class_NonBinary )

/// \brief Computes the nearest integer to each sample (rounds).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Round, []( auto its ) { return std::round( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the smallest integer larger or equal to each sample (rounds up).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Ceil, []( auto its ) { return std::ceil( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the largest integer smaller or equal to each sample (rounds down).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Floor, []( auto its ) { return std::floor( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the truncated value of each sample (rounds towards zero).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Truncate, []( auto its ) { return std::trunc( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the fractional value of each sample (`out = in - dip::Truncate(in)`).
/// Only defined for floating-point types, the output is the same type.
DIP__MONADIC_OPERATOR_FLOAT( Fraction, []( auto its ) { return dipm__Fraction( *its[ 0 ] ); }, DataType::Class_Float )

/// \brief Computes the reciprocal of each sample: out = in == 0 ? 0 : 1/in.
DIP__MONADIC_OPERATOR_FLEX( Reciprocal, []( auto its ) { return dipm__Reciprocal( *its[ 0 ] ); }, DataType::Class_NonBinary )   // x==0 ? 0 : 1/x

/// \brief Computes the square of each sample.
DIP__MONADIC_OPERATOR_FLEX( Square, []( auto its ) { return *its[ 0 ] * *its[ 0 ]; }, DataType::Class_NonBinary )

/// \brief Computes the square root of each sample.
DIP__MONADIC_OPERATOR_FLEX( Sqrt, []( auto its ) { return std::sqrt( *its[ 0 ] ); }, DataType::Class_NonBinary )

/// \brief Computes the base e exponent (natural exponential) of each sample.
DIP__MONADIC_OPERATOR_FLEX( Exp, []( auto its ) { return std::exp( *its[ 0 ] ); }, DataType::Class_NonBinary )

/// \brief Computes the base 2 exponent of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Exp2, []( auto its ) { return std::exp2( *its[ 0 ] ); }, DataType::Class_Real )

/// \brief Computes the base 10 exponent of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Exp10, []( auto its ) { return std::pow( decltype( *its[ 0 ] )( 10 ), *its[ 0 ] ); }, DataType::Class_Real )

/// \brief Computes the natural logarithm (base e logarithm) of each sample.
DIP__MONADIC_OPERATOR_FLEX( Ln, []( auto its ) { return std::log( *its[ 0 ] ); }, DataType::Class_NonBinary )

/// \brief Computes the base 2 logarithm of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Log2, []( auto its ) { return std::log( *its[ 0 ] ); }, DataType::Class_Real )

/// \brief Computes the base 10 logarithm of each sample.
DIP__MONADIC_OPERATOR_FLOAT( Log10, []( auto its ) { return std::log10( *its[ 0 ] ); }, DataType::Class_Real )

{'fraction_non_alphanumeric': 0.11614255765199162, 'fraction_numerical': 0.009727463312368973, 'mean_word_length': 4.194250871080139, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1549240', 'n_tokens_mistral': 3766, 'n_tokens_neox': 3620, 'n_words': 1294}