need an algorithm for continuity check: select a list of integer number to have best "coverage"
this is an hardcore algorithm problem that i'm facing right now:
let's say there is a sorted list of integers L1:
1. the total length of the list is known which is N (e.g. N could be 1e7)
2. all the elements are between two known boundaries, A and B , ( A <<< B )
e.g. L1 = [ 2,5,10,15,18,19,21...]
Right now, I need to select a subset of the elements from the list L1 to form a new list L2 with the total length of M (M < N)
(e.g. M could equal N /10 )
to satisfy a condition: the new list L2 needs to have best "coverage";
by "coverage", it means that all the elements integers in the L2 need to be distributed in the L1's range, [A,B], as equally as possible.
(a.k.a an unbiased subsampling method )
any help is deeply appreciated.
thanks for everyone's help and idea. I try to simplify the problem so that everyone (without the background knowledge can understand the problem). To define a rule for goodness of the coverage:
the ultimate goal is to achieve:
in the list L2, for any two neighbor element J and K, there are  J  K  , and the sum of this difference needs to be minimized
apply a given window with the total length of Q ( Q < M ) to list L2, and the number of elements within the window needs to be either equal (ideal situation) or almost equal
thanks
1 answer

My idea is to utilize bucket sort of which bucket size is (A  B) / M. After mapping each element in l1 to its corresponding bucket, pick element randomly from each bucket to from the new list. If the new list is shorter than m, then I repeat the process. The following is my implementation in Python:
import bisect import random import collections def form_new_list(l1, m, a, b, res): if m <= 0: return bucket_size = int((b  a) / m) bucket_list = collections.defaultdict(list) for i, num in enumerate(l1): bucket_num = int(num / bucket_size) bucket_list[bucket_num].append(num) for _, nums in bucket_list.items(): selected = random.choice(nums) position = bisect.bisect(res, selected) bisect.insort(res, selected) l1.remove(selected) form_new_list(l1, m  len(res), a, b, res) return res
See also questions close to this topic

Procedural algorithm to determine which math operation should be performed last
I'm building a procedural algorithm to determine which operation to perform last in a mathematical formula.
For context, I have written a token parser and a small interpreter, using VBA for Microsoft Excel.
But this is a generic algorithm question, so I'm happy to communicate in whichever language you prefer.For the purposes of this algorithm, my interpreter only needs to support parentheses, multiplication and division. (Let's not complicate things with too many operators for the timebeing.)
My token parser is already working, so we have an array of token objects to work with.
Therefore, we can pass an array of token objects as input to the interpreter, and the interpreter should be able to determine which operations are being performed, simplify the expression, reduce the fraction, etc.
For example, if we pass this array of tokens:x*y/x*z
... ultimately my interpreter would be able to reduce the fraction toy/z
since the x's cancel out, and since multiplication takes precedence over division.However, I need to support parentheses to make this practical for a specific enduser application.
Things get complicated when you include parentheses ...
For example:a/(b*c/(d*e/f)*g/h)/i*(j/k)
what a mess! XDRecursion to the rescue!
We can greatly simplify this problem by splitting it into smaller problems.
Since multiplication takes precedence, we can split this into two sections:
a/(b*c/(d*e/f)*g/h)
/i*(j/k)
... and then recursively invoke my interpreter (function) on each of those two sections individually, which would then break them down into even smaller sections, until eventually the problem is simple enough to compute with ease. (Base case = no division operators left)My interpreter is mostly complete, the only thing that is missing is the ability to determine where to split the array of tokens.
In other words, I need to build an algorithm that can look at the placement of operators and parentheses, and figure out which operation occurs last, so I can split the token array in half.Given a sequential array of token objects, I have written some helper functions to produce sequential arrays of integers, representing the indexes of parentheses, multiplication operators, and division operators.
Dim tokenCount As Long 'Total number of tokens in the current input array Dim openParenIndexes() As Long 'Arrays of integers, representing token positions Dim closeParenIndexes() As Long Dim multiplicationIndexes() As Long Dim divisionIndexes() As Long
In the above example (using zerobased indexing):
openParenIndexes
=[2, 7, 22]
closeParenIndexes
=[13, 18, 26]
multiplicationIndexes
=[4, 9, 14, 21]
divisionIndexes
=[1, 6, 11, 16, 19, 24]
Again, these arrays are already populated and ready to use.
My program needs to be able to look at the information in these arrays, and figure out that the division operator at index19
is the final operator to be computed, and therefore the array of tokens should be split at that index.Using this information, what's the best way to begin creating this algorithm?
Should this also be a recursive algorithm?I presume more recursion could possibly make this easier, but I'm having trouble figuring out where to start.

Implementing FloydWarshall in Java
I'm trying to implement the FloydWarshall algorithm and I'm a bit stuck on how to go about it.
I'm taking in an unspecified amount of weighted edges, the input is 3 integers: the source, destination and weight. Example:
1 9 4
1 is source node, 9 is destination node and 4 is weight.
Here is my code below:
import java.util.*; public class FloydWarshall { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); ArrayList<Edge> edges = new ArrayList<Edge>(); while(stdin.hasNext()) { String[] str = stdin.nextLine().split("[\\s,]+"); ArrayList<Integer> inner = new ArrayList<Integer>(); for(int i = 0; i < str.length; i++) { inner.add(Integer.parseInt(str[i])); } list.add(inner); } Graph graph = new Graph(edges.size()); for(int i = 0; i < list.size(); i++) { graph.addEdge(edges, list.get(i).get(0), list.get(i).get(1), list.get(i).get(2)); } } } class Edge { int source; int destination; int weight; public Edge(int source, int destination, int weight) { this.source = source; this.destination = destination; this.weight = weight; } } class Graph { int vertices; int adjMatrix[][]; public Graph(int vertices) { this.vertices = vertices; this.adjMatrix = new int[vertices][vertices]; } public void addEdge(ArrayList<Edge> edges, int source, int destination, int weight) { Edge edge = new Edge(source, destination, weight); edges.add(edge); adjMatrix[source][destination] = weight; } }
I'm trying to implement an adjacency matrix for the FloydWarshall algorithm but I'm having trouble implementing it, I've never used an adjacency matrix so any help would be appreciated!

How should i guarantee consistency in database involving finance transaction operations
I am trying to figure out how to handle consistency in the database. In scenario:
User A has an accounting document in the database include a balance field representing the amount of his current money. (supposed initially he has 100$)
My system has many methods to charge his account.
 Suppose 2 methods occur at the same time, each method charges him for 10$, these steps occur concurrently in below orders:
 Method 1 READ his balance and store in memory (100$)
 Method 2 READ his balance and store in memory (100$) ... some business logics
 Method 1 UPDATE his balance by subtracting variable in memory by 10 (100$  10$) and then save it
 Method 2 UPDATE his balance by subtracting variable in memory by 10 (100$  10$) and then save it
This means he has been charged only 10$ instead of 20$.
I searched this situation a while and can not get it clear (sorry for my stupidity). Really appreciate yours helps to enlighten my featherbrained. :)

How to find partial derivatives of f(x,y) with respect to x and y
Let
f(x,y) = x^5 Sin(xy^2)
, then find partial derivatives off(x,y)
with respect to x and y atx=2
,y=0
. 
Calculate launch angle based on launch direction
I'm working with a setup like this:
Where the vectors all start at transform.position and end as follows:
 Black: transform.position + HandlePosition
 Blue: transform.position + Vector2.right
 White: transform.position  HandlePosition.normalized
The idea is that i can move the red handle around to change the White vector (the launch direction of the object).
Now what I would like to get is the angle between the White and Blue vectors.
I want to get this angle so that I can calculate the trajectory of the object and display a trajectory line.
Given the example shown on the picture, I would expect an angle of about 45 degrees.

Prove that the powerset of a finite set is finite using Coq
While trying to prove some things, I encountered an innocent looking claim that I failed to prove in Coq. The claim is that for a given Finite Ensemble, the powerset is also finite. The statement is given in the Coq code below.
I looked through the Coq documentation on finite sets and facts about finite sets and powersets, but I could not find something that deconstructs the powerset into a union of subsets (such that the
Union_is_finite
constructor can be used). Another approach may be to show that the cardinal number of the powerset is 2^S but here I certainly have no idea how to approach the proof.From Coq Require Export Sets.Ensembles. From Coq Require Export Sets.Powerset. From Coq Require Export Sets.Finite_sets. Lemma powerset_finite {T} (S : Ensemble T) : Finite T S > Finite (Ensemble T) (Power_set T S). Proof. (* I don't know how to proceed. *) Admitted.

How to implement a variance equation with external variable in a GARCH model with rugarch package
I want to fit a GARCH model in R with the following variance equation:
I'm using the rugarchpackage because this is the only package I know of, which allows external variables to be included into GARCH models. Anyway, I don't know which GARCH model I should choose for my variance equation. I tried to figure out which of the valid models mentioned in the documentation would be appropriate but none of them seem to be the right one. I'm little confused because of the exponential part of the equation. Would be happy if somebody could help me out!

Why do I get different outcomes for sm.OLS and sklearn.linear_model although I use the same input?
I am trying to running a regression model with two different functions: OLS from statsmodels.api and linear_regression from sklearn, the output seems to be quite different from each other.
Here is my code:
import statsmodels.api as sm import pandas as pd import matplotlib import scipy.stats as stats import matplotlib.pyplot as plt from patsy import dmatrices from sklearn import linear_model data = pd.read_excel("2001_SCF_Pivot.xlsx") y,x = dmatrices("np.log(RETQLIQ) ~ W_P_ADJ+np.power(W_P_ADJ,2)+np.power(W_P_ADJ,3)+INCOME+np.power(INCOME,2)+WHITE+AGE+EDUC+FEMALE+SINGLE",data = data) LinearRegression = linear_model.LinearRegression() ols = LinearRegression.fit(x,y) sm_prediction = ols.predict(x) model_fit = sm.OLS(y,x) results = model_fit.fit() sklearn_prediction = results.predict(x)
When I scatter the data and add both predictions on the graph while in theory I need to get two plots on each other, the prediction of the two functions seems to be quite different as you can see from the attached image. My question is why do I get different results and what is the right way to do it in this case, thanks a lot in advance!
You can find the related graph here : https://imgur.com/a/OkqCcd1

How to web scrape the NBA table in Python?
With this code, I am only able to get the Atlantic Division but not the other divisions. I can't figure out the HTML tag for the other divisions. Here is the link to the source code from the website I am web scraping: viewsource:https://www.basketballreference.com/leagues/NBA_2019_per_game.html
import os import time from selenium import webdriver from bs4 import BeautifulSoup as soup driver = webdriver.Chrome() driver.get("https://www.basketballreference.com/leagues/NBA_2019_per_game.html") page = soup(driver.page_source, "html.parser") Atlantic = page.find('tr').find_parent().get_text() Central = page.find('tr').find_parent().get_text() #Central = page.find('tr').find_parent().get_text() Southeast = page.find('tr').find_parent().get_text() Northwest = page.find('tr').find_parent().get_text() Pacific = page.find('tr').find_parent().get_text() Southwest = page.find('tr').find_parent().get_text() View_Atlantic = open('Atlantic_Division.txt','a') View_Atlantic.write(Atlantic) View_Atlantic.close() View_Central = open('Central_Division.txt','a') View_Central.write(Central) View_Central.close() View_Southeast = open('Southeast_Division.txt','a') View_Southeast.write(Southeast) View_Southeast.close() View_Northwest = open('Northwest_Division.txt','a') View_Northwest.write(Northwest) View_Northwest.close() View_Pacific = open('Pacific_Division.txt', 'a') View_Pacific.write(Pacific) View_Pacific.close() View_Southwest = open('Southwest_Division.txt', 'a') View_Southwest.write(Southwest) View_Southwest.close() driver.quit() while True: main = input("Which NBA division would you like to view?\n" + '1. Atlantic Division\n2 Central Division\n3 Southeast Division\n4 Northwest Division\n5 Pacific Division\n6 Southwest Division \n7 Quit') if main == '1': View_Atlantic = open('Atlantic_Division.txt' , 'r') lines = View_Atlantic.read() print(lines) View_Atlantic.close() if main == '2': View_Central = open('Central_Division.txt', 'r') lines = View_Central.read() print(lines) View_Central.close() if main == '3': View_Southeast = open('Southeast_Division.txt', 'r') lines = View_Southeast.read() print(lines) View_Southeast.close() if main == '4': View_Northwest = open('NorthwestDivision.txt', 'r') lines = View_Northwest.read() print(lines) View_Northwest.close() if main == '5': View_Pacific = open('Pacific_Division.txt', 'r') lines = View_Pacific.read() print(lines) View_Pacific.close() if main == '6': View_Southwest = open('Southwest_Division.txt','r') lines = View_Southwest.read() print(lines) View_Southwest.close() if main == '7': print('Bye!') break
I want to be able to display the NBA team and the team record by choosing a certain NBA division.

in nueral network , WX+B is linear transformation?
in linear algebra, linear function is called linear transformation
in neural network ,
linear transformation need two condition
f(x+y) = f(x) + f(y)
f(cx) = cf(x)
but
WX+B
is not satisfied these two condition. So, this is my question, evenwx+b
isn't satisfied above two condition, but normally people callwx + b
to linear function. why?thank you. master

Left and right eigenvectors in Julia
I have a general real matrix (i.e. not symmetric or Hermitian, etc.), and I would like to find its right eigenvectors and corresponding left eigenvectors in Julia.
Julia's
eigen
function returns the right eigenvectors only. I can find the left eigenvectors by doingeigen(copy(M'))
but this requires copying the whole matrix and performing the eigendecomposition again, and there is no guarantee that the eigenvectors will be in the same order. (The
copy
is necessary because there is noeigen
method for matrices of typeAdjoint
.)In Python we have
scipy.linalg.eigs
, which can compute the left and right eigenvectors simultaneously in a single pass, which is more efficient and guarantees that they will be in the same order. Is there something similar in Julia? 
how to check if vectors inside a matrix are independent?
Im trying to check if a matrix has vectors that are linearly independent or dependent (linear algebra)
I make a first routine that successfully do the revision, but Im interested that in the linearly dependent sets, make a new check to see if the vectors are so.
Lets say I have the next matrix
value=[1,0,0; 0,1,0; 1,1,0]
I ran the first check with the next code (entered via a table)
value=get(handles.uitable1,'Data'); value=str2double(value); set(handles.text5,'String',num2str(value)); R=rref(value); %Reduced matrix RangS=rank(R) [r, c] = size(value) if (rank(value))==r set(handles.text3,'String',('linearly independent')); else set(handles.text3,'String',('dependent system'));
and applying to the initial matrix it is linearly dependent.
But these condition is using the whole matrix, then which would be a new condition to see if the vectors are LD/LI, taking row by row when it falls only in the case of dependent system?