Skip to content

Instantly share code, notes, and snippets.

#include <Rcpp.h>
#include <algorithm>
using namespace Rcpp;
inline double vec_sum(const Rcpp::NumericVector &x) {
return std::accumulate(x.begin(), x.end(), 0.0);
}
// [[Rcpp::export]]
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
ExecStart=/usr/bin/jupyter-notebook --no-browser
[Install]
WantedBy=multi-user.target
from sympy import symbols, diff, solve, init_printing, latex
init_printing(use_latex = "mathjax")
from IPython.display import display, Math
# аргументы и формула
x, y, = symbols(("x", "y"))
expr = x ** 3 + 4 * y ** 3 - 3 * x - 3 * y
display(Math(r"f(x,y) = %s" % latex(expr)))
# производные первого порядка
dx = diff(expr, x)
dy = diff(expr, y)
import urllib.request as req
import io
import zipfile
import tempfile
import os.path
url = "https://stepic.org/media/attachments/lesson/24465/main.zip"
with req.urlopen(url) as resp:
with zipfile.ZipFile(io.BytesIO(resp.read())) as zfile:
dirlist = {os.path.dirname(i) for i in zfile.namelist() if i.endswith(".py")}
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>
// [[Rcpp::export]]
Rcpp::NumericMatrix cbind_rcpp_for(const Rcpp::NumericMatrix& x, const Rcpp::NumericMatrix& y) {
std::size_t ncols_x = x.cols(), nrows_x = x.rows();
#include <Rcpp.h>
using namespace Rcpp;
double median(const NumericVector& x) {
NumericVector y(x);
std::size_t n = y.size();
int half = n / 2;
std::nth_element(y.begin(), y.begin() + half, y.end());
if(n % 2 == 1) {
#include <Rcpp.h>
// [[Rcpp::export]]
double fastsum(const Rcpp::NumericVector& x) {
std::size_t n = x.size();
double total = 0;
if (n > 0) {
for (std::size_t i = 0, k = n - 1; i < k; ++i, --k)
total += x[i] + x[k];
if (n % 2 == 1)
#!usr/bin/env python
import sys
import random
def make_pairs(l):
if len(l) % 2 != 0:
raise ValueError("Количество элементов должно быть чётным.")
return zip(l[::2], l[1::2])
@artemklevtsov
artemklevtsov / rep.cpp
Last active December 4, 2016 13:06
rep functions family implementation
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
template <int RTYPE>
Vector<RTYPE> rep_each_impl(const Vector<RTYPE>& x, std::size_t each) {
std::size_t n = x.size();
Vector<RTYPE> res = no_init(n * each);
auto begin = res.begin();
@artemklevtsov
artemklevtsov / shuffle.cpp
Last active August 13, 2016 19:18
Fisher-Yates Shuffle for the Rcpp vector
// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
using namespace Rcpp;
template <int RTYPE>
Vector<RTYPE> shuffle_impl(const Vector<RTYPE>& x) {
Vector<RTYPE> res(x); // make copy
auto first = res.begin(), last = res.end();