コード例 #1
0
ファイル: char_conversion.hpp プロジェクト: EzoeRyou/Sprout
		inline SPROUT_CONSTEXPR Elem
		int_to_char(IntType val, int base) {
			return SPROUT_ASSERT(2 <= base && base <= 36), SPROUT_ASSERT(IntType(0) <= val && val < static_cast<IntType>(base)),
				val < 10 ? static_cast<Elem>('0') + val
					: static_cast<Elem>('a') + (val - 10)
				;
		}
コード例 #2
0
    constexpr PF <p> inverse(const PF <p> &pf)
    {
        SPROUT_ASSERT(pf.n != 0, "zero has no inverse.");
        int unit  = 1;
        int upper = pf.n * (p - 1);
        for (; unit <= upper; unit += p) {
            if (unit % pf.n == 0) {
                return PF <p>(unit / pf.n);
            }
        }
        SPROUT_ASSERT(false, "no inverse.");

        return PF <p>(0);
    }
コード例 #3
0
ファイル: g3.cpp プロジェクト: kundor/Sprout
	SPROUT_CXX14_CONSTEXPR void
	train(
		ForwardIterator1 in_first, ForwardIterator1 in_last,
		ForwardIterator2 t_first, ForwardIterator2 t_last,
		std::size_t repeat = 1000, value_type eta = value_type(0.1)
		)
	{
		SPROUT_ASSERT(sprout::distance(in_first, in_last) % In == 0);
		SPROUT_ASSERT(sprout::distance(in_first, in_last) / In == sprout::distance(t_first, t_last));

		worker work{};
		for (std::size_t times = 0; times != repeat; ++times) {
			ForwardIterator1 in_it = in_first;
			ForwardIterator2 t_it = t_first;
			for (; in_it != in_last; sprout::advance(in_it, In), ++t_it) {
				// forward propagation
				forward_propagation(in_it, sprout::next(in_it, In), work);

				// error calculation of output layer
				for (std::size_t i = 0; i != Out; ++i) {
					d3[i] = *t_it == i ? work.o3[i] - 1
						: work.o3[i]
						;
				}

				// weight update of output layer
				for (std::size_t i = 0; i != Hid + 1; ++i) {
					for (std::size_t j = 0; j != Out; ++j) {
						w2[i * Out + j] -= eta * d3[j] * work.o2[i];
					}
				}

				// error calculation of hidden layer
				for (std::size_t i = 0; i != Hid + 1; ++i) {
					d2[i] = 0;
					for (std::size_t j = 0; j != Out; ++j) {
						d2[i] += w2[i * Out + j] * d3[j];
					}
					d2[i] *= sprout::math::d_sigmoid(work.xi2[i]);
				}

				// weight update of hidden layer
				for (std::size_t i = 0; i != In + 1; ++i) {
					for (std::size_t j = 0; j != Hid; ++j) {
						w1[i * Hid + j] -= eta * d2[j] * work.o1[i];
					}
				}
			}
		}
	}
コード例 #4
0
			inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
			generate_uniform_real_true_2(
				Engine const& eng,
				T min_value, T max_value,
				T numerator, T divisor
				)
			{
				return SPROUT_ASSERT(divisor > 0), SPROUT_ASSERT(numerator >= 0 && numerator <= divisor),
					sprout::random::detail::generate_uniform_real_true_3(
						eng,
						min_value, max_value,
						numerator / divisor * (max_value - min_value) + min_value
						)
					;
			}
コード例 #5
0
    constexpr T gcd(const T& n, const T& m)
    {
        if (n == 0 || m == 0) {
            return 1;
        }
        T _n = std::abs(n);
        T _m = std::abs(m);
        if (_n < _m) {
            std::swap(_n, _m);
        }
        const size_t max_rep = _m;
        for (size_t i = 0; i < max_rep; i++) {
            // mは必ず小さくなるため高々m回の反復で終了する.
            int r = _n % _m;
            _n = _m;
            _m = r;
            if (_m == 0) {
                return _n;
            }
        }
        // 終了しなかった場合,エラー.
        SPROUT_ASSERT(false, "not finished.");

        return 0;
    }
コード例 #6
0
ファイル: bernoulli.hpp プロジェクト: LoliGothick/Sprout
		inline SPROUT_CONSTEXPR T bernoulli_number(std::size_t x) {
			typedef typename std::remove_cv<T>::type type;
			return SPROUT_ASSERT(x <= sprout::math::bernoulli_number_limit<type>()),
				x == 1 ? type(-1) / 2
					: x % 2 ? type(0)
					: sprout::math::detail::bernoulli_numbers<type>::table[x / 2]
				;
		}
コード例 #7
0
			inline SPROUT_CXX14_CONSTEXPR T
			generate_uniform_real(
				Engine& eng, T min_value, T max_value,
				std::true_type
				)
			{
				for(;;) {
					typedef T result_type;
					typedef typename Engine::result_type base_result;
					result_type numerator = static_cast<T>(sprout::random::detail::subtract<base_result>()(static_cast<base_result>(eng()), eng.min()));
					result_type divisor = static_cast<T>(sprout::random::detail::subtract<base_result>()(eng.max(), eng.min())) + 1;
					SPROUT_ASSERT(divisor > 0);
					SPROUT_ASSERT(numerator >= 0 && numerator <= divisor);
					result_type result = numerator / divisor * (max_value - min_value) + min_value;
					if (result < max_value) {
						return result;
					}
				}
			}
コード例 #8
0
ファイル: next.hpp プロジェクト: kundor/Sprout
		inline SPROUT_CONSTEXPR InputIterator
		next_impl_1(
			InputIterator const& it, typename std::iterator_traits<InputIterator>::difference_type n,
			std::input_iterator_tag*
			)
		{
			return SPROUT_ASSERT(sprout::math::greater_equal(n, 0)),
				n == 0 ? it
					: sprout::iterator_detail::next_impl_2(it, n)
				;
		}
コード例 #9
0
ファイル: g3.cpp プロジェクト: kundor/Sprout
	SPROUT_CXX14_CONSTEXPR std::size_t
	predict(ForwardIterator in_first, ForwardIterator in_last) const {
		SPROUT_ASSERT(sprout::distance(in_first, in_last) == In);

		worker work{};

		// prediction by forward propagation
		forward_propagation(in_first, in_last, work);

		// determining a class which output is maximum
		return sprout::distance(
			sprout::begin(work.o3),
			sprout::max_element(sprout::begin(work.o3), sprout::end(work.o3))
			);
	}
コード例 #10
0
ファイル: char_conversion.hpp プロジェクト: EzoeRyou/Sprout
		inline SPROUT_CONSTEXPR Elem
		int_to_char(IntType val) {
			return SPROUT_ASSERT(IntType(0) <= val && val < IntType(10)),
				static_cast<Elem>('0') + val
				;
		}
コード例 #11
0
ファイル: singleton.hpp プロジェクト: bolero-MURAKAMI/Sprout
		static inline SPROUT_NON_CONSTEXPR instance_type& get_mutable_instance() {
			SPROUT_ASSERT(!is_locked());
			return get_instance();
		}
コード例 #12
0
ファイル: singleton.hpp プロジェクト: bolero-MURAKAMI/Sprout
		static inline SPROUT_NON_CONSTEXPR instance_type& get_instance() {
			static detail::singleton_wrapper<instance_type> t;
			SPROUT_ASSERT(!testspr::detail::singleton_wrapper<instance_type>::m_is_destroyed);
			use(instance);
			return static_cast<instance_type&>(t);
		}