text:
#include <boost/math/constants/constants.hpp>
#include <boost/random/normal_distribution.hpp>

#include "devices/platform_list.h"
#include "documents/csv_document.h"
#include "documents/svg_document.h"
#include "fixture_register_macros.h"
#include "fixtures/damped_wave_opencl_fixture.h"
#include "fixtures/fixture_family.h"
#include "fixtures/koch_curve_opencl_fixture.h"
#include "fixtures/multibrot_opencl_fixture.h"
#include "fixtures/trivial_factorial_opencl_fixture.h"
#include "half_precision_fp.h"
#include "half_precision_normal_distribution.h"
#include "iterators/random_values_iterator.h"
#include "iterators/sequential_values_iterator.h"

#if 0
std::vector<std::shared_ptr<FixtureFamily>> CreateTrivialFixtures(
    const PlatformList& platform_list);
template <typename T, typename D = std::normal_distribution<T>>
std::vector<std::shared_ptr<FixtureFamily>> CreateDampedWave2DFixtures(
    const PlatformList& platform_list);
template <typename T, typename T4>
std::vector<std::shared_ptr<FixtureFamily>> CreateKochCurveFixtures(
    const PlatformList& platform_list);
template <typename T, typename P>
std::vector<std::shared_ptr<FixtureFamily>> FixtureRunner::CreateMultibrotSetFixtures(
    const PlatformList& platform_list);
#endif

namespace {
// TODO it is a template specialization but other places use other methods, consolidate them
// somehow?
template <typename T>
struct MultibrotSetParams {
    // static constexpr const size_t width_pix;
    // static constexpr const size_t height_pix;
};

template <>
struct MultibrotSetParams<half_float::half> {
    static constexpr const size_t width_pix = 1000;
    static constexpr const size_t height_pix = 1000;
};

template <>
struct MultibrotSetParams<float> {
    static constexpr const size_t width_pix = 2000;
    static constexpr const size_t height_pix = 2000;
};

template <>
struct MultibrotSetParams<double> {
    static constexpr const size_t width_pix = 3000;
    static constexpr const size_t height_pix = 3000;
};

template <typename P>
struct MultibrotResultConstants {
    // static constexpr const char* pixel_type_description;
};

template <>
struct MultibrotResultConstants<cl_uchar> {
    static constexpr const char* pixel_type_description = "grayscale 8 bit";
};

template <>
struct MultibrotResultConstants<cl_ushort> {
    static constexpr const char* pixel_type_description = "grayscale 16 bit";
};
}  // namespace

std::shared_ptr<FixtureFamily> CreateTrivialFactorialFixtures(
    const kpv::PlatformList& platform_list, int32_t data_size) {
    auto fixture_family = std::make_shared<FixtureFamily>();
    fixture_family->name =
        "Trivial factorial, " + Utils::FormatQuantityString(data_size) + " elements";
    fixture_family->element_count = data_size;
    for (auto& platform : platform_list.OpenClPlatforms()) {
        for (auto& device : platform->GetDevices()) {
            typedef std::uniform_int_distribution<int> Distribution;
            typedef RandomValuesIterator<int, Distribution> Iterator;
            fixture_family->fixtures.insert(
                std::make_pair<const FixtureId, std::shared_ptr<Fixture>>(
                    FixtureId(fixture_family->name, device, ""),
                    std::make_shared<TrivialFactorialOpenClFixture>(
                        std::dynamic_pointer_cast<OpenClDevice>(device),
                        std::make_shared<Iterator>(Distribution(0, 20)), data_size)));
        }
    }
    return fixture_family;
}

REGISTER_FIXTURE(
    "trivial-factorial", std::bind(&CreateTrivialFactorialFixtures, ::std::placeholders::_1, 100));
REGISTER_FIXTURE(
    "trivial-factorial", std::bind(&CreateTrivialFactorialFixtures, ::std::placeholders::_1, 1000));
REGISTER_FIXTURE(
    "trivial-factorial",
    std::bind(&CreateTrivialFactorialFixtures, ::std::placeholders::_1, 100000));
REGISTER_FIXTURE(
    "trivial-factorial",
    std::bind(&CreateTrivialFactorialFixtures, ::std::placeholders::_1, 1000000));

#if 0
template <typename T, typename D = std::normal_distribution<T>>
std::vector<std::shared_ptr<FixtureFamily>> CreateDampedWave2DFixtures(
    const PlatformList& platform_list) {
    T frequency = static_cast<T>(1.0);
    const T pi = boost::math::constants::pi<T>();

    T min = static_cast<T>(-10.0);
    T max = static_cast<T>(10.0);
    T step = static_cast<T>(0.001);
    // TODO change to int32_t?
    size_t data_size =
        static_cast<size_t>((max - min) / step);  // TODO something similar can be useful in Utils

    std::mt19937 randomValueGenerator;
    {
        std::vector<DampedWaveFixtureParameters<T>> params = {DampedWaveFixtureParameters<T>{
            static_cast<T>(1000.0), static_cast<T>(0.1), static_cast<T>(2 * pi * frequency),
            static_cast<T>(0.0), static_cast<T>(1.0)}};
        auto fixture_family = std::make_shared<FixtureFamily>();
        fixture_family->name =
            (boost::format("Damped wave, %1%, %2% values, %3% parameters, sequential input data") %
             OpenClTypeTraits<T>::short_description % Utils::FormatQuantityString(data_size) %
             Utils::FormatQuantityString(params.size()))
                .str();
        fixture_family->element_count = data_size;
        for (auto& platform : platform_list_.OpenClPlatforms()) {
            for (auto& device : platform->GetDevices()) {
                fixture_family->fixtures.insert(
                    std::make_pair<const FixtureId, std::shared_ptr<Fixture>>(
                        FixtureId(fixture_family->name, device, ""),
                        std::make_shared<DampedWaveOpenClFixture<T>>(
                            std::dynamic_pointer_cast<OpenClDevice>(device), params,
                            std::make_shared<SequentialValuesIterator<T>>(min, step), data_size,
                            fixture_family->name)));
            }
        }
        fixture_families_.push_back(fixture_family);
    }
    {
        std::vector<DampedWaveFixtureParameters<T>> params = {DampedWaveFixtureParameters<T>{
            static_cast<T>(1000.0), static_cast<T>(0.1), static_cast<T>(2 * pi * frequency),
            static_cast<T>(0.0), static_cast<T>(1.0)}};
        auto fixture_family = std::make_shared<FixtureFamily>();
        fixture_family->name =
            (boost::format("Damped wave, %1%, %2% values, %3% parameters, random input data") %
             OpenClTypeTraits<T>::short_description % Utils::FormatQuantityString(data_size) %
             Utils::FormatQuantityString(params.size()))
                .str();
        fixture_family->element_count = data_size;
        for (auto& platform : platform_list_.OpenClPlatforms()) {
            for (auto& device : platform->GetDevices()) {
                fixture_family->fixtures.insert(
                    std::make_pair<const FixtureId, std::shared_ptr<Fixture>>(
                        FixtureId(fixture_family->name, device, ""),
                        std::make_shared<DampedWaveOpenClFixture<T>>(
                            std::dynamic_pointer_cast<OpenClDevice>(device), params,
                            std::make_shared<RandomValuesIterator<T, D>>(
                                D(static_cast<T>(0.0), static_cast<T>(100.0))),
                            data_size, fixture_family->name)));
            }
        }
        fixture_families_.push_back(fixture_family);
    }
    {
        const size_t paramsCount = 1000;
        std::vector<DampedWaveFixtureParameters<T>> params;
        auto rand = std::bind(D(static_cast<T>(0.0), static_cast<T>(10.0)), randomValueGenerator);
        std::generate_n(std::back_inserter(params), paramsCount, [&rand]() {
            return DampedWaveFixtureParameters<T>(
                rand(), static_cast<T>(0.01), rand(), rand() - static_cast<T>(5.0),
                rand() - static_cast<T>(5.0));
        });
        auto fixture_family = std::make_shared<FixtureFamily>();
        fixture_family->name =
            (boost::format("Damped wave, %1%, %2% values, %3% parameters, sequential input data") %
             OpenClTypeTraits<T>::short_description % Utils::FormatQuantityString(data_size) %
             Utils::FormatQuantityString(params.size()))
                .str();
        fixture_family->element_count = data_size;
        for (auto& platform : platform_list_.OpenClPlatforms()) {
            for (auto& device : platform->GetDevices()) {
                fixture_family->fixtures.insert(
                    std::make_pair<const FixtureId, std::shared_ptr<Fixture>>(
                        FixtureId(fixture_family->name, device, ""),
                        std::make_shared<DampedWaveOpenClFixture<T>>(
                            std::dynamic_pointer_cast<OpenClDevice>(device), params,
                            std::make_shared<SequentialValuesIterator<T>>(min, step), data_size,
                            fixture_family->name)));
            }
        }
        fixture_families_.push_back(fixture_family);
    }
    {
        const size_t paramsCount = 1000;
        std::vector<DampedWaveFixtureParameters<T>> params;
        auto rand = std::bind(D(static_cast<T>(0.0), static_cast<T>(10.0)), randomValueGenerator);
        std::generate_n(std::back_inserter(params), paramsCount, [&rand]() {
            return DampedWaveFixtureParameters<T>(
                rand(), static_cast<T>(0.01), rand(), rand() - static_cast<T>(5.0),
                rand() - static_cast<T>(5.0));
        });
        auto fixture_family = std::make_shared<FixtureFamily>();
        fixture_family->name =
            (boost::format("Damped wave, %1%, %2% values, %3% parameters, random input data") %
             OpenClTypeTraits<T>::short_description % Utils::FormatQuantityString(data_size) %
             Utils::FormatQuantityString(params.size()))
                .str();
        fixture_family->element_count = data_size;
        for (auto& platform : platform_list_.OpenClPlatforms()) {
            for (auto& device : platform->GetDevices()) {
                fixture_family->fixtures.insert(
                    std::make_pair<const FixtureId, std::shared_ptr<Fixture>>(
                        FixtureId(fixture_family->name, device, ""),
                        std::make_shared<DampedWaveOpenClFixture<T>>(
                            std::dynamic_pointer_cast<OpenClDevice>(device), params,
                            std::make_shared<RandomValuesIterator<T, D>>(
                                D(static_cast<T>(0.0), static_cast<T>(100.0))),
                            data_size, fixture_family->name)));
            }
        }
        fixture_families_.push_back(fixture_family);
    }
}

template <typename T, typename T4>
std::vector<std::shared_ptr<FixtureFamily>> CreateKochCurveFixtures(
    const PlatformList& platform_list) {
    // TODO it would be great to get images with higher number of iterations but
    // another output method is needed (SVG doesn't work well)
    const std::vector<int> iteration_count_variants = {1, 3, 7};
    struct CurveVariant {
        std::vector<cl_double4> curves;
        std::string description;
    };

    std::vector<CurveVariant> curve_variants;
    {
        std::vector<cl_double4> singleCurve = {{0.0, 0.0, 1000.0, 0.0}};
        std::vector<cl_double4> twoCurvesFace2Face = {{0.0, 0.0, 1000.0, 0.0},
                                                      {1000.0, 300.0, 0.0, 300.0}};
        std::vector<cl_double4> snowflakeTriangleCurves;
        {
            cl_double2 A = {300.0, 646.41};
            cl_double2 B = {500.0, 300.0};
            cl_double2 C = {700.0, 646.41};
            snowflakeTriangleCurves = {Utils::CombineTwoDouble2Vectors(B, A),
                                       Utils::CombineTwoDouble2Vectors(C, B),
                                       Utils::CombineTwoDouble2Vectors(A, C)};
        }
        std::vector<cl_double4> snowflakeSomeFigure = {
            {300.0, 646.41, 500.0, 300.0},
            {700.0, 646.41, 300.0, 646.41},
            {500.0, 300.0, 700.0, 646.41},
        };
        std::vector<cl_double4> snowflakeSquareCurves;
        {
            cl_double2 A = {300.0, 300.0};
            cl_double2 B = {700.0, 300.0};
            cl_double2 C = {300.0, 700.0};
            cl_double2 D = {700.0, 700.0};
            snowflakeSquareCurves = {
                Utils::CombineTwoDouble2Vectors(B, A),
                Utils::CombineTwoDouble2Vectors(A, C),
                Utils::CombineTwoDouble2Vectors(D, B),
                Utils::CombineTwoDouble2Vectors(C, D),
            };
        }
        curve_variants = {{singleCurve, "single curve"},
                          {twoCurvesFace2Face, "two curves"},
                          {snowflakeTriangleCurves, "triangle"},
                          {snowflakeSquareCurves, "square"},
                          {snowflakeSomeFigure, "some figure"}};
    }

    for (int iterations : iteration_count_variants) {
        for (const auto& curve_variant : curve_variants) {
            auto fixture_family = std::make_shared<FixtureFamily>();
            fixture_family->name =
                (boost::format("Koch curve, %1%, %2% iterations, %3%") %
                 OpenClTypeTraits<T>::short_description % iterations % curve_variant.description)
                    .str();
            // TODO fixture_family->element_count can be calculated but is not trivial

            std::vector<T4> casted_curves;
            std::transform(
                curve_variant.curves.cbegin(), curve_variant.curves.cend(),
                std::back_inserter(casted_curves), [](const cl_double4& v) -> T4 {
                    return Utils::StaticCastVector4<T4, cl_double4>(v);
                });

            for (auto& platform : platform_list_.OpenClPlatforms()) {
                for (auto& device : platform->GetDevices()) {
                    fixture_family->fixtures.insert(
                        std::make_pair<const FixtureId, std::shared_ptr<Fixture>>(
                            FixtureId(fixture_family->name, device, ""),
                            std::make_shared<KochCurveOpenClFixture<T, T4>>(
                                std::dynamic_pointer_cast<OpenClDevice>(device), iterations,
                                casted_curves, 1000.0, 1000.0, fixture_family->name)));
                }
            }
            fixture_families_.push_back(fixture_family);
        }
    }
}

template <typename T, typename P>
std::vector<std::shared_ptr<FixtureFamily>> CreateMultibrotSetFixtures(
    const PlatformList& platform_list) {
    std::vector<double> powers = {1.0, 2.0, 3.0, 7.0, 3.5, 0.1, 0.5};
    std::complex<double> min{-2.5, -2.0};
    std::complex<double> max{1.5, 2.0};
    for (double power : powers) {
        auto fixture_family = std::make_shared<FixtureFamily>();
        fixture_family->name = (boost::format("%1%, %3%, %4%") %
                                ((power == 2.0) ? std::string("Mandelbrot set")
                                                : "Multibrot set, power " + std::to_string(power)) %
                                power % OpenClTypeTraits<T>::short_description %
                                MultibrotResultConstants<P>::pixel_type_description)
                                   .str();
        fixture_family->element_count =
            MultibrotSetParams<T>::width_pix * MultibrotSetParams<T>::height_pix;

        for (auto& platform : platform_list_.OpenClPlatforms()) {
            for (auto& device : platform->GetDevices()) {
                fixture_family->fixtures.insert(
                    std::make_pair<const FixtureId, std::shared_ptr<Fixture>>(
                        FixtureId(fixture_family->name, device, ""),
                        std::make_shared<MultibrotOpenClFixture<T, P>>(
                            std::dynamic_pointer_cast<OpenClDevice>(device),
                            MultibrotSetParams<T>::width_pix, MultibrotSetParams<T>::height_pix,
                            min, max, power, fixture_family->name)));
            }
        }
        fixture_families_.push_back(fixture_family);
    }
}
#endif

ext: cpp


text:
#include <fstream>
#include <boost/filesystem.hpp>
#include <gtest/gtest.h>
#include <rime/resource.h>

namespace fs = boost::filesystem;
using namespace rime;

static const ResourceType kMineralsType = ResourceType{
  "minerals",
  "not_",
  ".minerals",
};

TEST(RimeResourceResolverTest, ResolvePath) {
  ResourceResolver rr(kMineralsType);
  rr.set_root_path("/starcraft");
  auto actual = rr.ResolvePath("enough");
  fs::path expected =
      fs::system_complete(fs::current_path()).root_name().string() +
      "/starcraft/not_enough.minerals";
  EXPECT_TRUE(actual.is_absolute());
  EXPECT_TRUE(expected == actual);
}

TEST(RimeResourceResolverTest, FallbackRootPath) {
  FallbackResourceResolver rr(kMineralsType);
  rr.set_fallback_root_path("fallback");
  fs::create_directory("fallback");
  {
    fs::path nonexistent_default = "not_present.minerals";
    fs::remove(nonexistent_default);
    auto fallback = fs::absolute("fallback/not_present.minerals");
    std::ofstream(fallback.string()).close();
    auto actual = rr.ResolvePath("present");
    EXPECT_TRUE(fallback == actual);
    fs::remove(fallback);
  }
  {
    auto existent_default = fs::absolute("not_falling_back.minerals");
    std::ofstream(existent_default.string()).close();
    auto actual = rr.ResolvePath("falling_back");
    EXPECT_TRUE(existent_default == actual);
    fs::remove(existent_default);
  }
  {
    auto nonexistent_default = fs::absolute("not_any.minerals");
    fs::remove(nonexistent_default);
    auto nonexistent_fallback = fs::absolute("fallback/not_any.minerals");
    fs::remove(nonexistent_fallback);
    auto actual = rr.ResolvePath("any");
    EXPECT_TRUE(nonexistent_default == actual);
  }
  fs::remove_all("fallback");
}

ext: cc

max_stars_repo_name: mengzhisuoliu/librime

max_issues_repo_name: mengzhisuoliu/librime

max_forks_repo_name: mengzhisuoliu/librime


text:
#include "super_frame_mux.h"

#include <boost/smart_ptr/make_shared.hpp>

#include "av_decoder.h"
#include "buffer_utils.h"
#include "vqm_state.h"
#include "x264_encoder.h"

using namespace lmt::statistics;

namespace lmt
{
namespace mux
{
void SuperFrameMux::init(uint8_t numberOfIndividualFrames,
                         std::unique_ptr<encoder::VideoEncoder> encoder,
                         std::unique_ptr<decoder::VideoDecoder> decoder) noexcept
{
    superFrameBuffer_ = SuperFrameBuffer(numberOfIndividualFrames);
    frameStatsBufferList_ = FrameStatsBufferList(numberOfIndividualFrames);

    encoder_ = std::move(encoder);
    decoder_ = std::move(decoder);
}

void SuperFrameMux::addImageSegment(int index, const cv_bridge::CvImageConstPtr& img, const FrameStats& stats)
{
    superFrameBuffer_[index].push_back(img->image);
    frameStatsBufferList_[index].push_back(stats);

    if (std::none_of(superFrameBuffer_.begin(), superFrameBuffer_.end(), [](const FrameBuffer& segmentBuffer) {
            return segmentBuffer.empty();
        }))
    {
        const auto allIndividualRawImages = getFirstOfEveryBuffer(superFrameBuffer_);
        const auto superFrame = moveFramesIntoSuperFrame(allIndividualRawImages);
        vqmState_->update(superFrame);
        const auto superFramePtr =
            boost::make_shared<cv_bridge::CvImage>(std_msgs::Header(), img->encoding, superFrame);

        // NOLINTNEXTLINE(modernize-avoid-c-arrays)
        auto encodedData = std::make_unique<uint8_t[]>(img->image.elemSize() * img->image.total());
        auto superFrameStats = encoder_->encode(superFramePtr, encodedData);
        superFrameStats.temporalActivity = vqmState_->getTA();
        superFrameStats.spatialActivity = vqmState_->getSA();

        const auto decodedSuperFrame = decoder_->decode(encodedData, superFrameStats.encodedFrameSize);

        auto allFrameStats = getFirstOfEveryBuffer(frameStatsBufferList_);
        allFrameStats.push_back(superFrameStats);

        superFrameCallback_(allFrameStats, decodedSuperFrame, allIndividualRawImages);

        popFirstOfEveryBuffer(frameStatsBufferList_);
        popFirstOfEveryBuffer(superFrameBuffer_);
    }
}

SuperFrameMux::SuperFrameMux(SuperFrameMux::SuperFrameCallback superFrameCallback,
                             std::unique_ptr<quality::VQMState> vqmState)
    : superFrameCallback_(std::move(superFrameCallback)), vqmState_(std::move(vqmState))
{
}

cv::Mat moveFramesIntoSuperFrame(const std::vector<cv::Mat>& individualRawImages)
{
    auto it = individualRawImages.begin();
    cv::Mat superFrame = *it;

    ++it;
    for (; it != individualRawImages.end(); ++it)
    {
        addSegmentToSuperFrame(superFrame, *it);
    }

    return superFrame;
}

void addSegmentToSuperFrame(cv::Mat& superFrame, const cv::Mat& segment)
{
    cv::Mat tmpSegment = segment;
    if (segment.rows < superFrame.rows)
    {
        tmpSegment = cv::Mat(superFrame.rows, segment.cols, superFrame.type(), cv::Scalar::all(0));
        segment.copyTo(tmpSegment(cv::Rect(0, 0, segment.cols, segment.rows)));
    }
    cv::hconcat(superFrame, tmpSegment, superFrame);
}

}  // namespace mux
}  // namespace lmt

max_stars_repo_name: hofbi/amvs-se

max_issues_repo_name: hofbi/amvs-se

max_forks_repo_name: hofbi/amvs-se


text: #include <boost/geometry/algorithms/expand.hpp>

ext: hpp

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#pragma once

#include "qi_includes.hpp"
#include "phoenix_includes.hpp"
#include "fusion_includes.hpp"

#include <boost/fusion/include/adapt_struct.hpp>
#include <cstdint>
#include <string>

namespace TwistedSpirit
{
    namespace qi = boost::spirit::qi;
    namespace encoding = boost::spirit::ascii;
    namespace phoenix = boost::phoenix;
    namespace spirit = boost::spirit;
    namespace fusion = boost::fusion;

    using char_type = char;
    using const_iterator_type = std::basic_string <char_type>::const_iterator;

	struct empty {};

    namespace common_usings
    {
        // qi
        using qi::lit;
        using qi::lexeme;
        using qi::eps;
        using qi::on_error;
        using qi::accept;
        using qi::fail;
        using qi::repeat;
        using qi::int_;
        using qi::double_;
        using qi::digit;
		using qi::xdigit;
        using qi::long_double;
        using qi::long_long;
        using qi::ulong_long;
        using namespace qi::labels;

        // phoenix
        using phoenix::while_;
        using phoenix::if_;
        using phoenix::at_c;
        using phoenix::push_back;
		using phoenix::ref;
		using phoenix::cref;

        // encoding dependent
        namespace ascii
        {
            namespace b_ascii = boost::spirit::standard;
            using b_ascii::char_;
            using b_ascii::string;
        }
        namespace wide
        {
            namespace b_wide = boost::spirit::standard_wide;
            using b_wide::char_;
            using b_wide::string;
        }
    }

    namespace qi_error
    {
        struct handler_base {
            template <typename, typename, typename, typename, typename>
                struct result { typedef void type; };

            virtual ~handler_base() {}
        };

        struct error_handler_base : public handler_base {};

        struct warning_handler_base : public handler_base {};

        struct error_handler_cerr : public error_handler_base {
            template <typename Iterator, typename WhatType>
            void operator()(Iterator begin, Iterator end, Iterator where, WhatType what, ...) const
            {
                std::cerr   << "Error in for loop parser - expecting: "
                            << what
                            << " at: "
                            << where - begin
                            << " in expression: \""
                            << std::string (begin, where)
                            << "\""
                            << " from end: "
                            << end - where
                            << "\n"
                ;
            }
        };

        struct error_handler_silent : public error_handler_base {
            template <typename Iterator, typename WhatType>
            void operator()(Iterator, Iterator, Iterator, WhatType, ...) const
            {
                // ... ignore
            }
        };

        struct warning_handler_cout : public warning_handler_base {
            template <typename Iterator, typename WhatType>
            void operator()(Iterator begin, Iterator end, Iterator where, WhatType what, ...) const
            {
                std::cout   << "warning: "
                            << what
                            << " at: "
                            << where - begin
                            << " in expression: \""
                            << std::string (begin, where)
                            << "\""
                            << " from end: "
                            << end - where
                            << "\n"
                ;
            }
        };

        struct warning_handler_silent : public warning_handler_base {
            template <typename, typename>
            void operator()(...) const
            {
                // ... ignore
            }
        };

        struct debug_streamer {
            template <typename>
            struct result { typedef void type; };

            template <typename T>
            void operator()(T const& str) const {
                std::cout << str << "\n";
            }
        };
    }

} // namespace TwistedSpirit

#define GRAMMAR_TEMPLATE_SIGNATURE <typename ErrorHandler, typename WarningHandler, typename Iterator>
#define GRAMMAR_TEMPLATE_SIGNATURE_FORWARD <ErrorHandler, WarningHandler, Iterator>

#define HANDLE_QI_ERROR(rule, id)							\
on_error<fail>												\
(															\
    rule,													\
    error_handler(qi::_1, qi::_2, qi::_3, qi::_4, id)		\
)
// MAKRO END

#define HANDLE_QI_WARNING(rule, id)							\
on_error<accept>											\
(															\
	rule,													\
	warning_handler(qi::_1, qi::_2, qi::_3, qi::_4, id)		\
)
// MAKRO END

#define INSTALL_ERROR_HANDLER								\
const phoenix::function <ErrorHandler> error_handler
// MAKRO END

#define INSTALL_WARNING_HANDLER								\
const phoenix::function <WarningHandler> warning_handler
// MAKRO END

#define INSTALL_DEBUG_HANDLER								\
const phoenix::function <qi_error::debug_streamer> dout
// MAKRO END


text:
// The MIT License (MIT)

// Copyright (c) 2019 Caili Li

//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.

/* code */

#include <cmath>

#include <Eigen/LU>

#include "Transform.hpp"

namespace roboflow
{
namespace navigation_function
{
namespace math
{

Transform::Transform(Eigen::Vector2d s, double r, Eigen::Vector2d t)
{
    scale_ = s;
    rotate_ = r;
    translate_ = t;

    construct_affine();
}

Transform::Transform(double sx, double sy, double r, double x, double y)
    : Transform({sx, sy}, r, {x, y}) {}

Transform::Transform(const Transform &other)
{
    scale_ = other.scale_;
    rotate_ = other.rotate_;
    translate_ = other.translate_;
    affine_ = other.affine_;
}

void Transform::operator=(const Transform &other)
{
    scale_ = other.scale_;
    rotate_ = other.rotate_;
    translate_ = other.translate_;
    affine_ = other.affine_;
}

Transform::Transform(const Transform &&other)
{
    scale_ = std::move(other.scale_);
    rotate_ = std::move(other.rotate_);
    translate_ = std::move(other.translate_);
    affine_ = std::move(other.affine_);
}

void Transform::operator=(const Transform &&other)
{
    scale_ = std::move(other.scale_);
    rotate_ = std::move(other.rotate_);
    translate_ = std::move(other.translate_);
    affine_ = std::move(other.affine_);
}

void Transform::construct_affine()
{
    Eigen::Matrix2d S = (Eigen::Matrix2d() << scale_(0), 0, 0, scale_(1)).finished();
    Eigen::Matrix2d R = (Eigen::Matrix2d() << std::cos(rotate_), -std::sin(rotate_),
                         std::sin(rotate_), std::cos(rotate_))
                            .finished();
    affine_ = R * S;
}

Eigen::Vector2d Transform::operator()(Eigen::Vector2d p) const
{
    return transformFrom(p);
}

Eigen::Vector2d Transform::transformFrom(Eigen::Vector2d p) const
{
    return affine_.inverse() * (p - translate_);
}

Eigen::Vector2d Transform::transformTo(Eigen::Vector2d p) const
{
    return affine_ * p + translate_;
}

} // namespace math
} // namespace navigation_function
} // namespace roboflow


text:
/*
// Copyright (c) 2000-2009, Texas Engineering Experiment Station (TEES), a
// component of the Texas A&M University System.

// All rights reserved.

// The information and source code contained herein is the exclusive
// property of TEES and may not be disclosed, examined or reproduced
// in whole or in part without explicit written authorization from TEES.
*/

#ifndef STAPL_CONTAINERS_GID_OF_FWD_HPP
#define STAPL_CONTAINERS_GID_OF_FWD_HPP

#include <boost/version.hpp>

namespace stapl{

template<typename Iter>
typename
  boost::unordered::iterator_detail::iterator<Iter>::value_type::first_type
gid_of(boost::unordered::iterator_detail::iterator<Iter> it);

template<typename Iter, typename Iter2>
typename
#if BOOST_VERSION < 105800
  boost::unordered::iterator_detail::c_iterator<Iter, Iter2>::value_type
gid_of(boost::unordered::iterator_detail::c_iterator<Iter, Iter2> it);
#else
  boost::unordered::iterator_detail::c_iterator<Iter>::value_type
gid_of(boost::unordered::iterator_detail::c_iterator<Iter> it);
#endif

} // namespace stapl

#endif // STAPL_CONTAINERS_GID_OF_FWD_HPP


text:
#include <chrono>

#include <laser_slam_ros/GetLaserTrackSrv.h>
// #include <octomap_world/octomap_manager.h>
#include <ros/ros.h>
#include <ros/package.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl_ros/transforms.h>
#include <tf2_eigen/tf2_eigen.h>
#include <glog/logging.h>
#include <boost/filesystem.hpp>

#include <laser_slam/common.hpp>
#include <laser_slam_ros/common.hpp>
#include <segmatch/point_color_semantics.hpp>

int main(int argc, char** argv) {
  // Initialize glog for volumetric_mapping.
  google::InitGoogleLogging(argv[0]);
  // Get output file path.
  if (argc < 2) {
    ROS_ERROR("No output file path specified");
    return -1;
  }

  // Defaults.
  double resolution = 0.075;
  double prob_hit = 0.9;
  double prob_miss = 0.4;
  double max_range = 20.0;
  bool publish = true;

  boost::posix_time::ptime my_posix_time = ros::WallTime::now().toBoost();
  std::string current_time = boost::posix_time::to_iso_extended_string(my_posix_time);
  std::string pose_log_file_name =
    "pose_offset_log_" + current_time + ".txt";
  ROS_INFO("Writing pose offsets to %s", pose_log_file_name.c_str());
  std::ofstream pose_log_file(pose_log_file_name);
  int pose_counter = 0;

  std::string file_path(argv[1]);
  file_path = file_path.substr(0, file_path.length() - 4) + current_time + ".pcd";

  // Parse arguments.
  if (argc > 2) {
    if (argc % 2) {
      ROS_ERROR("Invalid number of command line arguments");
      return -1;
    }
    for (int i = 2; i < argc; i+=2) {
      const std::string argument(argv[i]);
      if (argument == "resolution") resolution = std::atof(argv[i+1]);
      else if (argument == "probability_hit") prob_hit = std::atof(argv[i+1]);
      else if (argument == "probability_miss") prob_miss = std::atof(argv[i+1]);
      else if (argument == "sensor_max_range") max_range = std::atof(argv[i+1]);
      else {
        ROS_ERROR("Invalid command line argument \"%s\"", argv[i]);
        return -1;
      }
    }

  }
  ROS_INFO("Output file path is: \"%s\"", file_path.c_str());
  ROS_INFO("Octomap resolution is %fm", resolution);
  ROS_INFO("Probability hit/miss are: %f / %f", prob_hit, prob_miss);
  ROS_INFO("Sensor max range is %fm", max_range);

  // Init ROS things.
  ros::init(argc, argv, "laser_to_octomap");
  ros::NodeHandle nh;
  ros::NodeHandle nh_private("~");

  // Get laser track.
  ros::ServiceClient client;
  client = nh.serviceClient<laser_slam_ros::GetLaserTrackSrv>("/segmapper/get_laser_track");
  laser_slam_ros::GetLaserTrackSrv call;
  ROS_INFO("Requested laser track. Waiting...");
  if (!client.call(call)) {
    ROS_ERROR("Client call failed");
    return -1;
  }
  const size_t traj_length = call.response.laser_scans.size();
  ROS_INFO("Received laser track of length %lu", traj_length);
  ROS_INFO("You can shut down laser_mapper");

  // Set parameters.
  nh_private.setParam("resolution", resolution);
  nh_private.setParam("probability_hit", prob_hit);
  nh_private.setParam("probability_miss", prob_miss);
  nh_private.setParam("sensor_max_range", max_range);
  nh_private.setParam("use_tf_transforms", false);
  nh_private.setParam("robot_frame", call.response.transforms.front().header.frame_id);
  nh_private.setParam("world_frame", call.response.transforms.front().child_frame_id);

  // Volumetric Mapping expects ConstPtr.
  // Make copies so there is no double free when ConstPtr falls out of scope.
  std::vector<sensor_msgs::PointCloud2::ConstPtr> ptr_vec;
  for (auto&& scan : call.response.laser_scans) {
    ptr_vec.emplace_back(new sensor_msgs::PointCloud2(scan));
    // for (int i = 0; i <  scan.fields[i].count; i++) {
    // for (int i = 0; i <  8; i++) {
    //   std::cout << "f: " << scan.fields[i].name << " and offset: " << scan.fields[i].offset << std::endl;
    // }
    // std::cout << scan << std::endl;
    // std::cout << *ptr_vec[0] << std::endl;
    // exit(0);
  }
  std::cout << "data: ";
  for (int i = 0; i < 80; ++i) {
    std::cout << std::to_string(static_cast<uint32_t>(ptr_vec[0]->data[i])) << ", ";
  }
  std::cout << std::endl;

  // accumulate all points in one point cloud
  // pcl::PointCloud<pcl::PointXYZRGB> pclCloud, tempCloud;
  pcl::PointCloud<segmatch::PointColorSemantics> pclCloud, tempCloud;
  std::string timestamp;
  for (size_t i = 0u; i < traj_length; ++i) {
    // pcl::fromROSMsg(*ptr_vec[i], tempCloud);
    // std::cout << "First point fromROSMsg: " << tempCloud[10].semantics_rgb << std::endl;
    // for (int j = 0; j < 10; ++j) {
    //   std::cout << tempCloud[j].x << ", " << tempCloud[j].y << ", " << tempCloud[j].rgb << ", " << tempCloud[j].semantics_rgb << ", " << std::endl;
    // }
    laser_slam_ros::convert_to_pcl_point_cloud(*ptr_vec[i], &tempCloud);
    std::cout << "First point convert_to_pcl_point_cloud: " << tempCloud[0].semantics_rgb << std::endl;
    for (int j = 0; j < 10; ++j) {
      std::cout << tempCloud[j].x << ", " << tempCloud[j].y << ", "
                << std::to_string(tempCloud[j].r) << ", " << std::to_string(tempCloud[j].g) << ", " << std::to_string(tempCloud[j].b)
                // << ", " << std::to_string(tempCloud[j].semantics_r) << ", " << std::to_string(tempCloud[j].semantics_g) << ", " << std::to_string(tempCloud[j].semantics_b)
                << ", " << tempCloud[j].semantics_rgb << std::endl;
    }
    // apply tf to point cloud
    Eigen::Affine3d tf = tf2::transformToEigen(call.response.transforms[i]);
    pcl::transformPointCloud(tempCloud, tempCloud, tf);
    pclCloud += tempCloud;

    // write poses (or translations) associated with each scan to log file
    ROS_INFO("current translation:\n%f\n%f\n%f",
             call.response.transforms[i].transform.translation.x,
             call.response.transforms[i].transform.translation.y,
             call.response.transforms[i].transform.translation.z);
    // boost::posix_time::ptime timestamp = call.response.transforms[i].header.stamp.toBoost();
    timestamp = boost::posix_time::to_iso_extended_string(call.response.transforms[i].header.stamp.toBoost());
    double time_ns = call.response.transforms[i].header.stamp.sec * 1e9 + call.response.transforms[i].header.stamp.nsec;
    ROS_INFO("current translation timestamp: %s or %f and frame seq: %d\n",
              timestamp.c_str(),
              time_ns,
             call.response.transforms[i].header.seq);
    pose_log_file << i << "\n"
                  << std::fixed /* << std::setprecision(3) */ << time_ns << "\n"
                  << call.response.transforms[i].transform.translation.x << "\n"
                  << call.response.transforms[i].transform.translation.y << "\n"
                  << call.response.transforms[i].transform.translation.z << "\n\n";
    pose_counter++;
  }
  pcl::io::savePCDFileASCII (file_path, pclCloud);

  // save current config file
boost::filesystem::path config_file_path = ros::package::getPath("segmapper") + "/launch/kitti/kitti_loop_closure.yaml";
boost::filesystem::copy_file(config_file_path,
                                           "kitti_loop_closure_" + current_time + ".yaml",
                                         boost::filesystem::copy_option::overwrite_if_exists);
  ROS_INFO("Copying config file %s", config_file_path.string().c_str());
  ROS_INFO("Saved pcd file to %s.", file_path.c_str());
}


text:
/* boost random/uniform_int.hpp header file
 *
 * Copyright Jens Maurer 2000-2001
 * Permission to use, copy, modify, sell, and distribute this software
 * is hereby granted without fee provided that the above copyright notice
 * appears in all copies and that both that copyright notice and this
 * permission notice appear in supporting documentation,
 *
 * Jens Maurer makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * See http://www.boost.org for most recent version including documentation.
 *
 * $Id: uniform_int.hpp,v 1.21.2.1 2003/03/17 01:39:15 beman_dawes Exp $
 *
 * Revision history
 *  2001-04-08  added min<max assertion (N. Becker)
 *  2001-02-18  moved to individual header files
 */

#ifndef BOOST_RANDOM_UNIFORM_INT_HPP
#define BOOST_RANDOM_UNIFORM_INT_HPP

#include <cassert>
#include <boost/config.hpp>
#include <boost/limits.hpp>
#include <boost/static_assert.hpp>
#include <boost/random/uniform_smallint.hpp>
#include <boost/random/detail/signed_unsigned_compare.hpp>
#ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
#include <boost/type_traits/is_float.hpp>
#endif

namespace boost {

// uniform integer distribution on [min, max]

namespace detail {

template<class UniformRandomNumberGenerator, class IntType>
class uniform_int_integer
{
public:
  typedef UniformRandomNumberGenerator base_type;
  typedef IntType result_type;

  uniform_int_integer(base_type & rng, IntType min, IntType max)
    : _rng(&rng), _bmin(_rng->min()), _brange(_rng->max() - _bmin)
  {
    assert(min < max);
    set(min, max);
  }

  void set(result_type min, result_type max)
  {
    _min = min;
    _max = max;
    _range = _max - _min;

    if(random::equal_signed_unsigned(_brange, _range))
      _range_comparison = 0;
    else if(random::lessthan_signed_unsigned(_brange, _range))
      _range_comparison = -1;
    else
      _range_comparison = 1;
  }

  result_type min() const { return _min; }
  result_type max() const { return _max; }
  base_type& base() const { return *_rng; }

  result_type operator()();

private:
  typedef typename base_type::result_type base_result;
  base_type * _rng;
  result_type _min, _max, _range;
  base_result _bmin, _brange;
  int _range_comparison;
};

template<class UniformRandomNumberGenerator, class IntType>
inline IntType uniform_int_integer<UniformRandomNumberGenerator, IntType>::operator()()
{
  if(_range_comparison == 0) {
    // this will probably never happen in real life
    // basically nothing to do; just take care we don't overflow / underflow
    return static_cast<result_type>((*_rng)() - _bmin) + _min;
  } else if(_range_comparison < 0) {
    // use rejection method to handle things like 0..3 --> 0..4
    for(;;) {
      // concatenate several invocations of the base RNG
      // take extra care to avoid overflows
      result_type limit;
      if(_range == std::numeric_limits<result_type>::max()) {
        limit = _range/(result_type(_brange)+1);
        if(_range % result_type(_brange)+1 == result_type(_brange))
          ++limit;
      } else {
        limit = (_range+1)/(result_type(_brange)+1);
      }
      // we consider "result" as expressed to base (_brange+1)
      // for every power of (_brange+1), we determine a random factor
      result_type result = result_type(0);
      result_type mult = result_type(1);
      while(mult <= limit) {
        result += ((*_rng)() - _bmin) * mult;
        mult *= result_type(_brange)+result_type(1);
      }
      if(mult == limit)
        // _range+1 is an integer power of _brange+1: no rejections required
        return result;
      // _range/mult < _brange+1  -> no endless loop
      result += uniform_int_integer<base_type,result_type>(*_rng, 0, _range/mult)() * mult;
      if(result <= _range)
        return result + _min;
    }
  } else {                   // brange > range
    if(_brange / _range > 4 /* quantization_cutoff */ ) {
      // the new range is vastly smaller than the source range,
      // so quantization effects are not relevant
      return boost::uniform_smallint<base_type,result_type>(*_rng, _min, _max)();
    } else {
      // use rejection method to handle things like 0..5 -> 0..4
      for(;;) {
        base_result result = (*_rng)() - _bmin;
        // result and range are non-negative, and result is possibly larger
        // than range, so the cast is safe
        if(result <= static_cast<base_result>(_range))
          return result + _min;
      }
    }
  }
}

template<class UniformRandomNumberGenerator, class IntType>
class uniform_int_float
{
public:
  typedef UniformRandomNumberGenerator base_type;
  typedef IntType result_type;

  uniform_int_float(base_type & rng, IntType min, IntType max)
    : _rng(rng)
  {
    set(min,max);
  }

  void set(result_type min, result_type max)
  {
    _min = min;
    _max = max;
    _range = static_cast<base_result>(_max-_min)+1;
  }

  result_type min() const { return _min; }
  result_type max() const { return _max; }
  base_type& base() const { return _rng.base(); }

  result_type operator()()
  {
    return static_cast<IntType>(_rng() * _range) + _min;
  }

private:
  typedef typename base_type::result_type base_result;
  uniform_01<base_type> _rng;
  result_type _min, _max;
  base_result _range;
};

// simulate partial specialization
template<bool is_integer>
struct uniform_int;

template<>
struct uniform_int<true>
{
  template<class UniformRandomNumberGenerator, class IntType>
  struct impl
  {
    typedef uniform_int_integer<UniformRandomNumberGenerator, IntType> type
;
  };
};

template<>
struct uniform_int<false>
{
  template<class UniformRandomNumberGenerator, class IntType>
  struct impl
  {
    typedef uniform_int_float<UniformRandomNumberGenerator, IntType> type;
  };
};

} // namespace detail

template<class UniformRandomNumberGenerator, class IntType = int>
class uniform_int
{
private:
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  typedef typename detail::uniform_int<std::numeric_limits<typename UniformRandomNumberGenerator::result_type>::is_integer>::BOOST_NESTED_TEMPLATE impl<UniformRandomNumberGenerator, IntType>::type impl_type;
#elif BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x0570) )
  typedef typename detail::uniform_int< boost::is_float<typename UniformRandomNumberGenerator::result_type>::value == false >::BOOST_NESTED_TEMPLATE impl<UniformRandomNumberGenerator, IntType>::type impl_type;
#else
  BOOST_STATIC_CONSTANT(bool, base_float = (boost::is_float<typename UniformRandomNumberGenerator::result_type>::value == false));
  typedef typename detail::uniform_int<base_float>::BOOST_NESTED_TEMPLATE impl<UniformRandomNumberGenerator, IntType>::type impl_type;
#endif

public:
  typedef uniform_int<UniformRandomNumberGenerator, IntType> adaptor_type;
  typedef UniformRandomNumberGenerator base_type;
  typedef IntType result_type;

  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);

  explicit uniform_int(base_type & rng, IntType min = 0, IntType max = 9)
    : impl(rng, min, max)
  {
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
    // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
    BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
#endif
  }

  result_type min() const { return impl.min(); }
  result_type max() const { return impl.max(); }
  adaptor_type& adaptor() { return *this; }
  base_type& base() const { return impl.base(); }
  void reset() { }

  result_type operator()() { return impl(); }

#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
  friend bool operator==(const uniform_int& x, const uniform_int& y)
  { return x.min() == y.min() && x.max() == y.max() && x.base() == y.base(); }

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  template<class CharT, class Traits>
  friend std::basic_ostream<CharT,Traits>&
  operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_int& ud)
  {
    os << ud.min() << " " << ud.max();
    return os;
  }

  template<class CharT, class Traits>
  friend std::basic_istream<CharT,Traits>&
  operator>>(std::basic_istream<CharT,Traits>& is, uniform_int& ud)
  {
# if BOOST_WORKAROUND(_MSC_FULL_VER, BOOST_TESTED_AT(13102292)) && BOOST_MSVC > 1300
      return detail::extract_uniform_int(is, ud, ud.impl);
# else
    IntType min, max;
    is >> std::ws >> min >> std::ws >> max;
    ud.impl.set(min, max);
    return is;
# endif
  }
#endif

#else
  // Use a member function
  bool operator==(const uniform_int& rhs) const
  { return min() == rhs.min() && max() == rhs.max() && base() == rhs.base();  }
#endif
private:
  impl_type impl;
};

#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
//  A definition is required even for integral static constants
template<class UniformRandomNumberGenerator, class IntType>
const bool uniform_int<UniformRandomNumberGenerator, IntType>::has_fixed_range;
#endif

} // namespace boost

#endif // BOOST_RANDOM_UNIFORM_INT_HPP


text:
/* Copyright (C) 2017 IBM Corp.
 *  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.
 */
/***********************************************************************
CRTmatrix: implements A matrix in CRT representation
************************************************************************/

#include <vector>
#include <cassert>
#include <cstdlib>
//#include <mpfr.h>
#include <NTL/mat_lzz_p.h>
#include <NTL/BasicThreadPool.h>
#include <NTL/ZZ.h>
#include <NTL/FFT.h>
#include <NTL/SmartPtr.h>

NTL_CLIENT
#include "utils/tools.h"
#include "DGaussSampler.h"
#include "CRTmatrix.h"
#include "TDMatrixParams.h"

//#define DEBUGPRINT

CRTmatrix& CRTmatrix::leftMultBy(const CRTmatrix& M) // A := M*A
{
    FHE_TIMER_START;
    assert(length()==params->kFactors);
    NTL::zz_pPush ppush; // backup NTL's current modulus

    // multi-threaded implementation, threads handle different moduli

    EXEC_RANGE(length(), first, last)
    //EXEC_RANGE(length(), first, last)
    for (long i=first; i<last; i++)
    {
        params->zzp_context[i].restore();
        mat_zz_p& matMod = (*this)[i];
        const mat_zz_p& Mmod = M[i];
        mul(matMod, Mmod, matMod);
    }
    //EXEC_RANGE_END
    EXEC_RANGE_END
    return *this;
}

CRTmatrix& CRTmatrix::leftMultBy(const mat_l& M) // A := M*A
{
    FHE_TIMER_START;
    assert(length()==params->kFactors);
    NTL::zz_pPush ppush; // backup NTL's current modulus

    // multi-threaded implementation, threads handle different moduli
    EXEC_RANGE(length(), first, last)
    for (long i=first; i<last; i++)
    {
        params->zzp_context[i].restore();
        mat_zz_p& matMod = (*this)[i];
        mat_zz_p Mmod = conv<mat_zz_p>(M);
        mul(matMod, Mmod, matMod);
    }
    EXEC_RANGE_END

    return *this;
}

CRTmatrix& CRTmatrix::leftMultBy(const vec_l& u) // A := u*A
{
    FHE_TIMER_START;
    assert(length()==params->kFactors);
    NTL::zz_pPush ppush; // backup NTL's current modulus

    // multi-threaded implementation, threads handle different moduli
    EXEC_RANGE(length(), first, last)

    for (long i=first; i<last; i++)
    {
        params->zzp_context[i].restore();
        mat_zz_p& matMod = (*this)[i]; // A
        const vec_zz_p umod = conv<vec_zz_p>(u);
        vec_zz_p vTmp;
        mul(vTmp, umod, matMod); // tmp = u*A

        // copy vTmp back to matMod
        matMod.SetDims(1, vTmp.length());
        matMod[0] = vTmp;
    }
    EXEC_RANGE_END

    return *this;
}

// Convert Mat<long> to CRT format, we assume that params are already set
CRTmatrix& CRTmatrix::operator=(const mat_l& M) // A := M
{
    FHE_TIMER_START;
    assert(params != NULL);

    NTL::zz_pPush ppush; // backup NTL's current modulus
    SetLength(params->kFactors);
    for (long i=0; i<length(); i++)
    {
        params->zzp_context[i].restore();
        conv((*this)[i], M);
    }

    return *this;
}

// Default assignment should work: NTL now guarantees
// that assignment and default constructors work, even
// "out of context"
CRTmatrix& CRTmatrix::operator=(const CRTmatrix& other) // A := M
{
    FHE_TIMER_START;
    params = other.params;
    NTL::zz_pPush ppush; // backup NTL's current modulus
    SetLength(other.length());
    for (long i=0; i<params->kFactors; i++)
    {
        params->zzp_context[i].restore();
        (*this)[i] = other[i];
    }
    return *this;
}

CRTmatrix& CRTmatrix::operator*=(const mat_l& M) // A := M*A
{
    FHE_TIMER_START;
    assert(length()==params->kFactors);
    NTL::zz_pPush ppush; // backup NTL's current modulus

    // multi-threaded implementation, threads handle different moduli
    EXEC_RANGE(length(), first, last)
    mat_zz_p Mmod;
    for (long i=first; i<last; i++)
    {
        params->zzp_context[i].restore();
        mat_zz_p& Amod = (*this)[i];
        Mmod = conv<mat_zz_p>(M);
        mul(Amod, Amod, Mmod);
    }
    EXEC_RANGE_END

    return *this;
}

CRTmatrix& CRTmatrix::operator*=(const CRTmatrix& M) // A := A*M
{
    FHE_TIMER_START;
    assert(length()==params->kFactors
           && M.length()==M.params->kFactors
           && params==M.params);
    NTL::zz_pPush ppush; // dibackup NTL's current modulus

    // multi-threaded implementation, threads handle different moduli
    EXEC_RANGE(length(), first, last)
    for (long i=first; i<last; i++)
    {
        params->zzp_context[i].restore();
        mat_zz_p& Amod = (*this)[i];
        const mat_zz_p& Mmod = M[i];
        mul(Amod, Amod, Mmod);
    }
    EXEC_RANGE_END

    return *this;
}

CRTmatrix& CRTmatrix::operator+=(const mat_l& M) // A := A+M
{
    FHE_TIMER_START;
    assert(length()==params->kFactors);
    NTL::zz_pPush ppush; // backup NTL's current modulus

    // multi-threaded implementation, threads handle different moduli
    EXEC_RANGE(length(), first, last)
    //for (long i=0; i<length(); i++)
    for (long i=first; i<last; i++)
    {
        mat_zz_p& matMod = (*this)[i];
        assert(matMod.NumRows()==M.NumRows() && matMod.NumCols()==M.NumCols());

        params->zzp_context[i].restore();
        for (long j=0; j<matMod.NumRows(); j++)
            for (long k=0; k<matMod.NumCols(); k++)
                matMod[j][k] += M[j][k];
    }
    EXEC_RANGE_END
    return *this;
}
CRTmatrix& CRTmatrix::operator-=(const mat_l& M) // A := A+M
{
    FHE_TIMER_START;
    assert(length()==params->kFactors);
    NTL::zz_pPush ppush; // backup NTL's current modulus

    EXEC_RANGE(length(), first, last)
    //for (long i=0; i<length(); i++)
    for (long i=first; i<last; i++)
    {
        mat_zz_p& matMod = (*this)[i];
        assert(matMod.NumRows()==M.NumRows() && matMod.NumCols()==M.NumCols());

        params->zzp_context[i].restore();
        for (long j=0; j<matMod.NumRows(); j++)
            for (long k=0; k<matMod.NumCols(); k++)
                matMod[j][k] -= M[j][k];
    }
    EXEC_RANGE_END
    return *this;
}

CRTmatrix& CRTmatrix::operator+=(const CRTmatrix& M) // A := A+M
{
    FHE_TIMER_START;
    assert(length()==params->kFactors
           && M.length()==M.params->kFactors
           && params==M.params);
    NTL::zz_pPush ppush; // backup NTL's current modulus

    EXEC_RANGE(length(), first, last)
    //for (long i=0; i<length(); i++)
    for (long i=first; i<last; i++)
    {
        mat_zz_p& Amod = (*this)[i];
        const mat_zz_p& Mmod = M[i];

        assert(Amod.NumRows()==Mmod.NumRows() && Amod.NumCols()==Mmod.NumCols());
        params->zzp_context[i].restore();
        Amod += Mmod;
    }
    EXEC_RANGE_END
    return *this;
}

CRTmatrix& CRTmatrix::operator-=(const CRTmatrix& M) // A := A+M
{
    FHE_TIMER_START;
    assert(length()==params->kFactors
           && M.length()==M.params->kFactors
           && params==M.params);
    NTL::zz_pPush ppush; // backup NTL's current modulus

    EXEC_RANGE(length(), first, last)
    //for (long i=0; i<length(); i++)
    for (long i=first; i<last; i++)
    {
        mat_zz_p& Amod = (*this)[i];
        const mat_zz_p& Mmod = M[i];

        assert(Amod.NumRows()==Mmod.NumRows() && Amod.NumCols()==Mmod.NumCols());
        params->zzp_context[i].restore();
        Amod -= Mmod;
    }
    EXEC_RANGE_END
    return *this;
}

// Get a CRT column: crtCol[i] is the j'th column of this[i]
void CRTmatrix::getColumn(Vec<vec_zz_p>& crtCol, long j)
{
    FHE_TIMER_START;
    crtCol.SetLength(length());
    for (long n=0; n<length(); n++)
    {
        vec_zz_p& colMod = crtCol[n];
        mat_zz_p& matMod = (*this)[n];

        colMod.SetLength(matMod.NumRows());
        // NOTE: this is OK, but beware that this would
        // not work for ZZ_p's without restoring context

        for (long i=0; i<matMod.NumRows(); i++)
            colMod[i] = matMod[i][j];
    }
}

//checks if all values of the matrix are smaller than the modolus
bool CRTmatrix::isSmall(long bitGap) const
{
    FHE_TIMER_START;
    mat_ZZ M; // reconstruct the matrix in ZZ format
    ZZ q;     // the modulus

    convert(M, q, *this); // cnovert to ZZ format

    q >>= bitGap;         // q / 2^{bitGap}

    for (long i=0; i<M.NumRows(); i++) for (long j=0; j<M.NumCols(); j++)
        {
            if (abs(M[i][j]) > q)
            {
                return false;
            }
        }
    return true;
}

// Choose a random n-by-m matrix in CRT representation
void CRTmatrix::randomFill(long n, long m)
{
    FHE_TIMER_START;

    const TDMatrixParams& prms = *params;
    SetLength(prms.kFactors);

    zz_pPush ppush; // backup NTL's current modulus

    EXEC_RANGE(length(), first, last)
    //for (long i=0; i< prms.kFactors; i++)
    for (long i=first; i< last; i++)
    {

        prms.zzp_context[i].restore();
        (*this)[i].SetDims(n, m);
        RandomFill((*this)[i]);
    }
    EXEC_RANGE_END
}

// Invert M modulo the product of the factors
bool CRTmatrix::invert(const mat_l& M)
{
    FHE_TIMER_START;
    SetLength(params->kFactors);

    zz_pPush ppush; // backup NTL's current modulus

    std::atomic<bool> result(true);

    EXEC_RANGE(params->kFactors, first, last)

    // Invert M wrt each factor separately
    //for (long i=0; i< params->kFactors; i++)
     for (long i=first; i< last; i++)
    {
        params->zzp_context[i].restore();
        // NOTE: Alternatively, can call relaxed_inv directly now
        if (!invMod((*this)[i], M))   // not invertible
        {
            SetLength(0);
            result = false;
            //return false;
        }
    }
    EXEC_RANGE_END

    return result;
    //return true;
}

void CRTmatrix::identity(long n)
{
    FHE_TIMER_START;
    zz_pPush ppush; // backup NTL's current modulus
    SetLength(params->kFactors);

    // Invert M wrt each factor separately
    for (long i=0; i< params->kFactors; i++)
    {
        params->zzp_context[i].restore();
        ident( (*this)[i], n ); // set as the identity mod f_i
    }
}

bool operator==(const CRTmatrix& A, const CRTmatrix& B)
{
    FHE_TIMER_START;
    if (A.params != B.params) return false;

    zz_pPush push; // backup the NTL current modulus
    for (long i=0; i < A.params->kFactors; i++)
    {
        (A.params->zzp_context)[i].restore();
#ifdef DEBUGPRINT
        cout << "A[i]=" << A[i] << ", B[i]=" << B[i] << endl;
#endif // DEBUGPRINT
        if (A[i] != B[i]) return false;
    }
    return true;
}

//invert a CRT matrix
bool CRTmatrix::invert(const CRTmatrix& A)
{
    FHE_TIMER_START;
    params = A.params;
    SetLength(params->kFactors);

    zz_pPush push; // backup the NTL current modulus

    for (long i=0; i < params->kFactors; i++)
    {
        const mat_zz_p& Amod = A[i];
        params->zzp_context[i].restore();

        zz_p d;
        //    if (isInvertible(Amod, params->factors[i]))
        //inv((*this)[i],Amod); // raises error if Amod is singular
        relaxed_inv(d,(*this)[i],Amod); //raises error if Amod is singular

        if (IsZero(d))
            return false; //non invertible
    }
    return true;
}

// reconstruct the matrix in ZZ fromat, returning in q the modulus
void convert(mat_ZZ& to, ZZ& q, const CRTmatrix& from)
{
    FHE_TIMER_START;
    q = to_ZZ(1L); // initalize to 1
    if (from.length() <=0)
    {
        to.kill(); // set as 0-by-0 matrix
        return;
    }

    to.SetDims(from[0].NumRows(), from[0].NumCols());
    clear(to);     // initialize to zero

    NTL::zz_pPush ppush; // backup NTL's current modulus
    for (long k=0; k<from.length(); k++)   // go over the factors one at a time
    {
        from.params->zzp_context[k].restore();
        NTL::CRT(to, q, from[k]); // incremental CRT
    }
}

// reconstruct the matrix in CRT fromat
// FIXME: can be optimized for many factors
void convert(CRTmatrix& to, const mat_ZZ& from, const TDMatrixParams& prms)
{
    FHE_TIMER_START;
    to.params = (TDMatrixParams*) &prms;
    to.SetLength(prms.kFactors);
    if (prms.kFactors==0) return; // nothing to do

    NTL::zz_pPush ppush; // backup NTL's current modulus

    EXEC_RANGE(to.length(), first, last)

    for (long k=first; k<last; k++)   // go over the factors one at a time
    {
        prms.zzp_context[k].restore();
        conv(to[k], from); // compute M mod current factor
    }

    EXEC_RANGE_END
}

void generateMultiPair(CRTmatrix& A, CRTmatrix& Ainv, long n)
{
    FHE_TIMER_START;

    NTL::Vec<CRTmatrix> Amats(INIT_SIZE, 12, A);
    NTL::Vec<CRTmatrix> Ainvmats(INIT_SIZE, 12, Ainv);

    EXEC_RANGE(12, first, last)
    for (long i=first; i<last; i++)
    {

        CRTmatrix &Apair = Amats[i];
        CRTmatrix &AinvPair = Ainvmats[i];
        generateMatrixPair(Apair, AinvPair, n);
    }

    EXEC_RANGE_END
}

void generateMatrixPair(CRTmatrix& A, CRTmatrix& Ainv, long n)
{
    FHE_TIMER_START;
    const TDMatrixParams& prms = *(A.params);

    A.SetLength(prms.kFactors);
    Ainv.SetLength(prms.kFactors);

    zz_pPush push; // backup the NTL current modulus

    // It's possible to do parallilizing at this level
    // need to call SetNumThreads(nt) in main.

    // NOTE: the zz_pPush in the "main" thread is sufficient,
    // as the "worker" threads do not (and should not) assume
    // any particular contextual state

#ifdef DEBUGPRINT
        cerr << "*** " << prms.kFactors << " " << AvailableThreads() << "\n";
#endif // DEBUGPRINT

    EXEC_RANGE(prms.kFactors, first, last)
    for (long i=first; i<last; i++)
    {

        mat_zz_p& Amod = A[i];
        mat_zz_p& AinvMod = Ainv[i];
        prms.zzp_context[i].restore(); // set i'th factor as NTL's current modulus
        GenerateMatrixPair(Amod, AinvMod, n);
    }
    EXEC_RANGE_END

    FHE_TIMER_STOP;
};

// binary I/O
#undef CRTMAT_CONVtoZZ
long CRTmatrix::writeToFile(FILE* handle) const
{
    FHE_TIMER_START;

    // write the parameters first
    long count = params->writeToFile(handle);
    assert (this->length() == params->kFactors);

#ifndef CRTMAT_CONVtoZZ
    long n,m;
    if (this->length()<=0)
        n = m = 0;
    else
    {
        n = (*this)[0].NumRows();
        m = (*this)[0].NumCols();
    }
    count += fwrite(&n, sizeof(n), 1, handle); // how many rows
    count += fwrite(&m, sizeof(m), 1, handle); // how many cols

    unsigned char buf[params->e];
    NTL::zz_pPush ppush; // backup NTL's current modulus
    for (long f=0; f<this->length(); f++)   // write one factor at a time
    {
        params->zzp_context[f].restore();
        const Mat<zz_p>& mat = (*this)[f];
        for (long i=0; i<n; i++) for (long j=0; j<m; j++)   // write every entry
            {
                for (long ie=0; ie < params->e; ie++) // write every byte
                    buf[ie] = (rep(mat[i][j]) >> (ie*8)) & 0xff; // extract one byte
                count += fwrite(buf, params->e, 1, handle); // do the actual writing
            }
#ifdef DEBUGPRINT
        //cout << "*this[f]="  << (*this)[f] << endl;
#endif // DEBUGPRINT
    }

#else
    ZZ q;
    mat_ZZ zM;
    convert(zM, q, *this); // convert to ZZ representation, then write it

    long qBytes = NumBytes(q); // # of bytes to represent q
    count += fwrite(&qBytes, sizeof(qBytes),1, handle); // how many bytes per int

    long n = zM.NumRows();
    long m = zM.NumCols();
    count += fwrite(&n, sizeof(n), 1, handle); // how many rows
    count += fwrite(&m, sizeof(m), 1, handle); // how many cols

    unsigned char buf[qBytes];
    for (long i=0; i<n; i++)
        for (long j=0; j<m; j++)
        {
            if (sign(zM[i][j])<0) zM[i][j] += q; // map to interval [0,q-1]
            BytesFromZZ(buf, zM[i][j], qBytes);      // get binary representation
            count += fwrite(buf, qBytes, 1, handle); // write it to file
        }
#endif
    return count;
}

long CRTmatrix::readFromFile(FILE* handle, TDMatrixParams* prmBuf)
{
    FHE_TIMER_START;

#ifdef DEBUGPRINT
    cout << "reading" << endl;
#endif

    assert(params != NULL || prmBuf != NULL); // some pointer must be provided
    long count;

    // If buffer is given, make params point to it and don't overwrite from iput
    if (prmBuf != NULL)
    {
        TDMatrixParams p;
        count = p.readFromFile(handle);
        assert(p == *prmBuf); // sanity check
        params = prmBuf;      // point to given params
    }
    else
        count = params->readFromFile(handle); // overwrite params from input

#ifndef CRTMAT_CONVtoZZ
    long n,m;
    count += fread(&n, sizeof(n), 1, handle); // how many rows
    count += fread(&m, sizeof(m), 1, handle); // how many column
    if (count == 0) return count; //nothing is read

    this->SetLength(params->kFactors);
    if (params->kFactors==0) return count; // nothing to do

    unsigned char buf[params->e];
    NTL::zz_pPush ppush; // backup NTL's current modulus
    for (long f=0; f<this->length(); f++)   // write one factor at a time
    {
        params->zzp_context[f].restore();
        (*this)[f].SetDims(n,m);
        for (long i=0; i<n; i++) for (long j=0; j<m; j++)   // write every entry
            {
                count += fread(buf, params->e, 1, handle); // read from file

                long zp = buf[(params->e)-1];  // assemble the integer, byte by byte
                //for (long ie=1; ie < params->e; ie++)
                for (long ie=((params->e)-2); ie >=0 ; ie--)
                {
                    zp <<= 8;
                    zp += buf[ie];
                }
                (*this)[f][i][j].LoopHole() = zp; // avoid calling rem()
            }
    }
#else
    long qBytes, n, m;
    count += fread(&qBytes, sizeof(qBytes), 1, handle); // how many bytes per int
    count += fread(&n, sizeof(n), 1, handle); // how many rows
    count += fread(&m, sizeof(m), 1, handle); // how many column

    if (count == 0) return count; //nothing is read

    unsigned char buf[qBytes];
    mat_ZZ zM(INIT_SIZE, n, m);
    for (long i=0; i<n; i++) for (long j=0; j<m; j++)
        {
            count += fread(buf, qBytes, 1, handle); // read from file
            ZZFromBytes(zM[i][j], buf, qBytes);     // make a ZZ object
        }

    convert(*this, zM, *params); // convert to CRT representation
#endif
    return count;
}

max_stars_repo_name: shaih/BPobfus

max_issues_repo_name: shaih/BPobfus

max_forks_repo_name: shaih/BPobfus


text:
#include "dynet/nodes.h"
#include "dynet/dynet.h"
#include "dynet/training.h"
#include "dynet/timing.h"
#include "dynet/rnn.h"
#include "dynet/gru.h"
#include "dynet/lstm.h"
#include "dynet/dict.h"
#include "dynet/expr.h"
#include "getpid.h"

#include <iostream>
#include <fstream>
#include <sstream>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

using namespace std;
using namespace dynet;

float pdrop = 0.5;
unsigned LAYERS = 1;
unsigned INPUT_DIM = 128;
unsigned HIDDEN_DIM = 128;
unsigned TAG_HIDDEN_DIM = 32;
unsigned TAG_DIM = 32;
unsigned TAG_SIZE = 0;
unsigned VOCAB_SIZE = 0;

bool eval = false;
dynet::Dict d;
dynet::Dict td;
int kNONE;
int kSOS;
int kEOS;

template <class Builder>
struct RNNLanguageModel {
  LookupParameter p_w;
  Parameter p_l2th;
  Parameter p_r2th;
  Parameter p_thbias;

  Parameter p_th2t;
  Parameter p_tbias;
  Builder l2rbuilder;
  Builder r2lbuilder;
  explicit RNNLanguageModel(Model& model) :
      l2rbuilder(LAYERS, INPUT_DIM, HIDDEN_DIM, &model),
      r2lbuilder(LAYERS, INPUT_DIM, HIDDEN_DIM, &model) {
    p_w = model.add_lookup_parameters(VOCAB_SIZE, {INPUT_DIM});
    p_l2th = model.add_parameters({TAG_HIDDEN_DIM, HIDDEN_DIM});
    p_r2th = model.add_parameters({TAG_HIDDEN_DIM, HIDDEN_DIM});
    p_thbias = model.add_parameters({TAG_HIDDEN_DIM});

    p_th2t = model.add_parameters({TAG_SIZE, TAG_HIDDEN_DIM});
    p_tbias = model.add_parameters({TAG_SIZE});
  }

  // return Expression of total loss
  Expression BuildTaggingGraph(const vector<int>& sent, const vector<int>& tags, ComputationGraph& cg, double* cor = 0, unsigned* ntagged = 0) {
    const unsigned slen = sent.size();
    l2rbuilder.new_graph(cg);  // reset RNN builder for new graph
    l2rbuilder.start_new_sequence();
    r2lbuilder.new_graph(cg);  // reset RNN builder for new graph
    r2lbuilder.start_new_sequence();
    Expression i_l2th = parameter(cg, p_l2th);
    Expression i_r2th = parameter(cg, p_r2th);
    Expression i_thbias = parameter(cg, p_thbias);
    Expression i_th2t = parameter(cg, p_th2t);
    Expression i_tbias = parameter(cg, p_tbias);
    vector<Expression> errs;
    vector<Expression> i_words(slen);
    vector<Expression> fwds(slen);
    vector<Expression> revs(slen);

    // read sequence from left to right
    l2rbuilder.add_input(lookup(cg, p_w, kSOS));
    for (unsigned t = 0; t < slen; ++t) {
      i_words[t] = lookup(cg, p_w, sent[t]);
      if (!eval) { i_words[t] = noise(i_words[t], 0.1); }
      fwds[t] = l2rbuilder.add_input(i_words[t]);
    }

    // read sequence from right to left
    r2lbuilder.add_input(lookup(cg, p_w, kEOS));
    for (unsigned t = 0; t < slen; ++t)
      revs[slen - t - 1] = r2lbuilder.add_input(i_words[slen - t - 1]);

    for (unsigned t = 0; t < slen; ++t) {
      if (tags[t] != kNONE) {
        if (ntagged) (*ntagged)++;
        Expression i_th = tanh(affine_transform({i_thbias, i_l2th, fwds[t], i_r2th, revs[t]}));
        //if (!eval) { i_th = dropout(i_th, pdrop); }
        Expression i_t = affine_transform({i_tbias, i_th2t, i_th});
        if (cor) {
          vector<float> dist = as_vector(cg.incremental_forward(i_t));
          double best = -9e99;
          int besti = -1;
          for (int i = 0; i < dist.size(); ++i) {
            if (dist[i] > best) { best = dist[i]; besti = i; }
          }
          if (tags[t] == besti) (*cor)++;
        }
        if (tags[t] != kNONE) {
          Expression i_err = pickneglogsoftmax(i_t, tags[t]);
          errs.push_back(i_err);
        }
      }
    }
    return sum(errs);
  }
};

int main(int argc, char** argv) {
  dynet::initialize(argc, argv);
  if (argc != 3 && argc != 4) {
    cerr << "Usage: " << argv[0] << " corpus.txt dev.txt [model.params]\n";
    return 1;
  }
  kNONE = td.convert("*");
  kSOS = d.convert("<s>");
  kEOS = d.convert("</s>");
  vector<pair<vector<int>,vector<int>>> training, dev;
  string line;
  int tlc = 0;
  int ttoks = 0;
  cerr << "Reading training data from " << argv[1] << "...\n";
  {
    ifstream in(argv[1]);
    assert(in);
    while(getline(in, line)) {
      ++tlc;
      int nc = 0;
      vector<int> x,y;
      read_sentence_pair(line, &x, &d, &y, &td);
      assert(x.size() == y.size());
      if (x.size() == 0) { cerr << line << endl; abort(); }
      training.push_back(make_pair(x,y));
      for (unsigned i = 0; i < y.size(); ++i) {
        if (y[i] != kNONE) { ++nc; }
      }
      if (nc == 0) {
        cerr << "No tagged tokens in line " << tlc << endl;
        abort();
      }
      ttoks += x.size();
    }
    cerr << tlc << " lines, " << ttoks << " tokens, " << d.size() << " types\n";
    cerr << "Tags: " << td.size() << endl;
  }
  d.freeze(); // no new word types allowed
  td.freeze(); // no new tag types allowed
  VOCAB_SIZE = d.size();
  TAG_SIZE = td.size();

  int dlc = 0;
  int dtoks = 0;
  cerr << "Reading dev data from " << argv[2] << "...\n";
  {
    ifstream in(argv[2]);
    assert(in);
    while(getline(in, line)) {
      ++dlc;
      vector<int> x,y;
      read_sentence_pair(line, &x, &d, &y, &td);
      assert(x.size() == y.size());
      dev.push_back(make_pair(x,y));
      dtoks += x.size();
    }
    cerr << dlc << " lines, " << dtoks << " tokens\n";
  }
  ostringstream os;
  os << "tagger"
     << '_' << LAYERS
     << '_' << INPUT_DIM
     << '_' << HIDDEN_DIM
     << "-pid" << getpid() << ".params";
  const string fname = os.str();
  cerr << "Parameters will be written to: " << fname << endl;
  double best = 9e+99;

  Model model;
  bool use_momentum = true;
  Trainer* sgd = nullptr;
  if (use_momentum)
    sgd = new MomentumSGDTrainer(&model);
  else
    sgd = new SimpleSGDTrainer(&model);

  RNNLanguageModel<LSTMBuilder> lm(model);
  //RNNLanguageModel<SimpleRNNBuilder> lm(model);
  if (argc == 4) {
    string fname = argv[3];
    ifstream in(fname);
    boost::archive::text_iarchive ia(in);
    ia >> model;
  }

  unsigned report_every_i = 50;
  unsigned dev_every_i_reports = 25;
  unsigned si = training.size();
  vector<unsigned> order(training.size());
  for (unsigned i = 0; i < order.size(); ++i) order[i] = i;
  bool first = true;
  int report = 0;
  unsigned lines = 0;
  while(1) {
    Timer iteration("completed in");
    double loss = 0;
    unsigned ttags = 0;
    double correct = 0;
    for (unsigned i = 0; i < report_every_i; ++i) {
      if (si == training.size()) {
        si = 0;
        if (first) { first = false; } else { sgd->update_epoch(); }
        cerr << "**SHUFFLE\n";
        shuffle(order.begin(), order.end(), *rndeng);
      }

      // build graph for this instance
      ComputationGraph cg;
      auto& sent = training[order[si]];
      ++si;
      Expression loss_expr = lm.BuildTaggingGraph(sent.first, sent.second, cg, &correct, &ttags);
      loss += as_scalar(cg.forward(loss_expr));
      cg.backward(loss_expr);
      sgd->update(1.0);
      ++lines;
    }
    sgd->status();
    cerr << " E = " << (loss / ttags) << " ppl=" << exp(loss / ttags) << " (acc=" << (correct / ttags) << ") ";

    // show score on dev data?
    report++;
    if (report % dev_every_i_reports == 0) {
      double dloss = 0;
      unsigned dtags = 0;
      double dcorr = 0;
      eval = true;
      //lm.p_th2t->scale_parameters(pdrop);
      for (auto& sent : dev) {
        ComputationGraph cg;
        Expression loss_expr = lm.BuildTaggingGraph(sent.first, sent.second, cg, &dcorr, &dtags);
        dloss += as_scalar(cg.forward(loss_expr));
      }
      //lm.p_th2t->scale_parameters(1/pdrop);
      eval = false;
      if (dloss < best) {
        best = dloss;
        ofstream out(fname);
        boost::archive::text_oarchive oa(out);
        oa << model;
      }
      cerr << "\n***DEV [epoch=" << (lines / (double)training.size()) << "] E = " << (dloss / dtags) << " ppl=" << exp(dloss / dtags) << " acc=" << (dcorr / dtags) << ' ';
    }
  }
  delete sgd;
}

max_stars_repo_name: slouvan/dynet

max_issues_repo_name: slouvan/dynet

max_forks_repo_name: slouvan/dynet


text:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#define _USE_MATH_DEFINES
#include "transforms.h"

#include <Eigen/Geometry>
#include <cmath>
#include <exception>
#include <iostream>

namespace scenepic
{
  namespace Transforms
  {
    Transform scale(float scale)
    {
      Transform matrix = Transform::Identity();
      matrix(0, 0) = matrix(1, 1) = matrix(2, 2) = scale;
      return matrix;
    }

    Transform scale(const Vector& scale)
    {
      Transform matrix = Transform::Identity();
      matrix(0, 0) = scale(0);
      matrix(1, 1) = scale(1);
      matrix(2, 2) = scale(2);
      return matrix;
    }

    Transform translate(const Vector& vec)
    {
      Transform matrix = Transform::Identity();
      matrix.topRightCorner(3, 1) = vec.transpose();
      return matrix;
    }

    Transform rotation_matrix_from_axis_angle(const Vector& axis, float angle)
    {
      if (std::abs(angle) < 0.0001)
      {
        return Transform::Identity();
      }

      float x = axis(0);
      float y = axis(1);
      float z = axis(2);
      float cos = std::cos(angle);
      float sin = std::sin(angle);
      Transform matrix;
      matrix << x * x + (1 - x * x) * cos, x * y * (1 - cos) - z * sin,
        x * z * (1 - cos) + y * sin, 0, x * y * (1 - cos) + z * sin,
        y * y + (1 - y * y) * cos, y * z * (1 - cos) - x * sin, 0,
        x * z * (1 - cos) - y * sin, z * y * (1 - cos) + x * sin,
        z * z + (1 - z * z) * cos, 0, 0, 0, 0, 1;
      return matrix;
    }

    Quaternion quaternion_from_axis_angle(const Vector& axis, float angle)
    {
      Vector norm_axis = axis.normalized();
      float half_sin = std::sin(angle * 0.5f);
      float half_cos = std::cos(angle * 0.5f);
      Quaternion quat(
        norm_axis(0) * half_sin,
        norm_axis(1) * half_sin,
        norm_axis(2) * half_sin,
        half_cos);
      return quat;
    }

    std::pair<Vector, float> axis_angle_to_align_x_to_axis(const Vector& axis)
    {
      std::pair<Vector, float> axis_angle;
      Vector norm_axis = axis.normalized();
      if (norm_axis(1) == 0 && norm_axis(2) == 0)
      {
        if (norm_axis(0) == -1)
        {
          axis_angle.first << 0, 1, 0;
          axis_angle.second = static_cast<float>(M_PI);
        }
        else
        {
          axis_angle.first << 1, 0, 0;
          axis_angle.second = 0;
        }
      }
      else
      {
        float rot_angle = std::acos(norm_axis(0));
        if (rot_angle == 0)
        {
          axis_angle.first << 1, 0, 0;
          axis_angle.second = 0;
        }
        else
        {
          axis_angle.first << 0, -norm_axis(2), norm_axis(1);
          axis_angle.first /= std::sqrt(
            norm_axis(2) * norm_axis(2) + norm_axis(1) * norm_axis(1));
          axis_angle.second = rot_angle;
        }
      }

      return axis_angle;
    }

    Quaternion quaternion_to_align_x_to_axis(const Vector& axis)
    {
      auto axis_angle = axis_angle_to_align_x_to_axis(axis);
      return quaternion_from_axis_angle(axis_angle.first, axis_angle.second);
    }

    Transform rotation_to_align_x_to_axis(const Vector& axis)
    {
      auto axis_angle = axis_angle_to_align_x_to_axis(axis);
      return rotation_matrix_from_axis_angle(
        axis_angle.first, axis_angle.second);
    }

    Transform rotation_about_x(float angle)
    {
      float cos = std::cos(angle);
      float sin = std::sin(angle);
      Transform matrix;
      matrix << 1, 0, 0, 0, 0, cos, -sin, 0, 0, sin, cos, 0, 0, 0, 0, 1;
      return matrix;
    }

    Transform rotation_about_y(float angle)
    {
      float cos = std::cos(angle);
      float sin = std::sin(angle);
      Transform matrix;
      matrix << cos, 0, sin, 0, 0, 1, 0, 0, -sin, 0, cos, 0, 0, 0, 0, 1;
      return matrix;
    }

    Transform rotation_about_z(float angle)
    {
      float cos = std::cos(angle);
      float sin = std::sin(angle);
      Transform matrix;
      matrix << cos, -sin, 0, 0, sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1;
      return matrix;
    }

    Transform look_at_rotation(
      const Vector& center, const Vector& look_at, const Vector& up_dir)
    {
      Transform matrix = Transform::Identity();
      auto z_axis = (center - look_at).normalized();
      auto x_axis = (up_dir.cross(z_axis)).normalized();
      auto y_axis = (z_axis.cross(x_axis)).normalized();
      matrix.row(0).leftCols(3) = x_axis;
      matrix.row(1).leftCols(3) = y_axis;
      matrix.row(2).leftCols(3) = z_axis;
      return matrix;
    }

    Transform euler_angles_to_matrix(
      const Vector& euler_angles, const std::string& convention)
    {
      Transform matrix = Transform::Identity();
      for (auto i = 2; i >= 0; --i)
      {
        auto axis = convention[i];
        float angle = euler_angles(i);
        switch (axis)
        {
          case 'X':
          case 'x':
            matrix = rotation_about_x(angle) * matrix;
            break;

          case 'Y':
          case 'y':
            matrix = rotation_about_y(angle) * matrix;
            break;

          case 'Z':
          case 'z':
            matrix = rotation_about_z(angle) * matrix;
            break;

          default:
            throw std::invalid_argument("Invalid convention: " + convention);
        }
      }

      return matrix;
    }

    Transform quaternion_to_matrix(const Quaternion& quaternion)
    {
      float qw = quaternion.w();
      float qx = quaternion.x();
      float qy = quaternion.y();
      float qz = quaternion.z();
      float qx2 = qx * qx;
      float qy2 = qy * qy;
      float qz2 = qz * qz;
      Transform matrix;
      matrix << 1 - 2 * qy2 - 2 * qz2, 2 * qx * qy - 2 * qz * qw,
        2 * qx * qz + 2 * qy * qw, 0, 2 * qx * qy + 2 * qz * qw,
        1 - 2 * qx2 - 2 * qz2, 2 * qy * qz - 2 * qx * qw, 0,
        2 * qx * qz - 2 * qy * qw, 2 * qy * qz + 2 * qx * qw,
        1 - 2 * qx2 - 2 * qy2, 0, 0, 0, 0, 1;
      return matrix;
    }

    Quaternion quaternion_multiply(const Quaternion& a, const Quaternion& b)
    {
      float x = a.w() * b.x() + a.x() * b.w() + a.y() * b.z() - a.z() * b.y();
      float y = a.w() * b.y() + a.y() * b.w() + a.z() * b.x() - a.x() * b.z();
      float z = a.w() * b.z() + a.z() * b.w() + a.x() * b.y() - a.y() * b.x();
      float w = a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z();
      return Quaternion(x, y, z, w);
    }

    Transform gl_projection(
      double fov_y_degrees, double aspect_ratio, double znear, double zfar)
    {
      double fov_y = (M_PI * fov_y_degrees / 180.0);
      double f = 1.0 / std::tan(fov_y / 2);
      float fx = static_cast<float>(f / aspect_ratio);
      float fy = static_cast<float>(f);
      double nf = 1.0 / (znear - zfar);
      float A = static_cast<float>((zfar + znear) * nf);
      float B = static_cast<float>(2 * zfar * znear * nf);

      Transform matrix;
      matrix << fx, 0, 0, 0, 0, fy, 0, 0, 0, 0, A, B, 0, 0, -1, 0;
      return matrix;
    }

    Transform gl_projection(
      const Intrinsic& camera_matrix,
      int width,
      int height,
      double znear,
      double zfar)
    {
      float K00 = camera_matrix(0, 0);
      float K01 = camera_matrix(0, 1);
      float K02 = camera_matrix(0, 2);
      float K11 = camera_matrix(1, 1);
      float K12 = camera_matrix(1, 2);
      float A = static_cast<float>((zfar + znear) / (znear - zfar));
      float B = static_cast<float>(2 * zfar * znear / (znear - zfar));
      Transform matrix;
      matrix << 2 * K00 / width, -2 * K01 / width, (width - 2 * K02) / width, 0,
        0, 2 * K11 / height, (2 * K12 - height) / height, 0, 0, 0, A, B, 0, 0,
        -1, 0;
      return matrix;
    }

    Transform gl_world_to_camera(const Extrinsic& extrinsic)
    {
      Transform camera_to_world =
        extrinsic * rotation_about_x(static_cast<float>(M_PI));
      return camera_to_world.inverse();
    }

  } // namespace Transforms
} // namespace scenepic

max_stars_repo_name: microsoft/scenepic

max_issues_repo_name: microsoft/scenepic

max_forks_repo_name: microsoft/scenepic


text:
/*
 * MIT License
 *
 * Copyright (c) 2018 PulsarSearchSoft
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "pss/astrotypes/sigproc/HeaderField.h"
#include <boost/units/quantity.hpp>
#include <boost/units/cmath.hpp>
#include <iostream>
#include <exception>
#include <cmath>

namespace pss {
namespace astrotypes {
namespace sigproc {

template<typename T>
HeaderField<T>::HeaderField(SigProcLabel const& header_label, Header& h)
    : BaseT(header_label, h)
{
}

template<typename T>
HeaderField<T>::HeaderField(SigProcLabel const& header_label, Header& h, T const& t)
    : BaseT(header_label, h)
    , _var(t)
{
}

template<typename T>
HeaderField<T>::HeaderField(SigProcLabel const& header_label, Header& h, HeaderField const& t)
    : BaseT(header_label, h)
    , _var(t._var)
{
}

template<typename T>
HeaderField<T>& HeaderField<T>::operator=(T const& var )
{
    _var = var;
    return *this;
}

template<typename T>
void HeaderField<T>::operator=(const HeaderFieldBase& h )
{
    _var = reinterpret_cast<HeaderField<T> const&>(h)._var;
}

template<typename T>
unsigned HeaderField<T>::read(std::istream& stream)
{
    T v;
    auto count = SigProcVariable<T>::read(stream, v);
    _var=v;
    return count;
}

template<typename T>
unsigned HeaderField<T>::write(std::ostream& stream) const
{
    return SigProcVariable<T>::write(stream, *_var);
}

template<typename T>
void HeaderField<T>::write_info(std::ostream& stream) const
{
    stream << std::setprecision(16) << *_var;
}

template<typename T>
void HeaderField<T>::reset()
{
    _var.reset();
}

template<typename T>
bool HeaderField<T>::is_set() const
{
    return _var.is_set();
}

template<typename T>
bool HeaderField<T>::operator==(const HeaderFieldBase& f) const
{
    return *this == reinterpret_cast<HeaderField const&>(f);
}

template<typename T>
bool HeaderField<T>::operator==(const HeaderField& f) const
{
    return *f._var == *_var;
}
// -------------- std::vector specialization
template<typename T>
HeaderField<std::vector<T>>::HeaderField( SigProcLabel const& start_label
                                        , SigProcLabel const& item_label
                                        , SigProcLabel const& end_label
                                        , Header& header)
    : BaseT(start_label, header)
    , _item_label_handler(_var)
    , _end_label(end_label)
    , _item_label(item_label)
{
    // register the read handlers
    BaseT::add_read(end_label, _end_label_handler, header);
    BaseT::add_read(item_label, _item_label_handler, header);
}

template<typename T>
HeaderField<std::vector<T>>::HeaderField( SigProcLabel const& start_label
                                        , SigProcLabel const& item_label
                                        , SigProcLabel const& end_label
                                        , Header& header
                                        , HeaderField const& copy)
    : BaseT(start_label, header)
    , _var(copy._var)
    , _item_label_handler(_var)
    , _end_label(end_label)
    , _item_label(item_label)
{
    // register the read handlers
    BaseT::add_read(end_label, _end_label_handler, header);
    BaseT::add_read(item_label, _item_label_handler, header);
}

template<typename T>
void HeaderField<std::vector<T>>::reset()
{
    _var.clear();
}

template<typename T>
bool HeaderField<std::vector<T>>::is_set() const
{
    return !_var.empty();
}

template<typename T>
unsigned HeaderField<std::vector<T>>::read(std::istream&)
{
    _var.clear();
    return 0;
}

template<typename T>
unsigned HeaderField<std::vector<T>>::ItemField::read(std::istream& stream)
{
    T v;
    unsigned c = SigProcVariable<T>::read(stream, v);
    _vec.emplace_back(v);
    return c;
}

template<typename T>
unsigned HeaderField<std::vector<T>>::write(std::ostream& stream) const
{
    unsigned size=0;
    for(auto const& var : _var) {
        stream << _item_label;
        size += _item_label.size();
        size += SigProcVariable<T>::write(stream, var);
    }
    // write out the end marker
    stream << _end_label;
    size += _end_label.size();

    return size;
}

template<typename T>
std::string const& HeaderField<std::vector<T>>::header_info(std::string const&) const
{
    return _item_label.string();
}

template<typename T>
void HeaderField<std::vector<T>>::write_info(std::ostream& stream) const
{
    // list all the elements if its a reasonable number
    std::string sep="";
    stream << "(";
    if(_var.size() < 4) {
        for( auto const& var : _var )
        {
            stream << sep << var;
            sep = ", ";
        }
    }
    else {
        stream << _var[0] << ", " << _var[1] << ", ..., " << _var.back();
    }
    stream << ") " << _var.size() << " elements";
}

template<typename T>
bool HeaderField<std::vector<T>>::operator==(HeaderFieldBase const& h) const
{
    return *this == reinterpret_cast<HeaderField<std::vector<T>> const&>(h);
}

template<typename T>
bool HeaderField<std::vector<T>>::operator==(const HeaderField& h) const
{
    if(h._var.size() != _var.size()) return false;
    return true;    // only checks if sizes are the same. is this enough?
}

template<typename T>
void HeaderField<std::vector<T>>::operator=(const HeaderFieldBase& h )
{
    _var = reinterpret_cast<HeaderField<std::vector<T>> const&>(h)._var;
}

// ------ HeaderFieldWithTolerance ------
template<typename T, typename ToleranceType>
HeaderFieldWithTolerance<T, ToleranceType>::HeaderFieldWithTolerance(SigProcLabel const& header_label, Header& header, ToleranceType const& t)
    : BaseT(header_label, header)
    , _tolerance(t)
{
}

template<typename T, typename ToleranceType>
HeaderFieldWithTolerance<T, ToleranceType>::HeaderFieldWithTolerance(SigProcLabel const& header_label, Header& header, ToleranceType const& t, HeaderFieldWithTolerance const& copy)
    : BaseT(header_label, header, copy)
    , _tolerance(t)
{
}

template<typename T, typename ToleranceType>
HeaderFieldWithTolerance<T, ToleranceType>& HeaderFieldWithTolerance<T, ToleranceType>::operator=(T const& h)
{
    static_cast<BaseT&>(*this) = h;
    return *this;
}

template<typename T, typename ToleranceType>
bool HeaderFieldWithTolerance<T, ToleranceType>::operator==(HeaderFieldBase const& h) const
{
    return *this == reinterpret_cast<HeaderFieldWithTolerance const&>(h);
}

namespace {
template<typename T2>
struct compare_tolerance
{
    template<typename T1>
    static inline
    bool exec(T1 const& t1, T2 const& t2) {
        return t1 < t2;
    }
};

template<typename T2>
struct compare_tolerance<HeaderField<T2>>
{
    template<typename T1>
    static inline
    bool exec(T1 const& t1, HeaderField<T2> const& t2) {
        if(t2.is_set())
            return t1 < static_cast<T2 const&>(t2);
        return true;
    }
};

} // namespace

template<typename T, typename ToleranceType>
bool HeaderFieldWithTolerance<T, ToleranceType>::operator==(HeaderFieldWithTolerance const& h) const
{
    // allow for ADL lookups defaulting to std
    using std::abs;
    return compare_tolerance<typename std::decay<ToleranceType>::type>::exec(abs(*this->_var - *h._var) , _tolerance);
}

inline HeaderFieldBase::HeaderFieldBase(SigProcLabel const& header_name, Header& h)
{
    h.add(header_name, *this);
}

inline void HeaderFieldBase::add_read(SigProcLabel const& header_label, HeaderFieldBase& field, Header& header)
{
    header.add_read(header_label, field);
}

} // namespace sigproc
} // namespace astrotypes
} // namespace pss

max_stars_repo_name: PulsarSearchSoft/astrotypes

max_issues_repo_name: PulsarSearchSoft/astrotypes

max_forks_repo_name: PulsarSearchSoft/astrotypes


text:
/*
 * Copyright (c) CERN 2013-2015
 *
 * Copyright (c) Members of the EMI Collaboration. 2010-2013
 *  See  http://www.eu-emi.eu/partners for details on the copyright
 *  holders.
 *
 * 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.
 */

#include <boost/test/unit_test_suite.hpp>
#include <boost/test/test_tools.hpp>

#include "cli/ui/SetCfgCli.h"

using fts3::cli::bad_option;
using fts3::cli::SetCfgCli;

BOOST_AUTO_TEST_SUITE(cli)
BOOST_AUTO_TEST_SUITE(SetCfgCliTest)

BOOST_AUTO_TEST_CASE (SetCfgCliOverflow)
{
    SetCfgCli cli;
    std::vector<const char*> argv{
        "prog_name",
        "-s",
        "https://fts3-server:8080",
        "--bring-online",
        "srm://se",
        "11111111111111111111111111111111111111111111111111111111111111111111111111"
    };
    BOOST_CHECK_THROW(cli.parse(argv.size(), (char**)argv.data()), bad_option);
}

BOOST_AUTO_TEST_CASE (SetCfgCliBadLexicalCast)
{
    SetCfgCli cli;
    std::vector<const char*> argv2 {
        "prog_name",
        "-s",
        "https://fts3-server:8080",
        "--bring-online",
        "srm://se",
        "dadadad"
    };
    BOOST_CHECK_THROW(cli.parse(argv2.size(), (char**)argv2.data()), bad_option);
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()


text:
#include <burst/container/make_forward_list.hpp>
#include <burst/container/make_vector.hpp>
#include <burst/range/adaptor/buffered_chunked.hpp>

#include <doctest/doctest.h>

#include <boost/range/iterator_range.hpp>

TEST_SUITE("buffered_chunked")
{
    TEST_CASE("Может работать с rvalue-диапазоном")
    {
        const auto v = burst::make_vector({1, 2, 3, 4, 5, 6});

        const auto chunks = boost::make_iterator_range(v) | burst::buffered_chunked(3);

        const auto expected = {1, 2, 3};
        CHECK(chunks.front() == expected);
    }

    TEST_CASE("Может работать с lvalue-диапазоном")
    {
        const auto v = burst::make_forward_list({3u, 2u, 1u});

        const auto chunks = v | burst::buffered_chunked(3);

        const auto expected = {3, 2, 1};
        CHECK(chunks.front() == expected);
    }
}

max_stars_repo_name: izvolov/thrust

max_issues_repo_name: izvolov/burst

max_forks_repo_name: izvolov/thrust