<commit_before>﻿// Copyright (c) 2016, reginell. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Subject: Algorithms and Data structures (AaDs).
// Lab work 2.4: Derivative / integral functions approximation.

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

double f(double x) {
  return pow(x, 2) + 5 * cos(x);
}

std::vector<double> dY_precise(const std::vector<double>& X) {
  const std::size_t n = X.size();
  std::vector<double> DY = std::vector<double>(n);

  // Точное значение 1 производной.
  for (std::size_t i = 0; i < n; i++)
    DY[i] = 2 * X[i] - 5 * sin(X[i]);

  return DY;
}

std::vector<double> d2Y_precise(const std::vector<double>& X) {
  const std::size_t n = X.size();
  std::vector<double> D2Y = std::vector<double>(n);

  // Точное значение 2 производной.
  for (std::size_t i = 0; i < n; i++)
    D2Y[i] = 2 - 5 * cos(X[i]);

  return D2Y;
}

double integral_precise(double x) {
  return pow(x, 3) / 3 + 5 * sin(x);
}

double integral_precise(double a, double b) {
  return integral_precise(b) - integral_precise(a);
}

std::vector<double> dY_approximate(const std::vector<double>& Y, double h) {
  const std::size_t n = Y.size();
  std::vector<double> dY = std::vector<double>(n);

  // Аппроксимация первой производной в крайних точках.
  dY[0] = -(3 * Y[0] - 4 * Y[1] + Y[2]) / (2 * h);
  dY[n - 1] = (Y[n - 3] - 4 * Y[n - 2] + 3 * Y[n - 1]) / (2 * h);

  // Аппроксимация первой производной в остальных точках.
  for (std::size_t i = 1; i < n - 1; i++)
    dY[i] = (Y[i + 1] - Y[i - 1]) / (2 * h);

  return dY;
}

std::vector<double> d2Y_approximate(const std::vector<double>& Y, double h) {
  const std::size_t n = Y.size();
  std::vector<double> d2Y = std::vector<double>(n);

  // В крайних точках не считаем.
  d2Y[0] = d2Y[n - 1] = nan("");

  // Аппроксимация второй производной.
  for (std::size_t i = 1; i < n - 1; i++)
    d2Y[i] = (Y[i - 1] - 2 * Y[i] + Y[i + 1]) / (h * h);

  return d2Y;
}

double integral_by_3_rank_gauss_method(double (*func)(double),
  double a, double b, int m) {
  double step = (b - a) / m, step_half = step / 2;

  double x0 = a + step_half;
  double x1 = x0 - step_half * 0.7745966692;<commit_msg>Add missed std::<commit_after>﻿// Copyright (c) 2016, reginell. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Subject: Algorithms and Data structures (AaDs).
// Lab work 2.4: Derivative / integral functions approximation.

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

double f(double x) {
  return std::pow(x, 2) + 5 * std::cos(x);
}

std::vector<double> dY_precise(const std::vector<double>& X) {
  const std::size_t n = X.size();
  std::vector<double> DY = std::vector<double>(n);

  // Точное значение 1 производной.
  for (std::size_t i = 0; i < n; i++)
    DY[i] = 2 * X[i] - 5 * std::sin(X[i]);

  return DY;
}

std::vector<double> d2Y_precise(const std::vector<double>& X) {
  const std::size_t n = X.size();
  std::vector<double> D2Y = std::vector<double>(n);

  // Точное значение 2 производной.
  for (std::size_t i = 0; i < n; i++)
    D2Y[i] = 2 - 5 * std::cos(X[i]);

  return D2Y;
}

double integral_precise(double x) {
  return std::pow(x, 3) / 3 + 5 * std::sin(x);
}

double integral_precise(double a, double b) {
  return integral_precise(b) - integral_precise(a);
}

std::vector<double> dY_approximate(const std::vector<double>& Y, double h) {
  const std::size_t n = Y.size();
  std::vector<double> dY = std::vector<double>(n);

  // Аппроксимация первой производной в крайних точках.
  dY[0] = -(3 * Y[0] - 4 * Y[1] + Y[2]) / (2 * h);
  dY[n - 1] = (Y[n - 3] - 4 * Y[n - 2] + 3 * Y[n - 1]) / (2 * h);

  // Аппроксимация первой производной в остальных точках.
  for (std::size_t i = 1; i < n - 1; i++)
    dY[i] = (Y[i + 1] - Y[i - 1]) / (2 * h);

  return dY;
}

std::vector<double> d2Y_approximate(const std::vector<double>& Y, double h) {
  const std::size_t n = Y.size();
  std::vector<double> d2Y = std::vector<double>(n);

  // В крайних точках не считаем.
  d2Y[0] = d2Y[n - 1] = std::nan("");

  // Аппроксимация второй производной.
  for (std::size_t i = 1; i < n - 1; i++)
    d2Y[i] = (Y[i - 1] - 2 * Y[i] + Y[i + 1]) / (h * h);

  return d2Y;
}

double integral_by_3_rank_gauss_method(double (*func)(double),
  double a, double b, int m) {
  double step = (b - a) / m, step_half = step / 2;

  double x0 = a + step_half;
  double x1 = x0 - step_half * 0.7745966692;
{'fraction_non_alphanumeric': 0.1860666378191259, 'fraction_numerical': 0.03072263089571614, 'mean_word_length': 3.3084808946877913, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6955882', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1978, 'n_words': 529}