performance tuning – Writing compiled functions as fast as Python’s Numba

I want to write some code to simulate a damped oscillator that is just as fast as code written using Numba’s @njit decorator. I’ve written the mathematica code and it is 20-40x slower than the python code written by YouTuber Jack of Some.

Here is the code from Jack of Some’s video on speeding up Python code with Numba; I’ve changed it a bit to run in just one jupyter cell:

import numpy as np
from numba import jit, njit, types, vectorize

@njit
def friction_fn(v, vt):
    if v > vt:
        return - v * 3
    else:
        return - vt * 3 * np.sign(v)

@njit
def simulate_spring_mass_funky_damper(x0, T=10, dt=0.0001, vt=1.0):
    times = np.arange(0, T, dt)
    positions = np.zeros_like(times)

    v = 0
    a = 0
    x = x0
    positions(0) = x0/x0

    for ii in range(len(times)):
        if ii == 0:
            continue
        t = times(ii)
        a = friction_fn(v, vt) - 100*x
        v = v + a*dt
        x = x + v*dt
        positions(ii) = x/x0
    return times, positions

_ = simulate_spring_mass_funky_damper(0.1)

%time _ = simulate_spring_mass_funky_damper(0.1)

The output is

CPU times: user 1.38 ms, sys: 337 µs, total: 1.72 ms
Wall time: 1.72 ms

vs my Mathematica code

ClearAll(friction, simulateSpring, jacksSpring);

friction = Compile({{v, _Real}, {vt, _Real}},
   If(v > vt,
        -v*3.0,
        -vt*3.0*Sign(v))
   );

simulateSpring = 
  Compile({{x0, _Real}, {t, _Real}, {dt, _Real}, {vt, _Real}},
   
   Module({(Tau), times, positions, v = 0.0, a = 0.0, x = x0},
    
    (Tau) = t;
    times = Range(0.0, t, dt);
    positions = ConstantArray(0.0, Length@times);
    positions((1)) = x0/x0;
    
    Do(
        (Tau) = times((i));
        a = friction(v, vt) - 100*x;
        v = v + a*dt;
        x = x + v*dt;
        positions((i)) = x/x0;
     ,
     {i, 2, Length@times});
    {times, positions}
    )
   );
jacksSpring(x_) := simulateSpring(x, 10.0, 0.0001, 1.0);

Print("CPU time: ", Timing(jacksSpring(0.1))((1))*1000, " ms")

from which we have

CPU time: 27.703 ms

Control functions in mobile Youtube iFrame

Is it possible to add controls into mobile version of YouTube iFrame player?
With controls I mean being able to change the speed and quality of the video, or turn on the subtitles.

functions – Approximation of the convolution operator

I am trying to convolve two functions:

$f(x) = e^{-h t}$

$g(x) = e^{-(e^{-t})^2}$

$(f*g)(x) = int_{0}^{t} f(t-tau)g(tau) dx = int_{0}^{t} e^{t-tau} e^{-(e^{-tau})^2} dx$

Unfortunately, neither Mathematica nor Maple mastered this integral.

Are there any ways to approximate the convolution operator and get at least an approximate solution?

I would be grateful for any advice and help.

how to find convolution between two functions?

I have some problems with the convolution between two functions. i want to find the convolution of two models. The two models are given by gd and gl
The parameters are :

ws = 4.3266; wd = 4.7556; wss = 0.205; wdd = 0.038; q1 = 0.15177;

eps1 = 2*(t - ws)/(2*wss);
    eps2 = 2*(t - wd)/(wdd);
   

I want to get a Voigt function by using the convolution of the above two functions,

 gd(t_) = 1/(1 + eps1^2);
    gl(t_) = 1 - ((eps2 + q1)^2)/(1 + eps2^2);

Voi(t_) := NIntegrate(gd(ts)*gl(t - ts), {ts, -Infinity, Infinity})

fitplot = Plot({gd(t), gl(t), Voi(t_)}, {t, 4, 5}, PlotRange -> All, PlotStyle -> {Green, Blue, Red})

My data is

{{4.2056}, {4.2066}, {4.2076}, {4.2086}, {4.2096}, {4.2106}, {4.2116}, 
{4.2126}, {4.2136}, {4.2146}, {4.2156}, {4.2166}, {4.2176}, {4.2186}, 
{4.2196}, {4.2206}, {4.2216}, {4.2226}, {4.2236}, {4.2246}, {4.2256}, 
{4.2266}, {4.2276}, {4.2286}, {4.2296}, {4.2306}, {4.2316}, {4.2326}, 
{4.2336}, {4.2346}, {4.2356}, {4.2366}, {4.2376}, {4.2386}, {4.2396}, 
{4.2406}, {4.2416}, {4.2426}, {4.2436}, {4.2446}, {4.2456}, {4.2466}, 
{4.2476}, {4.2486}, {4.2496}, {4.2506}, {4.2516}, {4.2526}, {4.2536}, 
{4.2546}, {4.2556}, {4.2566}, {4.2576}, {4.2586}, {4.2596}, {4.2606}, 
{4.2616}, {4.2626}, {4.2636}, {4.2646}, {4.2656}, {4.2666}, {4.2676}, 
{4.2686}, {4.2696}, {4.2706}, {4.2716}, {4.2726}, {4.2736}, {4.2746}, 
{4.2756}, {4.2766}, {4.2776}, {4.2786}, {4.2796}, {4.2806}, {4.2816}, 
{4.2826}, {4.2836}, {4.2846}, {4.2856}, {4.2866}, {4.2876}, {4.2886}, 
{4.2896}, {4.2906}, {4.2916}, {4.2926}, {4.2936}, {4.2946}, {4.2956}, 
{4.2966}, {4.2976}, {4.2986}, {4.2996}, {4.3006}, {4.3016}, {4.3026}, 
{4.3036}, {4.3046}, {4.3056}, {4.3066}, {4.3076}, {4.3086}, {4.3096}, 
{4.3106}, {4.3116}, {4.3126}, {4.3136}, {4.3146}, {4.3156}, {4.3166}, 
{4.3176}, {4.3186}, {4.3196}, {4.3206}, {4.3216}, {4.3226}, {4.3236}, 
{4.3246}, {4.3256}, {4.3266}, {4.3276}, {4.3286}, {4.3296}, {4.3306}, 
{4.3316}, {4.3326}, {4.3336}, {4.3346}, {4.3356}, {4.3366}, {4.3376}, 
{4.3386}, {4.3396}, {4.3406}, {4.3416}, {4.3426}, {4.3436}, {4.3446}, 
{4.3456}, {4.3466}, {4.3476}, {4.3486}, {4.3496}, {4.3506}, {4.3516}, 
{4.3526}, {4.3536}, {4.3546}, {4.3556}, {4.3566}, {4.3576}, {4.3586}, 
{4.3596}, {4.3606}, {4.3616}, {4.3626}, {4.3636}, {4.3646}, {4.3656}, 
{4.3666}, {4.3676}, {4.3686}, {4.3696}, {4.3706}, {4.3716}, {4.3726}, 
{4.3736}, {4.3746}, {4.3756}, {4.3766}, {4.3776}, {4.3786}, {4.3796}, 
{4.3806}, {4.3816}, {4.3826}, {4.3836}, {4.3846}, {4.3856}, {4.3866}, 
{4.3876}, {4.3886}, {4.3896}, {4.3906}, {4.3916}, {4.3926}, {4.3936}, 
{4.3946}, {4.3956}, {4.3966}, {4.3976}, {4.3986}, {4.3996}, {4.4006}, 
{4.4016}, {4.4026}, {4.4036}, {4.4046}, {4.4056}, {4.4066}, {4.4076}, 
{4.4086}, {4.4096}, {4.4106}, {4.4116}, {4.4126}, {4.4136}, {4.4146}, 
{4.4156}, {4.4166}, {4.4176}, {4.4186}, {4.4196}, {4.4206}, {4.4216}, 
{4.4226}, {4.4236}, {4.4246}, {4.4256}, {4.4266}, {4.4276}, {4.4286}, 
{4.4296}, {4.4306}, {4.4316}, {4.4326}, {4.4336}, {4.4346}, {4.4356}, 
{4.4366}, {4.4376}, {4.4386}, {4.4396}, {4.4406}, {4.4416}, {4.4426}, 
{4.4436}, {4.4446}, {4.4456}, {4.4466}, {4.4476}, {4.4486}, {4.4496}, 
{4.4506}, {4.4516}, {4.4526}, {4.4536}, {4.4546}, {4.4556}, {4.4566}, 
{4.4576}, {4.4586}, {4.4596}, {4.4606}, {4.4616}, {4.4626}, {4.4636}, 
{4.4646}, {4.4656}, {4.4666}, {4.4676}, {4.4686}, {4.4696}, {4.4706}, 
{4.4716}, {4.4726}, {4.4736}, {4.4746}, {4.4756}, {4.4766}, {4.4776}, 
{4.4786}, {4.4796}, {4.4806}, {4.4816}, {4.4826}, {4.4836}, {4.4846}, 
{4.4856}, {4.4866}, {4.4876}, {4.4886}, {4.4896}, {4.4906}, {4.4916}, 
{4.4926}, {4.4936}, {4.4946}, {4.4956}, {4.4966}, {4.4976}, {4.4986}, 
{4.4996}, {4.5006}, {4.5016}, {4.5026}, {4.5036}, {4.5046}, {4.5056}, 
{4.5066}, {4.5076}, {4.5086}, {4.5096}, {4.5106}, {4.5116}, {4.5126}, 
{4.5136}, {4.5146}, {4.5156}, {4.5166}, {4.5176}, {4.5186}, {4.5196}, 
{4.5206}, {4.5216}, {4.5226}, {4.5236}, {4.5246}, {4.5256}, {4.5266}, 
{4.5276}, {4.5286}, {4.5296}, {4.5306}, {4.5316}, {4.5326}, {4.5336}, 
{4.5346}, {4.5356}, {4.5366}, {4.5376}, {4.5386}, {4.5396}, {4.5406}, 
{4.5416}, {4.5426}, {4.5436}, {4.5446}, {4.5456}, {4.5466}, {4.5476}, 
{4.5486}, {4.5496}, {4.5506}, {4.5516}, {4.5526}, {4.5536}, {4.5546}, 
{4.5556}, {4.5566}, {4.5576}, {4.5586}, {4.5596}, {4.5606}, {4.5616}, 
{4.5626}, {4.5636}, {4.5646}, {4.5656}, {4.5666}, {4.5676}, {4.5686}, 
{4.5696}, {4.5706}, {4.5716}, {4.5726}, {4.5736}, {4.5746}, {4.5756}, 
{4.5766}, {4.5776}, {4.5786}, {4.5796}, {4.5806}, {4.5816}, {4.5826}, 
{4.5836}, {4.5846}, {4.5856}, {4.5866}, {4.5876}, {4.5886}, {4.5896}, 
{4.5906}, {4.5916}, {4.5926}, {4.5936}, {4.5946}, {4.5956}, {4.5966}, 
{4.5976}, {4.5986}, {4.5996}, {4.6006}, {4.6016}, {4.6026}, {4.6036}, 
{4.6046}, {4.6056}, {4.6066}, {4.6076}, {4.6086}, {4.6096}, {4.6106}, 
{4.6116}, {4.6126}, {4.6136}, {4.6146}, {4.6156}, {4.6166}, {4.6176}, 
{4.6186}, {4.6196}, {4.6206}, {4.6216}, {4.6226}, {4.6236}, {4.6246}, 
{4.6256}, {4.6266}, {4.6276}, {4.6286}, {4.6296}, {4.6306}, {4.6316}, 
{4.6326}, {4.6336}, {4.6346}, {4.6356}, {4.6366}, {4.6376}, {4.6386}, 
{4.6396}, {4.6406}, {4.6416}, {4.6426}, {4.6436}, {4.6446}, {4.6456}, 
{4.6466}, {4.6476}, {4.6486}, {4.6496}, {4.6506}, {4.6516}, {4.6526}, 
{4.6536}, {4.6546}, {4.6556}, {4.6566}, {4.6576}, {4.6586}, {4.6596}, 
{4.6606}, {4.6616}, {4.6626}, {4.6636}, {4.6646}, {4.6656}, {4.6666}, 
{4.6676}, {4.6686}, {4.6696}, {4.6706}, {4.6716}, {4.6726}, {4.6736}, 
{4.6746}, {4.6756}, {4.6766}, {4.6776}, {4.6786}, {4.6796}, {4.6806}, 
{4.6816}, {4.6826}, {4.6836}, {4.6846}, {4.6856}, {4.6866}, {4.6876}, 
{4.6886}, {4.6896}, {4.6906}, {4.6916}, {4.6926}, {4.6936}, {4.6946}, 
{4.6956}, {4.6966}, {4.6976}, {4.6986}, {4.6996}, {4.7006}, {4.7016}, 
{4.7026}, {4.7036}, {4.7046}, {4.7056}, {4.7066}, {4.7076}, {4.7086}, 
{4.7096}, {4.7106}, {4.7116}, {4.7126}, {4.7136}, {4.7146}, {4.7156}, 
{4.7166}, {4.7176}, {4.7186}, {4.7196}, {4.7206}, {4.7216}, {4.7226}, 
{4.7236}, {4.7246}, {4.7256}, {4.7266}, {4.7276}, {4.7286}, {4.7296}, 
{4.7306}, {4.7316}, {4.7326}, {4.7336}, {4.7346}, {4.7356}, {4.7366}, 
{4.7376}, {4.7386}, {4.7396}, {4.7406}, {4.7416}, {4.7426}, {4.7436}, 
{4.7446}, {4.7456}, {4.7466}, {4.7476}, {4.7486}, {4.7496}, {4.7506}, 
{4.7516}, {4.7526}, {4.7536}, {4.7546}, {4.7556}, {4.7566}, {4.7576},
{4.7586}, {4.7596}, {4.7606}, {4.7616}, {4.7626}, {4.7636}, {4.7646}, 
{4.7656}, {4.7666}, {4.7676}, {4.7686}, {4.7696}, {4.7706}, {4.7716}, 
{4.7726}, {4.7736}, {4.7746}, {4.7756}, {4.7766}, {4.7776}, {4.7786}, 
{4.7796}, {4.7806}, {4.7816}, {4.7826}, {4.7836}, {4.7846}, {4.7856}, 
{4.7866}, {4.7876}, {4.7886}, {4.7896}, {4.7906}, {4.7916}, {4.7926}, 
{4.7936}, {4.7946}, {4.7956}, {4.7966}, {4.7976}, {4.7986}, {4.7996}, 
{4.8006}, {4.8016}, {4.8026}, {4.8036}, {4.8046}, {4.8056}, {4.8066}, 
{4.8076}, {4.8086}, {4.8096}, {4.8106}, {4.8116}, {4.8126}, {4.8136}, 
{4.8146}, {4.8156}, {4.8166}, {4.8176}, {4.8186}, {4.8196}, {4.8206}, 
{4.8216}, {4.8226}, {4.8236}, {4.8246}, {4.8256}, {4.8266}, {4.8276}, 
{4.8286}, {4.8296}, {4.8306}, {4.8316}, {4.8326}, {4.8336}, {4.8346}, 
{4.8356}, {4.8366}, {4.8376}, {4.8386}, {4.8396}, {4.8406}, {4.8416}, 
{4.8426}, {4.8436}, {4.8446}, {4.8456}, {4.8466}, {4.8476}, {4.8486}, 
{4.8496}, {4.8506}, {4.8516}, {4.8526}, {4.8536}, {4.8546}, {4.8556}, 
{4.8566}, {4.8576}, {4.8586}, {4.8596}, {4.8606}, {4.8616}, {4.8626}, 
{4.8636}, {4.8646}, {4.8656}, {4.8666}, {4.8676}, {4.8686}, {4.8696}, 
{4.8706}, {4.8716}, {4.8726}, {4.8736}, {4.8746}, {4.8756}, {4.8766}, 
{4.8776}, {4.8786}, {4.8796}, {4.8806}, {4.8816}, {4.8826}, {4.8836}, 
{4.8846}, {4.8856}, {4.8866}, {4.8876}, {4.8886}, {4.8896}, {4.8906}, 
{4.8916}, {4.8926}, {4.8936}, {4.8946}, {4.8956}, {4.8966}, {4.8976}, 
{4.8986}, {4.8996}, {4.9006}, {4.9016}, {4.9026}, {4.9036}, {4.9046},
{4.9056}, {4.9066}, {4.9076}, {4.9086}, {4.9096}, {4.9106}, {4.9116}, 
{4.9126}, {4.9136}, {4.9146}, {4.9156}, {4.9166}, {4.9176}, {4.9186}, 
{4.9196}, {4.9206}, {4.9216}, {4.9226}, {4.9236}, {4.9246}, {4.9256}, 
{4.9266}, {4.9276}, {4.9286}, {4.9296}, {4.9306}, {4.9316}, {4.9326}, 
{4.9336}, {4.9346}, {4.9356}, {4.9366}, {4.9376}, {4.9386}, {4.9396}, 
{4.9406}, {4.9416}, {4.9426}, {4.9436}, {4.9446}, {4.9456}, {4.9466}, 
{4.9476}, {4.9486}, {4.9496}, {4.9506}, {4.9516}, {4.9526}, {4.9536}, 
{4.9546}, {4.9556}, {4.9566}, {4.9576}, {4.9586}, {4.9596}, {4.9606},
{4.9616}, {4.9626}, {4.9636}, {4.9646}, {4.9656}, {4.9666}, {4.9676}, 
{4.9686}, {4.9696}, {4.9706}, {4.9716}, {4.9726}, {4.9736}, {4.9746}, 
{4.9756}, {4.9766}, {4.9776}, {4.9786}, {4.9796}, {4.9806}};

I can’t find the convolution, So, how to do this convolution in Mathematica?
The result is

enter image description here

functions – WordPress – How to show my post type TEAM in dinamic way?

Hi everyone,

I am starting now to develoo my first wordpress Theme and please, I need your help with code.

With this code I created my post type Team, the problem is to show them, I do not know how to do it.
If it’s possible, I wish to create some easy option for users in Bakery page builder to add it in a row with the option for slider. How can I do it ?

Thanks for any information

function create_post_type() {

register_post_type( 'Team',
    array(
    'labels' => array(
        'name' => __( 'Team' ),
        'singular_name' => __( 'Team' ),
        'add_new' => __( 'Add Member' ),
        'add_new_item' => __( 'Add New Member' ),
        'featured_image' => __( 'Add Photo' ),
        'edit_item' => __( 'Edit Member' )
    ),
    'public' => true,
    'menu_icon' => 'dashicons-universal-access',
    'has_archive' => true,
    'rewrite' => array('slug' => 'Team'),
    'supports' => array('title', 'editor','custom-fields', 'thumbnail'),
    )
);

}
add_action( ‘init’, ‘create_post_type’ );

functions – Override the filter from plugin in child theme

That’s not how filters work. Much like a filter in real life, something goes in, it gets changed, and the result goes out. Filters always take in a parameter and return something. They’re an opportunity to change something

So if we take this:

$review_num_fetch = apply_filters('tourmaster_review_num_fetch', 5);

This means the plugin uses 5, but it has given other code an opportunity to change this to another value. It does this by passing it to the tourmaster_review_num_fetch filter then using the result. So hook into that filter and return a different value to change it.

When that line runs, WP looks at all the filters registered, passes the value, and then replaces it with what the filter returned.

This is what a filter that adds 1 to the value looks like:

function addone( $in ) {
    $out = $in + 1;
    return $out;
}

And this is how you would add it:

add_filter( 'tourmaster_review_num_fetch', 'addone' );

With that you should have all the knowledge needed for implementing your solution

functions – Replace word in “the_content” only for index.php

I have some posts with video tag that contains autoplay (they should autoplay when you access the post page).

I am using the the_content to show these videos on index page.

The problem is that when you access the home page, all the videos start playing.

I have used the following function to remove it:

function replace_ap($text){
    $replace = array(
        'autoplay=""' => ''
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
}
 
add_filter('the_content', 'replace_ap');

but now the “autoplay” attribute is removed from the post pages also.

How can I remove it only from the index ?

My thought was to create a function that gets the content and add the filter to this function, then call it from index.php instead of “the_content”, but I don’t know how or if it’s possible

Thank you!

plotting – Plot time dependent 3D heat equation solution with functions like Plot3D +Manipulate (or Graph3D) but using cylindrical coordinates

I have a time dependant heat diffusion equation here and I would like to plot the result of NDSolveValue.

Here is the code I am using :



ClearAll("Global`*")
r0 = 0.5;
h = 1;

eq1 = D(u(t, r, z), 
    t) - (D(u(t, r, z), r, r) + 1/r*D(u(t, r, z), r) + 
     D(u(t, r, z), z, z));

ic = {u(0, r, z) == 1};

bc = {u(t, r0, z) == 0, 
   u(t, 1, z) == 0, (D(u(t, r, z), r) /. r -> r0) == 
    0, (D(u(t, r, z), r) /. r -> 1) == 1, u(t, r, 0) == u(t, r, h)};


sol = NDSolveValue({eq1 == 0, ic, bc}, 
  u(t, r, z), {t, 0, 10}, {r, r0, 1}, {z, 0, h}, 
  MaxSteps -> Infinity , MaxStepFraction -> 1/10)

Manipulate(Plot3D(sol(t, r, z), {t, 0, 10}, {r, r0, 1}), {z, 0, 1})

So I end up with something like this :
enter image description here

The thing is, I would like to have the function plot over a cylinder centered around r=0 instead of plotting the function in a box with 3 orthogonal axis like shown in these answers here or there.

Therefore i would like to ask, is it possible to have a plot over a cylinder, maybe with with a color function….Is it possible to plot things using cylindrical coordinates in mathematica ?

Thank you in advance for any answer.

General class of functions satisfying growth condition on a given functional :

Consider analytic function $f$ which is positive real valued for positive real domain .

I want to know if there’s a general class of function $f$ which satisfies the following growth of given functional :

$$int_0^infty frac{f(x + iy) − f(x − iy)}{e^{2πy}-1} dy=oleft(int_1^x f(t) dtright) $$ ?

(Sorry for such a generality and straight-forwardness of the question )

postgresql – How to use aggregate functions on each array in a table, in Postgres?

Imagine I have a table like:

games (
    scores INTEGER()
)

And it had values

{1,2}
{3,4}
{}

I’d like to do arbitrary aggregations on that array.

For example, how would I do SELECT SUM(scores) AS total FROM games? Or an average? Or a max?

I’d like to get out:

3
7 
0

I tried SELECT SUM(UNNEST(scores)) AS total FROM games but that gave ERROR: aggregate function calls cannot contain set-returning function calls.