optimization

Why does my sieve not perform well for finding primes?

故事扮演 提交于 2020-03-22 09:05:15
问题 I wrote two prime finder functions and the sieve only performs about 10% better. I'm using two optimizations for the simple version. Don't check even numbers Only check up to the square root or j * j <= i . ( equivalent ) and one optimization for the sieve version Only check up to the square root or i * i <= n . ( equivalent ) What optimizations can I add to the sieve? My sieve is pretty slow. I don't want to do a bitwise implementation yet, I want to understand if this implementation offers

Optimizing parsing of massive python dictionary, multi-threading

淺唱寂寞╮ 提交于 2020-03-18 09:59:31
问题 Let's take a small example python dictionary, where the values are lists of integers. example_dict1 = {'key1':[367, 30, 847, 482, 887, 654, 347, 504, 413, 821], 'key2':[754, 915, 622, 149, 279, 192, 312, 203, 742, 846], 'key3':[586, 521, 470, 476, 693, 426, 746, 733, 528, 565]} Let's say I need to parse the values of the lists, which I've implemented into the following function: def manipulate_values(input_list): return_values = [] for i in input_list: new_value = i ** 2 - 13 return_values

Gradient descent impementation python - contour lines

烈酒焚心 提交于 2020-03-18 05:17:20
问题 As a self study exercise I am trying to implement gradient descent on a linear regression problem from scratch and plot the resulting iterations on a contour plot. My gradient descent implementation gives the correct result (tested with Sklearn) however the gradient descent plot doesn't seem to be perpendicular to the contour lines. Is this expected or did I get something wrong in my code / understanding? Algorithm Cost function and gradient descent import numpy as np import pandas as pd from

Serialization/Deserialization of a Vector of Integers in C++

吃可爱长大的小学妹 提交于 2020-03-16 08:24:13
问题 Task to be Acomplished I'm trying to serialize a vector of integers into a string so that it can be stored into a file. The approach used is to copy the integers byte-by-byte into a buffer. For this I used the std::copy_n function. To deserialize, I've done the same thing in reverse i.e. copied byte-by-byte into an integer from the buffer and appended those integers to a vector. I'm not sure if this is the best/fastest way to achieve this. Code Serialize function char *serialize(vector <int>

How to optimize between two matrices, one of the matrices is derived from Excel data and the other matrix of the formula in Python code?

空扰寡人 提交于 2020-03-04 19:35:51
问题 The matrix code obtained as a formula is as follows: def objective(x): #= params x00 = x[0] y00 = x[1] u_m = x[2] a = x[3] b = x[4] u_mean_real = x[5] A = x[6] B = x[7] H = x[8] if 0.9 < y00 <= 1: alpha = 1.01 elif 0.8 < y00 <= 0.9: alpha = 1.02 elif 0.7 < y00 <= 0.8: alpha = 1.03 elif 0.6 < y00 <= 0.7: alpha = 1.04 elif 0.55 < y00 <= 0.6: alpha = 1.05 elif y00 <= 0.55: alpha = 1.1 if len(x) > 9: alpha = x[9] x = Symbol('x') y = Symbol('y') zeta = Symbol('zeta') sghi = Symbol('sigh') zeta0 =

Constrained Optimization of battery scheduling in microgrid

ⅰ亾dé卋堺 提交于 2020-03-03 11:55:21
问题 Given inputs such as electricity consumption, generation from solar panel, price, (All at a given time t), we have a battery, and we want to evaluate how much it should (dis)/charge at any given time. The Problem can be formulated as follows: Pt = price of electricity at time t Lt = consumption of electricity at time t Zt = charge of battery at time t (how much is in the battery) St = Electricity generated from solar generator at time t Qt = amount the battery (dis)/charges at time t the

Constrained Optimization of battery scheduling in microgrid

浪尽此生 提交于 2020-03-03 11:52:05
问题 Given inputs such as electricity consumption, generation from solar panel, price, (All at a given time t), we have a battery, and we want to evaluate how much it should (dis)/charge at any given time. The Problem can be formulated as follows: Pt = price of electricity at time t Lt = consumption of electricity at time t Zt = charge of battery at time t (how much is in the battery) St = Electricity generated from solar generator at time t Qt = amount the battery (dis)/charges at time t the

Lookup and sort the foreign collection

删除回忆录丶 提交于 2020-03-03 10:25:13
问题 so I have a collection users , and each document in this collection, as well as other properties, has an array of ids of documents in the other collection: workouts . Every document in the collection workouts has a property named date . And here's what I want to get: For a specific user, I want to get an array of {workoutId, workoutDate} for the workouts that belong to that user, sorted by date. This is my attempt, which is working fine. Users.aggregate([ { $match : { _id : ObjectId(

Optimisation: Shrinking file size in C or C++

落爺英雄遲暮 提交于 2020-03-03 04:50:07
问题 When performing computer simulations of systems with n (e.g. 10000) particles, the usual workflow involves saving the state of the system frequently at given intervals. This would entail writing down in a file the position coordinates of all the particles (so 3 floats/doubles per line, each line for a particle), with some header information. The floating precision is set to a fixed value. The way I usually save/write down my configuration files is as follows (part of a function that creates

optim in r :non finite finite difference error

三世轮回 提交于 2020-03-02 07:42:06
问题 I have a simple likelihood function (from a normal dist with mean=0) that I want to maximize. optim keeps giving me this error: Error in optim(par = phi, fn = loglike, estimates = estimates, NULL, hessian = TRUE, : non-finite finite-difference value [1] Here is my data and likelihood function: y = [ -0.01472 0.03942 0.03592 0.02776 -0.00090 ] C = a varcov matrix: 1.66e-03 -0.000120 -6.780e-06 0.000102 -4.000e-05 -1.20e-04 0.001387 7.900e-05 -0.000140 -8.000e-05 -6.78e-06 0.000079 1.416e-03 -0