google-research
58 строк · 1.7 Кб
1// Copyright 2023 The Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "utilities.h"
16
17#include <stdint.h>
18
19#include <algorithm>
20#include <cassert>
21#include <cmath>
22#include <cstdlib>
23#include <iostream>
24#include <random>
25#include <string>
26#include <vector>
27
28std::mt19937 RandomHandler::generator_;
29
30std::string PrettyNum(int64_t number) {
31std::string pretty_number = std::to_string(number);
32for (int i = static_cast<int>(pretty_number.size()) - 3; i > 0; i -= 3) {
33pretty_number.insert(pretty_number.begin() + i, ',');
34}
35return pretty_number;
36}
37
38void Fail(const std::string& error) {
39std::cerr << error << std::endl;
40exit(1);
41}
42
43std::vector<double> LogSpace(double small, double large, double base) {
44if (small > large) {
45std::vector<double> log_space = LogSpace(large, small, base);
46std::reverse(log_space.begin(), log_space.end());
47return log_space;
48}
49assert(base > 1);
50int steps =
51static_cast<int>(ceil((log(large) - log(small)) / log(base) - 1e-6));
52double step = pow(large / small, 1.0 / steps);
53std::vector<double> log_space = {small};
54for (int i = 0; i < steps; ++i) {
55log_space.push_back(log_space.back() * step);
56}
57return log_space;
58}
59