## algorithms – Converting a function with a single parameter to a function with several parameters

I recently solved algorithm issues and a scheme that I have observed in some problems is as follows:

Given a string or a list, perform an aggregation operation on each of its elements. Here, in each of these elements, we apply a certain recurrence to solve it.

An example of one of these problems is shown below.

Problem: Since n integers return the total number of binary search trees that can be trained using n integers

To solve this problem, I define a recurrence relation as follows:

``````f(n) = 1 // if n = 0
f(n) = ∑ f(i) * f(n-i-1) where 0 <= i <= n-1
``````

It works and I get the correct answer but I want to change the function a bit.

Instead of expressing function in terms of `f(n)` I want to express it in terms of `f(n, i)` so that I can remove the summons. However, I cannot do it properly.

Coded

My code for solving the problem by defining the recurrence in terms of f (n) is as follows: (I know it can be optimized by DP but that is not what I am trying to do here)

``````public int f(int n) {
if(n == 0)
return 1;

int result = 0;
for(int i = 0; i< n; i++)
result += f(i) * f(n-i-1);
return result;
}
``````

I want to remove this for the loop and express the function in terms of `f(n,i)` instead of `f(n)`.

Question

1. How to convert the above recurrence from `f(n)` at `f(n,i)` and remove the summons?
• Here, "n" is the size of the list of items and "i" is the ith item in the list that we choose to be the root of the tree.

## adjustment – Mathematica is slow to estimate the parameters of the vector autoregressive model

I have been trying to get VAR model parameters using EstimatedProcess and FindProcessParameters. It seems like it takes a long time as soon as I have more than 6 or 7 time series and an offset> 2.

The data I use is the z-rated daily newspaper of the financial time series, with around 1000 data points.

It works fine up to 8 sets with shift 2 and only up to 6 sets with shift 3 (taking 15 minutes for the last one). Anything above that doesn't seem to be working or is really very slow. EstimatedProcess has been running for 3 hours now for a process p = 3 VAR with 7 time series.

I have also tried this on data that I simulated using ARProcess and the problem is the same, it is time consuming and above a number of series and latency, it doesn't return results (unless I haven't waited long enough).

Any help would be greatly appreciated.

## Resolving inequalities and finding parameters

Let

$$S = 4 ( alpha -2) beta -58 ( alpha -2) ^ 2- beta ^ 2-8 c_6 left (7 alpha +2 c_6-13 right)$$

$$T = – frac { left (( alpha -4) ( alpha -2) ( alpha (3 alpha +4 beta -27) -18 beta +60) +4 c_2 ( alpha + beta -5) right) {} ^ 2} {4 ( alpha + beta -5) ^ 2} + c_2 ( alpha + beta -5) left (8 alpha + beta +4 c_6-16 right) {} ^ 2-c_2 ( alpha + beta -5) left (2 ( alpha -2) (3 alpha +10 beta -6) +8 c_6 ( alpha + beta -3) right)$$

$$U = left (- frac {1} {12} left (-12 c_4 ( alpha + beta -4) -36 c_2 right) left (-2 alpha + beta -2 left (-5 alpha -2 c_6 + 10 right) +4 right) {} ^ 2- frac {1} {6} c_6 left ( frac {3 ( alpha -6) ( alpha – 5) ( alpha -4) ( alpha -2)} { alpha + beta -5} -6 ( alpha -4) (2 alpha -9) ( alpha -2) -12 c_2 right ) left (-2 alpha + beta -2 left (-5 alpha -2 c_6 + 10 right) +4 right) – frac {1} {6} left ( frac {3 ( alpha -6) ( alpha -5) ( alpha -4) ( alpha -2)} { alpha + beta -5} -6 ( alpha -4) (2 alpha -9) ( alpha -2) -12 c_2 right) left (-3 ( alpha -2) (3 alpha -11) -2 c_6 ( alpha + beta -3) -6 c_4 right) + frac { 1} {12} left (-12 c_4 ( alpha + beta -4) -36 c_2 right) left (-2 ( alpha -2) (7 alpha -24) -4 ( alpha + beta -3) left (-5 alpha -2 c_6 + 10 right) right) right) {} ^ 2-4 left (- frac {1} {36} left ( frac { 3 ( alpha -6) ( alpha -5) ( alpha -4) ( alpha -2)} { alpha + beta -5} -6 ( alpha -4) (2 alpha -9) ( alpha -2) -12 c_2 right) {} ^ 2 + c_2 ( alpha + beta -5) left (-2 alpha + beta -2 left (-5 alpha -2 c_6 + 10 right) +4 right t) {} ^ 2-c_2 ( alpha + beta -5) left (-2 ( alpha -2) (7 alpha -24) -4 ( alpha + beta -3) left (- 5 alpha -2 c_6 + 10 right) right) right) left (- frac {1} {4} c_6 ^ 2 left (-2 ( alpha -2) (7 alpha -24) -4 ( alpha + beta -3) left (-5 alpha -2 c_6 + 10 right) right) – frac {1} {2} c_6 left (-2 alpha + beta – 2 gauche (-5 alpha -2 c_6 + 10 right) +4 right) left (-3 ( alpha -2) (3 alpha -11) -2 c_6 ( alpha + beta -3) -6 c_4 right) – frac {1 } {4} left (-3 ( alpha -2) (3 alpha -11) -2 c_6 ( alpha + beta -3) -6 c_4 right) {} ^ 2 + c_4 left (- 2 alpha + beta -2 left (-5 alpha -2 c_6 + 10 right) +4 right) {} ^ 2-c_4 left (- 2 ( alpha -2) (7 alpha – 24) -4 ( a lpha + beta -3) left (-5 alpha -2 c_6 + 10 right) right) right)$$,
or $$c_2, c_4, c_6 in mathbb {R}$$ and $$alpha, beta> 0$$. My question is the following:

Are there $$alpha, beta> 0$$ and $$c_2, c_4, c_6 in mathbb {R}$$ such as $$S> 0$$, $$T> 0$$, $$U le0$$?

I used the codes

``````FindInstance(S>0 && T>0 && U<=0 && α > 0 && β >0, {c2,c4,c6,α,β}, Reals)
``````

But it lasts more than 5 minutes and no results can be obtained. So I pick up several α on $$(1 / 10.10)$$ like that

``````α=17/7; FindInstance(S>0 && T>0 && U<=0 && α > 0 && β >0, {c2,c4,c6,β}, Reals)
``````

He replied with an empty set

``````{}
``````

It seems that the interval of $$alpha$$ is so narrow and hard to find. Any other approach to get a single solution $$( alpha, beta, c_2, c_4, c_6)$$?

Any reference, suggestion, idea or comment is welcome. Thank you!

``````S=-58 (-2 + α)^2 + 4 (-2 + α) β - β^2 - 8 Subscript(c, 6) (-13 + 7 α + 2 Subscript(c, 6))

T=-((-4 + α) (-2 + α) (60 -
18 β + α (-27 + 3 α + 4 β)) +
4 (-5 + α + β) Subscript(c,
2))^2/(4 (-5 + α + β)^2) + (-5 + α + β) Subscript(c, 2) (-16 + 8 α + β + 4 Subscript(c, 6))^2 - (-5 + α + β) Subscript(c, 2) (2 (-2 + α) (-6 + 3 α + 10 β) + 8 (-3 + α + β) Subscript(c, 6))

U=(-(1/12) (-36 Subscript(c, 2) -
12 (-4 + α + β) Subscript(c, 4)) (4 -
2 α + β -
2 (10 - 5 α - 2 Subscript(c, 6)))^2 + 1/12 (-36 Subscript(c, 2) -
12 (-4 + α + β) Subscript(c,
4)) (-2 (-2 + α) (-24 + 7 α) -
4 (-3 + α + β) (10 - 5 α -
2 Subscript(c, 6))) - 1/6 (-6 (-4 + α) (-2 + α) (-9 + 2 α) + (
3 (-6 + α) (-5 + α) (-4 + α) (-2 + α))/(-5 + α + β) - 12 Subscript(c, 2)) (4 - 2 α + β -
2 (10 - 5 α - 2 Subscript(c, 6))) Subscript(c, 6) - 1/6 (-6 (-4 + α) (-2 + α) (-9 + 2 α) + (
3 (-6 + α) (-5 + α) (-4 + α) (-2 + α))/(-5 + α + β) - 12 Subscript(c, 2)) (-3 (-2 + α) (-11 + 3 α) - 6 Subscript(c, 4) - 2 (-3 + α + β) Subscript(c, 6)))^2 - 4 (-(1/36) (-6 (-4 + α) (-2 + α) (-9 + 2 α) + (
3 (-6 + α) (-5 + α) (-4 + α) (-2 + α))/(-5 + α + β) - 12 Subscript(c, 2))^2 + (-5 + α + β) Subscript(c, 2) (4 - 2 α + β - 2 (10 - 5 α - 2 Subscript(c, 6)))^2 - (-5 + α + β) Subscript(c, 2) (-2 (-2 + α) (-24 + 7 α) - 4 (-3 + α + β) (10 - 5 α -
2 Subscript(c, 6)))) (Subscript(c, 4) (4 - 2 α + β -
2 (10 - 5 α - 2 Subscript(c, 6)))^2 -
Subscript(c, 4) (-2 (-2 + α) (-24 + 7 α) - 4 (-3 + α + β) (10 - 5 α - 2 Subscript(c, 6))) -
1/4 (-2 (-2 + α) (-24 + 7 α) -
4 (-3 + α + β) (10 - 5 α -
2 Subscript(c, 6))) !(*SubsuperscriptBox((c), (6), (2))) -
1/2 (4 - 2 α + β -
2 (10 - 5 α - 2 Subscript(c, 6))) Subscript(c,
6) (-3 (-2 + α) (-11 + 3 α) - 6 Subscript(c, 4) -
2 (-3 + α + β) Subscript(c, 6)) -
1/4 (-3 (-2 + α) (-11 + 3 α) - 6 Subscript(c, 4) -
2 (-3 + α + β) Subscript(c, 6))^2)
``````

## Why is it better to use parameters rather than temporary global variables?

If you are a very old programmer like me, you may have written stuff like this at the start:

``````LET A = 2
LET B = 2
PRINT C
END
LET C = A + B
RETURN
``````

Of course, the modern approach would look more like this:

``````var c = Add(2,2);
Print(c);
``````

I understand that the first reason is obviously "bad"; this is not disputed. I'm just trying to explain to another engineer the specific technical reasons why.

## column – Validation parameters act oddly

So I had this problem with the validation settings on a list of share points.

I use the formula below and it works a little. I cannot choose "tekniker klar" and save the form if I have not filled in these columns (Ärende tilldelat), (Ärende påbörjat) and the forms have the status "LSP, MSP, HSP".

But the problem is that if the formula activates and indicates the error message at the top, I want to scroll down and fill in the missing columns, but when I click on the empty field, another column drop down at the top of the form opens instead, and after that another column is automatically chosen and so on until she has chosen all the columns once, which I can fill in the missing column.

I hope someone can help me!

``````=IF(AND(Ärendetyp="LSP";Status="Tekniker klar");IF(AND((Ärende tilldelat)<>"";(Ärende påbörjat)<>"");TRUE;FALSE);TRUE)=IF(AND(Ärendetyp="MSP";Status="Tekniker klar");IF(AND((Ärende tilldelat)<>"";(Ärende påbörjat)<>"");TRUE;FALSE);TRUE)=IF(AND(Ärendetyp="HSP";Status="Tekniker klar");IF(AND((Ärende tilldelat)<>"";(Ärende påbörjat)<>"");TRUE;FALSE);TRUE)
``````

## 8 – The getQueryString method of Symfony does not return the modified query parameters

The Symfony Request object has a request property of type
ParameterBag, this maintains an iterable list of parameters. However, the ParameterBag is not directly translatable into a query string. This causes problems when you try to use the getQueryString () method after changing the ParameterBag parameters.

Solution 1 – Create a new query string:

``````   \$request = Drupal::request();
\$parameter_bag = \$request->query;
\$parameter_bag->remove('destination');

// This has to be http_build_query() because
// Drupal::request()->getQueryString() returns the original query string
// but here we need the modified query string (without destination).
\$query = http_build_query(\$parameter_bag->all());
``````

Solution 2 – Update all global variables:

``````    Drupal::request()->query->remove('destination');
Drupal::request()->overrideGlobals();
\$query = Drupal::request()->getQueryString();
``````

Reference to overrideGlobals ()

The main problem is that `getQueryString()` returns the global server variable query string that does not reflect changes to the parameter bag. Editing parameters is no longer as simple as it used to be. We must either create a new query string or replace ALL of the global variables. In my particular use case, I thought that overwriting all the globals was too wide and that it would overwrite all the modifications that other modules were implementing on which our specific code doesn’t There is no control, so I opted for solution 1 above as it is generally a safer code to set up.

My two options for solving this problem the right way are:

1) ParameterBag needs a method to build a query string

2) The request object must be able to return changed request parameters. Probably by deprecating / defaulting the existing function to return the output of a new function called getQueryStringOriginal () and creating another new function called getQueryStringModified ().

## c ++ 17 – Immutable c ++ object, defensive control parameters after defining const variables?

Let's go back to C ++ after a break in Java. Attempt to create an immutable object. Defined in the header as

``````public:
const int A;
``````

You want to check defensively that A is positive with the constructor. Is this sane?

``````MyObj::MyObj(int a) : A(a) {
if (b < 0)
throw invalid_argument("");
}
``````

A public const variable for A seems to be a clearer and cleaner solution than a getter only with a non-constant integer behind, but open to that or to other ideas if it is bad practice .

## Passing functions to execute as parameters in Python

I have functions that are implemented in a class as shown below

`Methods.py`

``````class Perform_stats:

def get_mean(self,data):
m = np.mean(data)
return m

def get_rms(self,data):
r = np.sqrt(np.mean(data**2))
return r
....
``````

`Testmethods.py`

``````import Methods as mds

ps = mds.Perform_stats()

df_mean = ps.get_mean(df)

df_rms  = ps.get_rms(df)

``````

So in `Testmethods.py` I need to transmit the same data `df` to the different function of `Perform_stats` , is it possible to pass the name of the functions to be implemented as `list` so that i can call all the functions and get the result `dict`?

Something as shown below

``````import Methods as mds

ps = mds.Perform_stats(df,methods=('mean','rms','All')) #All is the default arugument which return a dictionary

``````

I found an article that does the same, but I have not implemented my requirement.

## Rails: how to validate the form parameters in the controller?

I'm new to Rails. I have the following form

``````<%= form_for :user, :url => { :action => :update, :controller => :usuario, :id => current_user.id }, :method => :put do |f| %>
Nueva encuesta:

<%= f.label :servicio %>
<%= f.check_box :fluido, {}, "fluido", false%>Servicio de fluidos
<%= f.check_box :solido, {}, "solido", false %>Servicio de control de sólidos
<%= f.check_box :ambiente, {}, "ambiente", false %>Servicio Ambiental

``````

and I want to validate the value of the check boxes to perform certain actions within the controller, for example:

user_controller.rb:

``````@usuario = User.find(params(:id))
if (request(:fluido)=='fluido')
if @usuario.update_attributes(:fluido => 'fluido')
redirect_to 'index'
end
end
if (request(:solido)=='solido')
if @usuario.update_attributes(:solido => 'solido')
redirect_to 'index'
end
end
if (request(:ambiente)=='ambiente')
if @usuario.update_attributes(:ambiente => 'ambiente')
redirect_to 'index'
end
end
``````

the problem is that the values ​​of the query object that are compared in the if are & # 39; nil & # 39;, that is, they don't return the values ​​of the form

## plotting – How to plot a parametric time-dependent plot with different parameters by taking the average of time?

I am trying to trace the solution given in the code regarding "delc". Now the problem is that it can be plotted for a particular value of "t" as t = 10,20,50,60, .. up to 100 but what I need is the average of all these plots. Is it possible to plot this by adjusting the y-axis so that I can get a single graph in which the time also varies up to 100.

`````` w1 = 1;
gma1 = 0.005;
n1 = 1;
gma2 = 0.005;
G1 = 0.005;
k1 = .1;
k2 = 0.1;
a1 = 0.07;
a2 = 0.58;
k0 = 0.1;
Q1 = 1.268;
del0 = 1;
N1 = 1;
ome = 1;
M1 = del0*(1 - Cos(ome*t));
s = ParametricNDSolveValue({V11'(t) - V21(t)*w1 - V12(t)*w1 == 0,
V12'(t) - V22(t)*w1 + w1*V11(t) + gma1*V12(t) -
Sqrt(2)*G1*a1*V13(t) - Sqrt(2)*G1*a2*V14(t) == 0,
V13'(t) - V23(t)*w1 + k1*V13(t) +
Sqrt(2)*G1*a2*V11(t) - (-G1*Q1 + delc)*V14(t) == 0,
V14'(t) - V24(t)*w1 + k1*V14(t) -
Sqrt(2)*G1*a1*V11(t) - (G1*Q1 - delc)*V13(t) == 0,
V21'(t) + V11(t)*w1 + gma1*V21(t) - Sqrt(2)*G1*a1*V31(t) -
Sqrt(2)*G1*a2*V41(t) - w1*V22(t) == 0,
V22'(t) + V12(t)*w1 + gma1*V22(t) - Sqrt(2)*G1*a1*V32(t) -
Sqrt(2)*G1*a2*V42(t) + w1*V21(t) + gma1*V22(t) -
Sqrt(2)*G1*a1*V23(t) - Sqrt(2)*G1*a2*V24(t) - gma2*(2*n1 + 1) ==
0, V23'(t) + w1*V13(t) + gma1*V23(t) - Sqrt(2)*G1*a1*V33(t) -
Sqrt(2)*G1*a2*V43(t) + k1*V23(t) +
Sqrt(2)*G1*a2*V21(t) - (-G1*Q1 + delc)*V24(t) == 0,
V24'(t) + V14(t)*w1 + gma1*V24(t) - Sqrt(2)*G1*a1*V34(t) -
Sqrt(2)*G1*a2*V44(t) + k1*V24(t) -
Sqrt(2)*G1*a1*V21(t) - (G1*Q1 - delc)*V23(t) == 0,
V31'(t) + k1*V31(t) +
Sqrt(2)*G1*a2*V11(t) - (-G1*Q1 + delc)*V41(t) - w1*V32(t) == 0,
V32'(t) + k1*V32(t) +
Sqrt(2)*G1*a2*V12(t) - (-G1*Q1 + delc)*V42(t) + w1*V31(t) -
Sqrt(2)*G1*a2*V34(t) - Sqrt(2)*G1*a1*V33(t) + gma1*V32(t) == 0,
V33'(t) + k1*V33(t) +
Sqrt(2)*G1*a2*V13(t) - (-G1*Q1 + delc)*V43(t) + k1*V33(t) +
Sqrt(2)*G1*a2*V31(t) - (-G1*Q1 + delc)*V34(t) - k0 == 0,
V34'(t) + k1*V34(t) +
Sqrt(2)*G1*a2*V14(t) - (-G1*Q1 + delc)*V44(t) + k1*V34(t) -
Sqrt(2)*G1*a1*V31(t) - (G1*Q1 - delc)*V33(t) == 0,
V41'(t) + k1*V41(t) -
Sqrt(2)*G1*a1*V11(t) - (G1*Q1 - delc)*V31(t) - w1*V42(t) == 0,
V42'(t) + k1*V42(t) +
Sqrt(2)*G1*a1*V12(t) - (G1*Q1 - delc)*V32(t) + w1*V41(t) -
Sqrt(2)*G1*a2*V44(t) - Sqrt(2)*G1*a1*V43(t) + gma1*V42(t) == 0,
V43'(t) + k1*V43(t) -
Sqrt(2)*G1*a1*V13(t) - (G1*Q1 - delc)*V33(t) + k1*V43(t) +
Sqrt(2)*G1*a2*V41(t) - (-G1*Q1 + delc)*V44(t) == 0,
V44'(t) + k1*V44(t) -
Sqrt(2)*G1*a1*V14(t) - (G1*Q1 - delc)*V34(t) + k1*V44(t) -
Sqrt(2)*G1*a1*V41(t) - (G1*Q1 - delc)*V43(t) - k0 == 0,
V11(0) == 1, V12(0) == 1, V13(0) == 0, V14(0) == 0, V21(0) == 0,
V22(0) == 1, V23(0) == 0, V24(0) == 0, V31(0) == 0, V32(0) == 0,
V33(0) == 0, V34(0) == 0, V41(0) == 0, V42(0) == 0, V43(0) == 0,
V44(0) == 0}, {V11, V12, V13, V14, V21, V22, V23, V24, V31, V32,
V33, V34, V41, V42, V43, V44},{t, 0, 100},delc);
P2 = Plot({Evaluate(1/2*(V11(t) + V22(t) - 2*V12(t))^(-1) /. s)}, {t,
0, 60}, PlotRange -> {0, 1}, Frame -> True,
FrameLabel -> {Style("Time", Bold, 20),
Style(" !(*SubscriptBox((S), (q)))", Bold, 20)},
FrameTicksStyle -> Directive(FontSize -> 20),
PlotStyle -> {Thickness(0.0005), Thickness(0.008)})
``````