## compile – Accelerate the distance function of two point sets

I have two point sets `point` and `impPoint`. I want to calculate a distance function from each element in the `point` set to all elements in `impoint` sets. I have written a compiled version of the function, but its speed is a little slow. Is there any way to speed up the code?

``````w=0.1;
qs=0.1;
n=200;
nn=n*n;
nimp=0.1;
Nimp=IntegerPart(nimp*nn);
point=RandomReal(1,{nn,2});
impPoint=RandomSample(point,Nimp);
pm=ReplacePart(Table(1,Nimp),(RandomSample(Range(1,Nimp,1),Nimp/2)//Partition(#,1)&)->-1);
cf=Compile({{w,_Real},{qs,_Real},{pm,_Real,1},{Nimp,_Real},{impPoint,_Real,2},{point,_Real,1}},Total(Table(w/qs^2*Exp(-(((point((1))-impPoint((i,1)))^2+(point((2))-impPoint((i,2)))^2))/qs^2),{i,1,Nimp})*pm),CompilationTarget->"C",Parallelization->True,RuntimeAttributes->{Listable},RuntimeOptions->"Speed");
AbsoluteTiming(cf(w,qs,pm,Nimp,impPoint,point)//Total)
``````

In fact, `n` takes 5000, so I don’t recommend using `DistanceMatrix`, because it saves time but costs a lot of memory.

Posted on

## differential equations – Passing Product function through NIntegrate?

I have a set of intervals, Times1, that I have calculated using the following code. This code works well on its own.

``````a = 3/100; b = d = 1; c = 1/2; K = HMax = 2000;NCritical = 100; SearchIncrements = 100; FinalSearchTime = 5000; InterpolatingFinalTime = 30000;

Needs("RootSearch`");

System3(g1_, t0_) :=Once(Y(t) /. NDSolve({X'(t) == X(t)*b*H(t)/HMax - X(t)*d*(X(t)+Y(t))/K,

Y'(t) == Y(t)*b*(1 - c*g1)*H(t)/HMax + Y(t)b(1 - c*g1)g1(1 - H(t)/HMax) - Y(t)d(X(t) + Y(t))/K,

H'(t) == Y(t)*b*(1 - c*g1)*(1 - H(t)/HMax)*g1 - a*H(t),

X(0) == ((b E^((b E^(-a t0) K t0)/HMax) K^2)/(d E^(a t0) (-1 + E^((b E^(-a t0) K t0)/HMax)) HMax + b K)), Y(0) == 1, H(0) == Exp(-a*t0)*HMax},

{X(t), Y(t), H(t)}, {t, 0,InterpolatingFinalTime}) // First);

Times1(g1_, t0_) := Block({},If(0.2 <= g1 <= 1,Times2 = Drop(Flatten(Table(RootSearch(System3(g1, t0) == NCritical,

{t, i*SearchIncrements, (i + 1)*SearchIncrements}), {i, 0,
FinalSearchTime/SearchIncrements, 1})), 1)((All, 2));

Intervals1 = Partition(Times2, 2, 2)))
``````

I am trying to solve a numerical integral, P1. This integral is a function of another numerical integral, P2, that is a function of `Product`. This product is performed j times, where j is the number of elements in Times1.

``````System1 = ParametricNDSolve({X'(t) == X(t)*b - a*X(t)^2/K,X(0) == Exp(-a*t0)*K}, {X}, {t, 0,1000}, {g, t0});

System2 = ParametricNDSolve({X'(u) == X(u)*b - a*X(u)^2/K,X(0) == Exp(-a*t0)*K}, {X}, {u, 0,1000}, {g, t0});

P1(g_, t0_?NumericQ) := Module({B1, P2},

P2(t1_?NumericQ) := Piecewise({{Product(

B1(u_?NumericQ) :=NIntegrate(b*(1 - c*g)*(H(g, t0)(t) /. System1)/HMax -d*((X(g, t0)(t) /. System1))/K,

{t, Times1(g, t0)((j, 1)), u},Method -> {Automatic, "SymbolicProcessing" -> 0});

1 - (1 - 2/(1 + NIntegrate(Exp(-B1(u))*(b*(1 - c*g)),{u, Times1(g, t0)((j, 1)), Times1(g, t0)((j, 2))},

Method -> {Automatic, "SymbolicProcessing" -> 0}))),{j, 1, Length(Times1(g, t0))}),

0.2 <= g <= 1}, {0, g < 0.2}}); P2(t0))

P1(0.6, 80)
``````

When I try this, I receive an error that says “The expression j cannot be used as a part specification.” Is there a solution to this problem? Any help would be greatly appreciated!

Alex

Posted on

## programming languages – what is the formula used in the scale_quantile function in R?

I want to know what formula is being is in the package scale_quantile by the dynutils package. I want to check if I can achieve the same results as with manual calculations like this

``````# Air quality dataset
data = datasets::airquality

# Manual calculation
dat = data\$Wind

#Calcuate quantile
q_995 = quantile(dat, 0.995)
q_005 = quantile(dat, 0.005)

#Filtering the data based on the quantile value
dat = dat(dat >= q_005 & dat <= q_995)

MEAN = mean(dat)
SD = sd(dat)

manual = (dat - MEAN)/SD
``````

. This is the site I referred for documentation, Documentation of scale_quantile

Posted on

## plotting – How to specify tick marks for each axis in the function of ParallelAxisPlot?

I try to use the general code, i.e., Ticks -> {{x1,label1},{x2,label2},…}, to specify tick marks in the function of ParallelAxisPlot but it doesn’t work. Is there any other ways to specify tick marks in the function of ParallelAxisPlot? Many thanks!

Here is a toy example.

``````ParallelAxisPlot[{{6, 13, 5, 9}, {10, 5, 8, 3}, {2, 8, 3, 1}}, PlotRange -> {{0, 12}, {3, 13}, {2, 10}, {0, 10}}, Ticks -> Automatic]
`````` I try to use the general code, i.e., Ticks -> {{x1,label1},{x2,label2},…}, to specify the tick marks. It gives me a result like this. The code, i.e., {a, b, c, d, e} works with no problem. It will put the labels on the side of the ticks. However, the code, i.e., {13, “here”} has an issue now. I want to put a label “here” on the side of 13 of the 2nd axis. I don’t know how to make it work in the function of ParallelAxisPlot.

In addition, is there any way to manually adjust the intervals of the ticks in the given PlotRange？ For example, the 1st axis has 6 intervals but the 4th axis has 5 intervals. These intervals are divided automatically. In fact, the general code, i.e., Ticks -> {{x1,label1},{x2,label2},…} works for me to manually adjust such intervals in other plotting functions but it doesn’t work in this function.

``````ParallelAxisPlot[{{6, 13, 5, 9}, {10, 5, 8, 3}, {2, 8, 3, 1}}, PlotRange -> {{0, 12}, {3, 13}, {2, 10}, {0, 10}}, Ticks -> {Automatic, {{13, "here"}}, {a, b, c, d, e}, Automatic}]
`````` Posted on

## java – Daily coding problem: Job scheduler invoking a function after specific time

This is a problem from Daily Coding Problem. I have implemented it in Java.
For a function `f` I have used `Runnable` interface.

Problem:

This problem was asked by Apple.

Implement a job scheduler which takes in a function `f` and an integer `n`, and calls `f` after `n` milliseconds.

Solution:

``````import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

class Ideone {

interface JobScheduler {
void schedule(final Runnable f, int n);
}

static class JobSchedulerImpl implements JobScheduler {
private final AtomicInteger idValue = new AtomicInteger(0);
private final Map<Integer, Thread> jobs = new ConcurrentHashMap<>();

private int getNextId() {
return idValue.incrementAndGet();
}

@Override
public void schedule(final Runnable f, final int n) throws IllegalArgumentException {
if (n < 0) {
throw new IllegalArgumentException("n cannot be nagative");
}
int id = getNextId();
@Override
public void run() {
try {
f.run();
jobs.remove(id);
} catch (InterruptedException e) {
jobs.remove(id);
}
}
};
jobs.put(id, job);
job.start();
}
}

public static void main(String() args) {
JobScheduler jobScheduler = new JobSchedulerImpl();
jobScheduler.schedule(new Runnable() {
@Override
public void run() {
System.out.println("Job1 launched at: " + System.currentTimeMillis());
}
}, 10000);
jobScheduler.schedule(new Runnable() {
@Override
public void run() {
System.out.println("Job2 launched at: " + System.currentTimeMillis());
}
}, 1000);
}
}
$$```$$
``````

Posted on

## javascript – Although I am using Duplicate array, Array.map() function changing my original array

I have an array like :

``````export const switches = (
{ id: 1, switchName: 'HS - 3015', operation: 'Auto Start GA-3001 S', isActive: true },
{ id: 2, switchName: 'HS - 3016', operation: 'FSLL - 3001 (Pass - 1)', isActive: false },
{ id: 3, switchName: 'HS - 3017', operation: 'FSLL - 3002 (Pass - 2)', isActive: true }
);
``````

In my component I added a additional property `value` which is boolean and which is used for control switch.

In a function I first duplicate this array and than modify this duplicate array that is `value: true/false` to `value: 'normal/bypass'`. The big issue is I am not getting my original array. I am always get the modified array.
What is the problem, I can’t figure out.

Switch change and Submit function like this:

``````  const onSwitchChange = (e, switchId) => {
const { checked } = e.target;
const oldState = (...state);
const updatedState = oldState.map(s => {
if (s.id === switchId) {
s('value') = checked;
}
return s;
});
setState(updatedState);
};

const onSubmit = () => {
const oldData = (...state);
const data = oldData.map(item => {
item.value = item.value === true ? 'Bypass' : 'Normal';
return item;
});
document.getElementById('jsonData').textContent = JSON.stringify(data, null, 2);
};
``````

Find in codesandbox: https://codesandbox.io/s/switch-control-4ovyk

Posted on

## functional analysis – Recovering the objective function from the solution of a linear programming problem

There is a compact, convex space $$T subset mathbb{R}^N$$ of parameters and a finite set $$X$$ of cardinality $$N$$. I know that each $$x_1, …,x_i, …, x_N$$ is associated with a value $$c_{i}(t_{i})$$: the $$i$$-th entry of the $$t$$ vector shifts around the value of selecting each $$x_i$$. This generates a problem of choosing a vector of probabilities $$(P_{x_1}, …, P_{x_N})$$ to maximize $$sum_{i} P_{x_i} c_i(t_i)$$ subject to additional linear constraints on the selection probabilities $$P_{x_i}$$ for each item $$i$$ that are independent of $$t$$.

I have an upper hemi-continuous correspondence $$P^*:Trightarrow X$$ that is the solution to the linear programming problem. I need to recover the objective function $${c_i(t_i)}_{t in T, i in {1,…,N}}$$ that generated $$P^*(t)$$, and it must be continuous and bounded. By “recover”, I don’t mean it literally in the sense of an algorithm that I could run on a computer, but instead as an existence claim: “there exists a function $$c(t)$$ such that $$P^*$$ is the argmax correspondence”.

I can almost do it using an approach based on Michael’s Selection Theorem and the Separating Hyperplane Theorem, but from that I can only conclude that I can recover continuous functions $$c_i(t)$$, but not $$c_i(t_i)$$. I have tried adding additional assumptions about the form of $$P^*$$, but I just can’t find a straightforward way to eliminate the dependence of $$c_i(t)$$ on the $$t_j$$‘s besides $$t_i$$. I can make additional assumptions about $$P^*$$, and I have, but I continually run into the same kinds of meta-problems about existence of certain kinds of continuous functions.

Another thing to say is, if $$T$$ were finite, I could use Brouwer’s fixed point theorem to find what I’m looking for through a process that continuously adjusts guesses for the $$c$$‘s. I was thinking about (a) taking a finite subset of $$T$$ and letting the set get large, and trying to argue that it converges in some sense to a continuous function or (b) trying to go straight to Schauder’s FPT and argue that the set of functions I’m interested in is compact in the way required by the theorem. I tried yet another approach based on solving infinite systems of inequalities, but that doesn’t guarantee a continuous solution. But if there’s an easy answer to this question, I don’t want to do any of that, obviously — I’ve already wasted too much time on this piece of a larger puzzle.

I am just stuck and mostly looking for references, to a theorem or paper or book or researcher who does this kind of “inverse optimization”. This feels like the kind of question where someone with a good background in Constrained Optimization or Operations Research or Convex Analysis will roll their eyes and say, “That’s just Foo’s theorem, and there’s an obvious trick based on duality.” Great, sure, I love duality, tell me about the Foo.

Posted on

## Non trivial zeros of Dirichlet L function

So I was wondering if I can find some of the non trivial zeros of the Dirichlet-L function using mathematica. What I found out was there is a code `ZetaZero[]` which gives the non trivial zeros for the Riemann zeta function.

Is there a similar code for the Dirichlet-L function? I think not, as I wasn’t able to find something like that. So, I am wondering how should I try to find them.

I tried plotting the graphs for some particular Dirichel-L functions, for example i tried to use

`Plot[{Re[DirichletL[5, 3, 1/2 + I t]], Im[DirichletL[5, 3, 1/2 + I t]]}, {t, 0, 20}, PlotLegends -> "Expressions"]`

From here I get a rough idea about the location of zeros on the line $$sigma=1/2$$. But, I want the numerical values of $$t$$ which is where the graphs of both real and imaginary parts become zero in the graph.

Any ideas how should I go with this?

Posted on

## How to properly write a zeta function

enter image description hereI’m trying to make sense of what I’m writing in an effort to prove/disprove/learn what the zeta function does.. any input helps 😀

## Continuity of function defined as \$ ]t[ := n\$ if \$n-1 leq t leq n , nin mathbb{Z}\$

How to prove a function $$] · [ : mathbb{R} rightarrow mathbb{R}$$ defined as:

$$]t[ := n$$ if $$n-1 < t leq n , nin mathbb{Z}$$ is not continuous.

Hint: Check upper or lower semi-coninuity.

Posted on