Data binding to objects which are initially null

Question: I have a Camera object which has properties like Name, SensorWidth, SensorHeight etc. I would like to bind these properties to UI, but Camera is null at the time application starts (user can select any camera). Only when camera is opened, Camera object is initialized (and loads all its properties).
Is there a way to setup data binding in XAML so it starts to work when object is initialized?
I can do this easily in C# - setup data binding after camera is opened but this would had to be done in View code behind, which is probably bad for MVVM pattern I am using, since the button for opening camera is using Command interface to communicate with ViewModel.
What is the way to correctly approach this?
Comment: WPF data binding has no problem with any property in a property path being null. You may declare a Camera property that is initially null and bind like `{Binding Camera.Name}`. You only have to make sure that the Camera property fires a change notification.
Comment: You are right of course I missed the event in the Camera property
Answer:
Is there a way to setup data binding in XAML so it starts to work when object is initialized?

Bindings are designed to handle null values gracefully.  There should be no problem if your Camera object is null at startup.  WPF expects that the binding source might be null, and that any values along the property path might be null.
That said, it is useful to understand when a Binding produces a value successfully:

The path to the binding source resolves successfully.
The value converter, if any, is able to convert the resulting value.
The resulting value is valid for the binding target (target) property.

If (1) and (2) fail to produce a valid value, the binding will produce the default value of the target property.  If you want to specify an alterate value to use in this situation, you can provide a FallbackValue on your binding.
In your case, if you have {Binding Camera.Name}, and Camera is null, the binding will fail to produce a value.  If you changed the binding to {Binding Camera.Name, FallbackValue='n/a'}, then it produce the string n/a when Camera is null.
TargetNullValue lets you specify an alternate value in cases where the binding does produce a valid value, but that value is null.  For example, if Camera itself is non-null, but its Name is null, then {Binding Camera.Name, TargetNullValue='(no name)'} will produce the string (no name).


Is there a formula for an innovation outlier regressor in time series intervention analysis

Question: For an innovative outlier, the equations I got from a paper by Tsay, are these:
$$y_t = f(t) +z(t)\\
f(t) = w_0 \frac{\theta(B)}{\phi(B)}\epsilon_t^d\\
\epsilon_t^d =
\begin{cases}
      1 & t = d \\
      0 & t \neq d
\end{cases}
$$
and, $ \frac{\theta(B)}{\phi(B)}$ are polynomials in a backshift operator, representing an arma model for a time series $z(t)$, while $y_t$ is a new time series with the innovation taken into account.
I am trying to figure out how to work this into a regressor for different values of  $ \frac{\theta(B)}{\phi(B)}$.
For example, I tried to work out this simple case, but it is a ton of work.
if  $ \frac{\theta(B)}{\phi(B)} = \frac{1}{1-\phi_1B - \phi_2B^2}$:
$$
y_t = \frac{w_0 \epsilon_t ^ d}{1 - \phi_1B - \phi_2B ^ 2} + z_t\\
(1 - \phi_1 B - \phi_2 B ^ 2) y_t = w_0 \epsilon_t ^ d + (1-\phi_1 B - \phi_2 B ^ 2) z_t
$$
case 1:
$$t < d \rightarrow y_d = z_d $$
case 2:
$$
t = d : \\
y_d - \phi_1 y_{d - 1} - \phi_2 y_{d - 2} = w_0 \epsilon_d^d + (1-\phi_1 B - \phi_2 B ^ 2) z_t \rightarrow \\
\rightarrow y_d - \phi_1 z_{d - 1} - \phi_2 z_{d - 2} = w_0 + (1-\phi_1 B - \phi_2 B ^ 2) z_t \rightarrow \\
\rightarrow y_d = w_0 + z_d
$$
case 3:
$$
t = d + 1\\
y_{d + 1} - \phi_1 y_{d} - \phi_2 y_{d - 1} = w_0 + (1-\phi_1 B - \phi_2 B ^ 2) z_{d + 1} \rightarrow \\
\rightarrow y_{d+1} - \phi_1 (w_0 + z_d) -  \phi_2 z_{d - 1} = (1-\phi_1 B - \phi_2 B ^ 2) z_{d + 1} \rightarrow \\
\rightarrow y_{d+1} = \phi_1 w_0 + z_{d+1}
$$
case 4:
$$
t = d + 2\\
y_{d + 2} - \phi_1 y_{d+1} - \phi_2 y_{d} = w_0 + (1-\phi_1 B - \phi_2 B ^ 2) z_{d + 2} \rightarrow \\
\rightarrow y_{d + 2} - \phi_1 (\phi_1 w_0 + z_{d+1}) -  \phi_2 (w_0 + z_d) = (1-\phi_1 B - \phi_2 B ^ 2) z_{d + 2} \rightarrow \\
\rightarrow y_{d+2} = \phi_1 ^2 w_0 + \phi_2 w_0 + z_{d+2}
$$
case 5:
$$t = d + 3 \rightarrow y_{d+3} = \phi_1 ^ 3 w_0 + 2 \phi_1 \phi_2 w_0 + z_{d+3}$$
case 6:
$$t = d + 4 \rightarrow y_{d+4} = \phi_1 ^ 4 w_0 + 3 \phi_1^2 \phi_2 w_0 +  \phi_2 ^2 w_0 + z_{d+3}$$
My question is... is there some formula, where given any arma model $\theta(B)$ and $\phi(B)$, I can create a regressor with the terms solved above?
For example, in the above case I would have a regressor with $0$'s until $t = d$, and then the above terms :
$\left[0, 0, \ldots, 0, 1, \phi_1, \phi_1 ^2 + \phi_2, \phi_1 ^ 3 + 2 \phi_1 \phi_2, \phi_1 ^ 4 + 3 \phi_1^2 \phi_2 +  \phi_2 ^2, \right]$
The idea here is that using this regressor, and knowledge of the ARMA terms, I could find $w_0$ with a linear regression.
Or, is there no real formula, and I just have to write something recursive in R?
Answer: no FOR ADDITIVE OUTLIERS .. because the presence of the ma term IMPLIES long memory in both the Y and any user specified predictor OR empirically identified deterministic variable. See https://autobox.com/pdfs/SARMAX.pdf for the BIG PICTURE .
not so sure about innovative outliers as that requires ML .


What do the Hurwitz quaternions have to do with the Hurwitz quaternion order?

Question: The Hurwitz quaternions are the ring formed by the elements of the form $w+xi+yj+zij$ where $i^2=j^2=-1$, $ij=-ji$, and where $w,x,y,z$ are either all integers or all half-integers. These form a maximal order of the quaternion algebra $\Big(\frac{-1,-1}{\mathbb{Q}}\Big)$.
The Hurwitz quaternion order, on the other hand, is defined as follows (according to Wikipedia). Let $\rho$ be the primitive seventh root of unity and let $K$ be the maximal real subfield of $\mathbb{Q}(\rho)$. Let $\eta=2\cos(\frac{2\pi}{7})$ (so that $\mathbb{Z}[\eta]$ is the ring of integers of $K$) and consider the quaternion algebra $\Big(\frac{\eta,\eta}{K}\Big)$ (where $i^2=j^2=\eta$). Then let $\tau=1+\eta+\eta^2$ and $j'=\frac{1+\eta i+\tau j}{2}$, and the Hurwitz quaternion order is the maximal order $\mathbb{Z}[\eta][i,j,j']$ in $\Big(\frac{\eta,\eta}{K}\Big)$.
It seems that the Hurwitz quaternion order should be some sort of generalization of the Hurwitz quaternions but there are a lot of decisions here that seem arbitrary to me. What is the motivation for the similar nomenclature? What is special about the order $\mathbb{Z}[\eta][i,j,j']$ in $\Big(\frac{\eta,\eta}{K}\Big)$ and what does it have in common with the Hurwitz quaternions in $\Big(\frac{-1,-1}{\mathbb{Q}}\Big)$?
Answer: It appears that the term does not refer to a generalization of the relationship of Hamilton's quaternions to the Hurwitz quaternions (as one might expect) but rather the term as defined there is just a specific order in a specific quaternion algebra other than $\mathbb H$.  Go figure.
Comment: Further, I think Wiki's description in this case is ... unfortunate. In my experience, the "Hurwitz order" or whatever refers to the first thing in the question. The second thing may indeed have been investigated by Hurwitz (I don't know), and does indeed fit into a general understanding (e.g., see Weil's "Basic Number theory", or my notes on such stuff), but there's no immediate connection between those two things... apart from being two instances of a more general thing.
Comment: @paulgarrett I agree the nomenclature is unfortunate, but this time it is not Wikipedia's fault.  See my answer.
Answer: What they have to do is they are both related to things studied by Hurwitz.  The "Hurwitz quaternions" are an object typically considered in number theory (and as a number theorist, if I hear "Hurwitz order" that's what I'll think of), and were directly studied by Hurwitz.
The other "Hurwitz order" comes up in geometry because it is related to Hurwitz surfaces, which are compact Riemann surfaces that have $84(g-1)$ automorphisms ($g =$ genus).  This is the maximal number possible by a theorem of Hurwitz.  The smallest possible genera for Hurwitz surfaces are 3, 7 and 14.  The first two have automorphism groups of the form PSL(2,$q$), whereas the third has automorphism group the triangle group (2,3,7).  The latter group is essentially the unit group in the other "Hurwitz order," hence the name, though as far as I know was not actually studied by Hurwitz.


J2EE, ECLIPSE , Mysql , tomcat server

Question: I have made a GUI with eclipse where i have used mysql as databasemanagment and tomcat as a server.
I have some folder containig images that i need to link with my GUI.
How should I do it
Comment: What does it mean to link a folder with images to the GUI? You want to have those images available in your web app?
Comment: Yes . Basically My GUI is a bioinformatic  web application. So i have set of experiments that i store in the database. With them some images should also be stored so that i can view them. Its like uploading a whole directory of images and viewing them. How should i do it?
Comment: Reference an external file system/directory, or copy them into your project, or store them in the DB. I recommend the first.
Comment: To store them in the DB i need to give commands in DB and also in my GUI i need to write codes to connect with DB. May i know the codes?
Comment: You already interact with the DB, and how to store images in a DB is well-documented. But likely not recommended: file systems are a better place to store... files.
Answer: Take a look at this, this, this and this posts. All of them show how to list files from a directory using JSP (that will be deployed into tomcat).
Seems that you are a beginner. So, take a look into this and this tutorials related to jsp/tomcat.

J2EE, ECLIPSE , Mysql , tomcat server


Estimate pdf on a large vector

Question: I have a vector of 10,000 observations and I need to estimate the pdf at each point of the vector. The code I have is the following (Matlab):
n = 1000; %Sample size
Y = normrnd(0,1,n,1); %Data
h = 1.06*std(Y)*n^(-1/5); %Bandwith
pair = (Y - Y')/h; %Argument of Kernel
pair_kernel = normpdf(pair); %calculate kernel on all the matrix
f_hat = mean(pair_kernel,2)/h; %Kernel estimate of pdf

The running time increases considerably when I go from n=1000 to n=10000. This is an issue because I have to do this many times as it is a Monte Carlo study. I wonder if there is a more efficient way to deal with the large matrices.
Comment: Is there a reason why just estimating the pdf on the same set of equally spaced points won't give you a good approximation of the pdf?
Answer:
If you want to use the kernel density then yes, the biggest bottleneck would be doing the calculation over all the values, you cannot do much about it. In practice, what people do to speed it up, is they do not use the estimator directly, but they approximate it instead.

R's density function uses the FFT transformations, to reduce
the number of points used in the computation. Scikit-learn used a
different approach, they store the training data in a tree
structure, using ball trees and KD trees, so that instead of
looking at all the data, you are evaluating only the neighborhood of
the $x$ point.

This trades off the quality of the estimate over computation time.

I'm not sure what you are trying to do here, but if you want to sample from kernel density there are algorithms for that that do not need you to evaluate the probability density function.

As noticed in the comment by JimB, if the only thing you want to do is to estimate the distribution, this is what kernel density estimator does, you do not need to sample anything to achieve that.

Estimate pdf on a large vector


TYPO3 - TCA Migrations Check shows 4th parameter 'specConf' of the field 'showitem' with fieldName = 'sys_language_uid' has been migrated

Question: After upgrading from 7LTS to 8LTS and running TCA Migrations Check for my extensions the result list shows the following:
Comment:

The 4th parameter 'specConf' of the field 'showitem' with fieldName = 'sys_language_uid' has been migrated, from TCA table "tx_myextension_domain_model_accounts['types']['1']['showitem']"to "tx_myextension_domain_model_accounts['types']['1']['columnsOverrides']['sys_language_uid']['defaultExtras']".

In TCA I think this is the spot:
    'types' => array(
        '1' => array('showitem' => 'sys_language_uid;;;;1-1-1, l10n_parent, l10n_diffsource, hidden;;1, --div--;LLL:EXT:cms/locallang_ttc.xlf:tabs.access, starttime, endtime'),
    ),

What changes do I need to do to get rid of comment?
I think this is the relevant part:
https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/7.3/Deprecation-67229-TcaChanges.html
And this comment as well?:

Migrated 'showitem' field from TCA table tx_accountmanager_domain_model_accounts['types']['1']" : Moved additional palette with name "tx_myextension_domain_model_accounts['types']['1']['1']" as 3rd argument of field "tx_myextension_domain_model_accounts['types']['1']['hidden']"to an own palette. The result of this part is: "hidden, --palette--;;1"
Comment: Have a look at BE modul 'Configuration' under 'TCA'. There you will find changes to your TCA configuration, which you can copy from there.
Comment: Thanks for the hint ... I wasn't aware of that ...
Comment: If these changes are not applied right away, will the extension still work?
Answer: You may drop the whole ;;;;1-1-1 thing from your sys_language_uid field.
The best way to compare your original TCA with the migrated TCA is using the backend module "Configuration" and clicking through the TCA tree.
Comment: @urs You see the migrated version, no highlights, just copy from there to your TCA File.
Comment: I'm not sure if I understand: in Configuration > TCA: should I see the "migrated" TCA and copy the line from there (or some highlighted changes, which I don't see)
Comment: @bandanh can you give an example? Paste at end of TCA file or adapt TCA structure according to changes?
Comment: Remove ;;;;1-1-1 at all fields
Answer: Here a way to fix it, using the migrated version of the Backend Module Configuration:
If install tool > TCA Migrations says the following:

The 4th parameter 'specConf' of the field 'showitem' with fieldName = 'sys_language_uid' has been migrated, from TCA table "tx_arbeiten_domain_model_diplomarbeiten['types']['1']['showitem']"to "tx_arbeiten_domain_model_diplomarbeiten['types']['1']['columnsOverrides']['sys_language_uid']['defaultExtras']".

Go to:
TYPO3 Backend -> Configuration -> $GLOBALS['TCA'](Table configuration array) -> tx_arbeiten_domain_model_diplomarbeiten -> types -> 1 -> showitems
Copy the updated values from there, in this case:

showitem = sys_language_uid,l10n_parent,l10n_diffsource,hidden,--palette--;;1,authorfirstname,author,author2firstname,author2,author3firstname,author3,title,schoolclass,link,--palette--;Datei 1;downloadgroup,--palette--;Datei 2;download2group,--palette--;Datei 3;download3group,--div--;LLL:EXT:cms/locallang_ttc.xml:tabs.access,starttime,endtime

and paste them into your extension's TCA file (again at ['types']['1']['showitem'])
Comment: better solution than accepted, after use first i had problem with --palette--


Change of variables (Real Analysis)

Question: The change of variables formula is given as follows: For $\Omega, \Omega' \subset \mathbb{R}$ an open set, let $\phi: \Omega \to \Omega'$ such that $\phi$ is bijective, continuous, and $\phi^{-1}$ is continuous. Then for some integrable function $f$ (on $\Omega'$),
$$
\int_{\Omega'} f(x') \, dx' = \int_{\Omega} (f \cdot \phi)(x) |\det(J(x))| \, dx.
$$
I don't understand how to apply this to calculating the integral
$$
\int_{\mathbb{R}^d} f(x-y) \, dy.
$$
For instance, I don't know what I should take to be $\phi$. Is $\phi(x) = x+y$ because then $\phi(x-y) = x$? Or should $\phi(x) = x-y$ because we should take the change of variables $x-y = x'$? In the lectures, I think the change of variables $x-y = x'$ was taken.
Comment: $x'=x-y$ is the right substitution to make.
Answer: If $\phi:\mathbb{R}^d\to \mathbb{R}^d$ is an arbitrary surjective map, then $\phi(\mathbb{R}^d)=\mathbb{R}^d$. Hence,
$$
\int_{\mathbb{R}^d} f(x-y) \,\mathrm{d}y\;=\;\int_{\phi(\mathbb{R}^d)} f(x-y) \,\mathrm{d}y\ .
$$
If $\phi:\mathbb{R}^d\to \mathbb{R}^d$ is also a diffeomorphism, then by the formula that you correctly write, you get
$$
\int_{\phi(\mathbb{R}^d)} f(x-y) \,\mathrm{d}y\;=\; \int_{\mathbb{R}^d} f(x-\phi(z)) \cdot|\det D \phi(z)|\, \mathrm{d}z\, .
$$
In particular, for the diffeomorphism of $\mathbb{R}^d$ given by $\phi(z)=x-z$, you obtain$$
\int_{\mathbb{R}^d} f(x-\phi(z))\cdot |\det D \phi(z)| \,\mathrm{d}z \, \;=\;
\int_{\mathbb{R}^d} f(z) \,\mathrm{d}z \,
$$
because of $|\det  D \phi(z)|=|-1|=1$.

Change of variables (Real Analysis)


How do i calculate the gradients for the bayes-by-backprop algorithm in R?

Question: UPDATE - edits with my code progress are added at the end of this post.
I'm trying to create a neural network in R based on the bayes-by-backprop model as described in this paper. As inspiration for the code i'm using the python description on this website. However, in this paper they use an autograd function and i'm trying to calculate the gradient manually. The important algorithm from the paper is this one:

I think i've got most of the code ready, but i'm getting stuck on calculating the partial derivatives needed to calculate the gradients for mu (mean) and rho (parameter of the standard deviation).
This is the backpropagation function that i'm trying to make. I'm trying to adapt it from a regular neural network that i created before. The parameters are:

y = ground truth
Wn = list, every entry represents a layer of the network, containing a sample of the probability distributions in that layor
Un = list similar to Wn, the mus (mean) of the probability distributions in the posterior
Rn = list similar to Wn, the rhos (parameter used to define the standard deviation)
sigma = list similar to Wn, the standard deviation
eps = list similar to Wn, the random variable that is used for the reparametrization trick
ff = a list of the outputs of each feedforward layer, the last output is the model prediction.

backpropagate <- function(y, Wn, Un, Rn, sigma, ff, eps, learn_rate = 0.01) {

  y_hat <- ff[[3]]

  loss <- list()
  dWn <- list()

  loss[[2]] <- loss_function(y_hat, y, Wn, Un, sigma)

  dUn[[2]] <- ???
  dRn[[2]] <- ???

  loss[[1]] <- ???

  dUn[[1]] <- ???
  dRn[[1]] <- ???

  Un[[1]] <- Un[[1]] - learn_rate * dUn[[1]]
  Un[[2]] <- Un[[2]] - learn_rate * dUn[[2]]

  Rn[[1]] <- Rn[[1]] - learn_rate * dRn[[1]]
  Rn[[2]] <- Rn[[2]] - learn_rate * dRn[[2]]

  return(list(Un, Rn))
}

How do i calculate the gradients of mu and rho? I can do partial derivatives on polynomial and exponential functions but mu and rho are defined here only as probability distributions. The loss function is defined as the $$posterior - prior * likelihood$$ but how do i take the derivative w.r.t. mu and rho of those?
$$posterior = \log(P(w)) = \sum_i \log(N(w_i | \mu, \sigma^2)) $$
$$prior = \log(q(w|\theta)) = \sum_i \log(N(w_i | 0, 1)) $$
$$likelihood = \log(P(D|w)) = y * log(softmax(\hat{y})) $$
For reference:
My main training function looks like this:
train <- function(x, y, neurons = 32, layers = 3, rate = 0.01, iterations = 10000) {

  d <- ncol(x) + 1

  Un <- list()
  Rn <- list()

  Un[[1]] <- matrix(rnorm(d * neurons), d, neurons)        # generate mus for the gaussian distribution of each neuron. matrix d (rows) x neurons (columns)
  Un[[2]] <- matrix(rnorm(neurons + 1), neurons + 1, 1)

  Rn[[1]] <- matrix(-3, d, neurons)        # generate the rhos to calculate the standard deviation later
  Rn[[2]] <- matrix(-3, neurons + 1, 1)

  for (i in 1:iterations) {

    Wn <- list()
    sigma <- list()
    eps <- list()

    sigma[[1]] <- log(1 + exp(Rn[[1]]))     # calculate the standard deviation from rho
    sigma[[2]] <- log(1 + exp(Rn[[2]]))

    eps[[1]] <- matrix(rnorm(d * neurons, 0, 0.1), nrow = d, ncol = neurons)     # generate a random number epsilon for every neuron
    eps[[2]] <- matrix(rnorm(neurons + 1, 0, 0.1), neurons + 1, 1)

    Wn[[1]] <- Un[[1]] + sigma[[1]] * eps[[1]]     # take one sample of the posterior
    Wn[[2]] <- Un[[2]] + sigma[[2]] * eps[[2]]

    ff <- feedforward(x, Wn)
    backlist <- backpropagate(y, Wn, Un, Rn, sigma, ff, eps, learn_rate = rate)

    Un <- backlist[[1]]
    Rn <- backlist[[2]]
  }
  return(Wn)
}

And then my feedforward plus helper functions:
feedforward <- function(x, Wn) {

  ff <- list(x)
  Zn <- cbind(1, ff[[1]]) %*% Wn[[1]]
  ff[[2]] <- sigmoid::relu(Zn)
  Zn <- cbind(1, ff[[2]]) %*% Wn[[2]]
  ff[[3]] <- Zn

  return(ff)
}

log_softmax <- function(y_hat, y){
  y * log(exp(y_hat) / sum(exp(y_hat)))
}

log_gaussian <- function(x, mu, sigma){
  return(-0.5 * log(2*pi) - log(sigma) - (x - mu)**2 / (2 * sigma**2))
}

loss_function <- function(y_hat, y, Wn, Un, sigma){

  posterior <- sum(log_gaussian(Wn[[i]], Un[[i]], sigma[[i]]))

  prior <- sum(log_gaussian(Wn[[i]], 0, 1))

  likelihood <- log_softmax(y_hat, y)

  return(posterior - prior * likelihood)
}

EDIT: here's the code that i've tried in Torch for R so far. I think i have everything correct up to the point where i need to calculate the posterior and prior for the loss function. I don't know how i can call the gaussian function in torch to generate the distributions for the posterior and the prior.
For the posterior, i need to generate a distribution for every neuron with mean $\mu$ and standard deviation $\sigma$, which are the parameters for each neuron.
For the prior, i need to generate a distribution for every neuron with mean $0$ and standard deviation $1$.
The code is basically adapted from this page on the torch website.
library(torch)

### generate training data -----------------------------------------------------

# input dimensionality (number of input features)
d_in <- 3
# output dimensionality (number of predicted features)
d_out <- 1
# number of observations in training set
n <- 100

# create random data
x <- torch_randn(n, d_in)
y <- x[, 1, NULL] * 0.2 - x[, 2, NULL] * 1.3 - x[, 3, NULL] * 0.5 + torch_randn(n, 1)

### initialize weights ---------------------------------------------------------

# dimensionality of hidden layer
d_hidden <- 32
# mean values of the neurons in the first layer
mu1 <- torch_randn(d_in, d_hidden, requires_grad = TRUE)
# mean values of the neurons in the second layer
mu2 <- torch_randn(d_hidden, d_out, requires_grad = TRUE)

# rho values (parameters for the standard deviation) for the first layer
rho1 <- torch_randn(d_in, d_hidden, requires_grad = TRUE)
# rho values for the second layer
rho2 <- torch_randn(d_hidden, d_out, requires_grad = TRUE)

### network parameters ---------------------------------------------------------

learning_rate <- 1e-4

### training loop --------------------------------------------------------------

for (t in 1:200) {

  # random error
  eps1 <- torch_randn(d_in, d_hidden, requires_grad = TRUE)
  eps2 <- torch_randn(d_hidden, d_out, requires_grad = TRUE)

  ### -------- Sample variational posterior --------
  w1 <- torch_add(mu1, torch_multiply(torch_log(torch_add(torch_exp(rho1), 1))), eps1)
  w2 <- torch_add(mu2, torch_multiply(torch_log(torch_add(torch_exp(rho2), 1))), eps2)

  ### -------- Forward pass --------
  y_pred <- x$mm(w1)$clamp(min = 0)$mm(w2)$clamp(min = 0)

  ### -------- Likelihood --------
  likelihood <- torch_multiply(y, nnf_log_softmax(y_hat))

  ### -------- Prior --------

  ### -------- Posterior --------

  ### -------- compute loss --------
  loss <- (posterior - prior * likelihood)$sum()
  if (t %% 10 == 0)
    cat("Epoch: ", t, "   Loss: ", loss$item(), "\n")

  ### -------- Backpropagation --------

  # compute gradient of loss w.r.t. all tensors with requires_grad = TRUE
  loss$backward()

  ### -------- Update weights --------

  # Wrap in with_no_grad() because this is a part we DON'T
  # want to record for automatic gradient computation
  with_no_grad({
    mu1 <- mu1$sub_(learning_rate * mu1$grad)
    mu2 <- mu2$sub_(learning_rate * mu2$grad)
    rho1 <- rho1$sub_(learning_rate * rho1$grad)
    rho2 <- rho2$sub_(learning_rate * rho2$grad)

    # Zero gradients after every pass
    mu1$grad$zero_()
    mu2$grad$zero_()
    rho1$grad$zero_()
    rho2$grad$zero_()
  })

}

EDIT2: This is the R code that i have right now, using torch.
the log_gaussian function is copied directly from this website.
I create 100 observations with 3 features, with a hidden layer of 32 neurons and a single output. the output is either 1 or 0.
It seems to be working, but after 20 or so iterations the loss suddenly returns NaN and the network stops learning. I have some debugging still to do.
library(torch)
library(Rlab)

### generate training data -----------------------------------------------------

# input dimensionality (number of input features)
d_in <- 3
# output dimensionality (number of predicted features)
d_out <- 1
# number of observations in training set
n <- 100

# create random data
x <- torch_randn(n, d_in)
y <- torch_tensor(rbern(n, 0.6))

### initialize weights ---------------------------------------------------------

# dimensionality of hidden layer
d_hidden <- 32

# mean values of the neurons in the first and second layers
mu1 <- torch_randn(d_in, d_hidden, requires_grad = TRUE)
mu2 <- torch_randn(d_hidden, d_out, requires_grad = TRUE)
bmu1 <- torch_randn(1, d_hidden, requires_grad = TRUE)
bmu2 <- torch_randn(1, d_out, requires_grad = TRUE)

# rho values (parameters for the standard deviation) for the first and second layers
rho1 <- torch_randn(d_in, d_hidden, requires_grad = TRUE)
rho2 <- torch_randn(d_hidden, d_out, requires_grad = TRUE)
brho1 <- torch_randn(1, d_hidden, requires_grad = TRUE)
brho2 <- torch_randn(1, d_out, requires_grad = TRUE)

### network parameters ---------------------------------------------------------

learning_rate <- 1e-11

### training loop --------------------------------------------------------------

for (t in 1:200) {

  # random error
  eps1 <- torch_randn(d_in, d_hidden)
  eps2 <- torch_randn(d_hidden, d_out)
  eps3 <- torch_randn(d_in, d_hidden)
  eps4 <- torch_randn(d_hidden, d_out)

  beps1 <- torch_randn(1, d_hidden)
  beps2 <- torch_randn(1, d_out)
  beps3 <- torch_randn(1, d_hidden)
  beps4 <- torch_randn(1, d_out)

  s1 <- torch_log(torch_add(torch_exp(rho1), 1))
  s2 <- torch_log(torch_add(torch_exp(rho2), 1))

  bs1 <- torch_log(torch_add(torch_exp(brho1), 1))
  bs2 <- torch_log(torch_add(torch_exp(brho2), 1))

  ### -------- Sample variational posterior --------
  w1 <- rho1$exp()$add(1)$log()$multiply(eps1)$add(mu1)
  w12 <- rho1$exp()$add(1)$log()$multiply(eps3)$add(mu1)
  w2 <- rho2$exp()$add(1)$log()$multiply(eps2)$add(mu2)
  w22 <- rho2$exp()$add(1)$log()$multiply(eps4)$add(mu2)

  b1 <- brho1$exp()$add(1)$log()$multiply(beps1)$add(bmu1)
  b12 <- brho1$exp()$add(1)$log()$multiply(beps3)$add(bmu1)
  b2 <- brho2$exp()$add(1)$log()$multiply(beps2)$add(bmu2)
  b22 <- brho2$exp()$add(1)$log()$multiply(beps4)$add(bmu2)

  ### -------- Forward pass --------
  softmax <- nn_softmax(1)
  y_hat <- softmax(x$mm(w1)$add(b1)$clamp(min = 0)$mm(w2)$add(b2))
  y_hat2 <- softmax(x$mm(w12)$add(b12)$clamp(min = 0)$mm(w22)$add(b22))

  ### -------- Likelihood --------
  likelihood <- y_hat$log()$multiply(y)$sum()
  likelihood2 <- y_hat2$log()$multiply(y)$sum()

  ### -------- Prior --------

  prior1 <- torch_tensor(0)$subtract(w1)$square()$divide(2)$subtract(torch_log(2*pi))$sum()
  prior2 <- torch_tensor(0)$subtract(w2)$square()$divide(2)$subtract(torch_log(2*pi))$sum()
  prior3 <- torch_tensor(0)$subtract(b1)$square()$divide(2)$subtract(torch_log(2*pi))$sum()
  prior4 <- torch_tensor(0)$subtract(b2)$square()$divide(2)$subtract(torch_log(2*pi))$sum()

  prior <- prior1$add(prior2)$add(prior3)$add(prior4)

  prior12 <- torch_tensor(0)$subtract(w12)$square()$divide(2)$subtract(torch_log(2*pi))$sum()
  prior22 <- torch_tensor(0)$subtract(w22)$square()$divide(2)$subtract(torch_log(2*pi))$sum()
  prior32 <- torch_tensor(0)$subtract(b12)$square()$divide(2)$subtract(torch_log(2*pi))$sum()
  prior42 <- torch_tensor(0)$subtract(b22)$square()$divide(2)$subtract(torch_log(2*pi))$sum()

  priort2 <- prior12$add(prior22)$add(prior32)$add(prior42)

  ### -------- Posterior --------
  posterior1 <- w1$subtract(mu1)$square()$divide(s1$square()$multiply(2))$subtract(s1$log())$subtract(torch_log(2*pi))$sum()
  posterior2 <- w2$subtract(mu2)$square()$divide(s2$square()$multiply(2))$subtract(s2$log())$subtract(torch_log(2*pi))$sum()
  posterior3 <- b1$subtract(bmu1)$square()$divide(bs1$square()$multiply(2))$subtract(bs1$log())$subtract(torch_log(2*pi))$sum()
  posterior4 <- b2$subtract(bmu2)$square()$divide(bs2$square()$multiply(2))$subtract(bs2$log())$subtract(torch_log(2*pi))$sum()

  posterior <- posterior1$add(posterior2)$add(posterior3)$add(posterior4)

  posterior12 <- w12$subtract(mu1)$square()$divide(s1$square()$multiply(2))$subtract(s1$log())$subtract(torch_log(2*pi))$sum()
  posterior22 <- w22$subtract(mu2)$square()$divide(s2$square()$multiply(2))$subtract(s2$log())$subtract(torch_log(2*pi))$sum()
  posterior32 <- b12$subtract(bmu1)$square()$divide(bs1$square()$multiply(2))$subtract(bs1$log())$subtract(torch_log(2*pi))$sum()
  posterior42 <- b22$subtract(bmu2)$square()$divide(bs2$square()$multiply(2))$subtract(bs2$log())$subtract(torch_log(2*pi))$sum()

  posteriort2 <- posterior12$add(posterior22)$add(posterior32)$add(posterior42)

  ### -------- compute loss --------
  loss <- posterior$subtract(prior)$subtract(likelihood)$add(posteriort2$subtract(priort2)$subtract(likelihood2))
  if (t %% 10 == 0 | t < 10)
    cat("Epoch: ", t, "   Loss: ", loss$item(), "\n")

  ### -------- Backpropagation --------

  # compute gradient of loss w.r.t. all tensors with requires_grad = TRUE
  loss$backward()

  ### -------- Update weights --------

  # Wrap in with_no_grad() because this is a part we DON'T
  # want to record for automatic gradient computation
  with_no_grad({
    mu1 <- mu1$sub_(learning_rate * mu1$grad)
    mu2 <- mu2$sub_(learning_rate * mu2$grad)
    rho1 <- rho1$sub_(learning_rate * rho1$grad)
    rho2 <- rho2$sub_(learning_rate * rho2$grad)
    bmu1 <- bmu1$sub_(learning_rate * bmu1$grad)
    bmu2 <- bmu2$sub_(learning_rate * bmu2$grad)
    brho1 <- brho1$sub_(learning_rate * brho1$grad)
    brho2 <- brho2$sub_(learning_rate * brho2$grad)

    # Zero gradients after every pass
    mu1$grad$zero_()
    mu2$grad$zero_()
    rho1$grad$zero_()
    rho2$grad$zero_()
    bmu1$grad$zero_()
    bmu2$grad$zero_()
    brho1$grad$zero_()
    brho2$grad$zero_()
  })

Comment: You may be looking for an automatic differentiation package in R. The purpose of an automatic differentiation is to compute partial derivatives, gradients, Hessians, and even Jacobians at specific points using the forward computations. [torch in R](https://torch.mlverse.org/) is the R port of PyTorch, which is an excellent automatic differentiation package. See [this](https://torch.mlverse.org/technical/autograd/) page for more details on automatic differentiation using torch in R.
Comment: I have been trying to learn torch for a week now. I'm sure it would be possible to use it for this purpose, but i am not yet experienced enough with it to make it work. But i will continue to learn about it.
Comment: How about you edit your question to show us what you've tried so far in torch? By the way, if you are not constrained to using R, then it would likely be more convenient for you to use PyTorch in Python instead.
Comment: allright, i'll need a minute to type it up. unfortunately i'm constrained to using R, i'm working on this paper at an institute where R is the working language by policy.
Comment: @mhdadk i have added my working in torch so far. i'm stuck on the posterior and prior. Also, while i have run each line so far to see if they work, i have not run the entire loop yet (due to the missing posterior and prior). So i don't know if there are other mistakes in the structure.
Answer: After equation 2 in the paper, the author writes:

where $\mathbf{w}^{(i)}$ denotes the $i^{th}$ Monte Carlo sample drawn from the variational posterior $q(\mathbf{w}^{(i)}|\theta)$.

In section 3.2 titled "Gaussian variational posterior":

Suppose that the variational posterior is a diagonal Gaussian distribution,  then a sample of the weights $\mathbf{w}$ can be obtained by sampling a unit Gaussian, shifting it by a mean $\mu$ and scaling by a standard deviation $\sigma$. We parameterise the standard deviation pointwise as $\sigma = \log(1 + \exp(\rho))$ and so $\sigma$ is always non-negative. The variational posterior parameters are $\theta = (\mu,\rho)$. Thus the transform from a sample of parameter-free noise and the variational posterior parameters that yields a posterior sample of the weights $\mathbf{w}$ is: $\mathbf{w} = t(\theta,\epsilon) = \mu + \log(1 + \exp(\rho)) \circ \epsilon$ where $\circ$ is point-wise multiplication.

However, in your question you wrote
\begin{align}
\text{prior} &= \log(q(\mathbf{w}|\mu,\rho)) = \sum_i \log(p(w_i | 0, 1)) \\
\text{posterior} &= \log(P(\mathbf{w})) = \sum_i \log(p(w_i | \mu, \sigma^2)) \\
\text{likelihood} &= \log(P(\mathcal{D}|\mathbf{w})) = y \cdot \log(\text{softmax}(\hat{y}))
\end{align}
which does not seem to be correct, since $q(\mathbf{w}|\mu,\rho)$ is the posterior according to the author. Here is what I think the authors meant
\begin{align}
\text{posterior} &= q(\mathbf{w}|\theta) \\
\text{prior} &= P(\mathbf{w}) \\
\text{likelihood} &= P(\mathcal{D}|\mathbf{w})
\end{align}
Also, the authors did not specify the prior $P(\mathbf{w})$ until equation 7 in section 3.3, which is a mixture of Gaussians and not a standard Gaussian as you wrote. Just wanted to point this out.
Here is a rough outline of what you can do in torch to implement this:

Create the dataset $\mathcal{D}$ consisting of the vectors $\mathbf{x}_1,\mathbf{x}_2,\dots,\mathbf{x}_N$ and the labels $y_1,y_2,...,y_N$. This can be done by sampling $\mathbf{x}$ vectors from some multivariate distribution, and sampling $y$ labels from a Bernoulli distribution.
Sample $\epsilon$ from the multivariate normal distribution $N(0,I)$.
Sample initial values for $\mu$ and $\rho$ from the multivariate normal distribution $N(0,I)$ . The authors do not say anything about whether $\mu$ and $\rho$ are themselves random variables with an associated distribution $p(\theta)$ or are treated as unknown constants. For simplicity, I will assume they are treated as unknown constants. In that case, their initial values can be sampled from any distribution. For simplicity, you can sample them using the torch_randn function. You only need to do this once to start performing gradient descent. Note that $\mu$ is a column vector and $\rho$ is a diagonal matrix also a column vector because the authors use the element-wise multiplication operation $\circ$ in their paper to multiply $\epsilon$ by $\log(1 + \exp(\rho))$.
Compute $\mathbf{w} = \mu + \log(1 + \exp(\rho)) \circ \epsilon$.
Compute $\log(q(\mathbf{w}|\mu,\rho))$ by inputting the $\mathbf{w}$,$\mu$, and $\rho$ that you obtained in steps 3 and 4 into the multivariate normal probability density function.
Compute $\log(P(\mathbf{w}))$ by inputting the $\mathbf{w}$ that you obtained in step 3 into the multivariate normal probability density function with mean $\mathbf{0}$ and covariance $I$. Note again that this does not follow what the authors did in the paper, and that you would instead need to implement equation 7 in the paper.
Compute $\log(P(\mathcal{D}|\mathbf{w}))$. Note that you did not specify what $\hat{y}$ is. For the sake of simplicity, I will assume that $\hat{y} = \mathbf{w}^T \mathbf{x}$. This means that $\log(P(\mathcal{D}|\mathbf{w})) = -\sum_{i=1}^N y_i \cdot \log(\hat{y}_i)$. This is just the cross-entropy between the $y$ labels and the $\hat{y}$ labels. Note that each $y$ and $\hat{y}$ must range between 0 and 1.
Compute $f(\mathbf{w},\theta) = \log(q(\mathbf{w}|\theta)) - \log(P(\mathbf{w})) - \log(P(\mathcal{D}|\mathbf{w}))$.
Compute the gradients as given in steps 5 and 6 of the algorithm.
Update $\mu$ and $\rho$ as given in step 7 of the algorithm.
Repeat the following aforementioned steps until convergence: step 2 (sampling $\epsilon$) $\rightarrow$ step 4 (computing $\mathbf{w}$) $\rightarrow$ steps 5,6,7,8,9, and 10.

I am aware that I did not mention how to compute the gradients in torch. If that is still something that you are not sure about, let me know.
Response to comments

What is still unclear to me is how to design the hidden layers of neurons. Am i correct in understanding that if i have 100 observations, for (2) this means i sample 100 $\epsilon$'s from a mv normal distribution with $\mu=0$ and a 100x100 identity matrix? for (3) i sample 100 $\mu$'s as a column vector and $\rho$ is 100x100 diagonal matrix of 100 samples of the same mv normal distribution? and for (4) i will get 100 samples $\mathbf{w}$? For my hidden layer i have specified 32 neurons. Where do these come into play?

Suppose you have $N$ observations in your dataset $\mathcal{D}$ of $(\mathbf{x},y)$ pairs, as discussed in step 1 above, and suppose that each $\mathbf{x}$ is $K$-dimensional such that
$$
\mathbf{x} =
\begin{bmatrix}
x_1 \\
x_2 \\
\vdots \\
x_K
\end{bmatrix}
$$
Also, for the sake of simplicity, suppose that your network consists of a single layer and the output of your network is
$$
\hat{y} = \phi(\mathbf{W}^T \mathbf{x} + \mathbf{b})
$$
where $\mathbf{W}$ is a $K \times M$ matrix of weights, where $M$ is the number of possible classes, $\mathbf{b}$ is a $M \times 1$ column vector, and $\phi(\cdot)$ is the softmax function. Recall that the authors wrote
$$
\mathbf{w} = \mu + \log(1 + \exp(\rho)) \circ \epsilon
$$
This be generalized to a $K \times M$ matrix $\mathbf{W}$ by sampling a $K \times M$ matrix $\epsilon$ using torch_randn, sampling initial values (only need to sample once) of $K \times M$ matrices $\mu$ and $\rho$ using torch_randn. Since $\epsilon \sim N(0,I)$, then you can also sample a $K \times M$ matrix $\epsilon$ using torch_randn. You can then obtain $\mathbf{W}$ as shown above. Each row of $\mathbf{W}$ represents a single weight vector. You can repeat this process using column vectors for $\mu,\rho,$ and $\epsilon$ to obtain $\mathbf{b}$:
$$
\mathbf{b} = \mu + \log(1 + \exp(\rho)) \circ \epsilon
$$
You can then compute $\hat{y}$ as shown above.

For step 7, $\hat{y}$ is supposed to represent the predicted values, which are compared to the ground truth $y$. So i think we're talking about the same thing here. You mention both $y$ and $\hat{y}$ have to range between 0 and 1, does that mean i should apply a sigmoid activation function after the output layer?

I think we have slightly different definitions of $\hat{y}$. I am defining $\hat{y}$ as
$$
\hat{y} = \phi(\mathbf{W}^T \mathbf{x} + \mathbf{b})
$$

I'm not experienced in R, so here is my attempt using Python and PyTorch. You can (almost) translate quickly between the two by substituting . in Python with $ in R. I don't get NaN values, but the f loss does not decrease consistently. I commented my code as much as possible for clarity. Hope this helps.
import torch
import math

# define pi
pi = torch.tensor(math.pi)

def log_likelihood(x,mu,rho):
    # need to compute negative log-likelihood and NOT the
    # log-likelihood. This is because we are performing gradient descent.
    # In gradient descent, we are trying to minimize a function.
    # Minimizing the negative log-likelihood function is equivalent to
    # maximizing the log-likelihood function.

    # need to re-parameterize sigma to keep it positive. See the first
    # paragraph of section 3.2 in the paper and section 3.1 in the paper
    # for details
    sigma = torch.log(1 + torch.exp(rho))

    return -torch.log(sigma * torch.sqrt(2 * pi)) - (0.5 * (torch.div(x-mu,sigma) ** 2))

# uncomment one or the other to choose your activation function
act_func = torch.nn.Sigmoid()
# act_func = torch.nn.ReLU()

# the logarithm of the softmax function. See
# https://pytorch.org/docs/stable/generated/torch.nn.LogSoftmax.html
# for details
log_softmax = torch.nn.LogSoftmax(dim = 0)

learning_rate = 1e-5

# STEP 1
# create the dataset D consisting of vectors x_1,x_2,...,x_N and labels
# y_1,y_2,...,y_N

# input dimensionality (number of input features)
d_in = 3
# dimensionality of the hidden layer
d_hidden = 32
# output dimensionality (number of predicted features)
d_out = 1
# number of observations in training set
n = 100
# number of training iterations
num_iter = 100

# create input data. This is a (N x d_in) matrix, where N is the number of
# observations and d_in is the dimensionality of the input
X = torch.randn(n,d_in)

# generate the ground truth with a bernoulli distribution with probability
# p = 0.6
p = 0.6
y = torch.bernoulli(p * torch.ones((n,)))

# In STEP 2, I will sample the required mu and rho matrices and column
# vectors. Since the authors in the paper do not specify p(mu,rho)in the
# paper, then mu and rho are both treated here as unknown constants and
# not as random variables. In this case, I can sample initial values for
# mu and rho from any distributuon. For simplicity, I will use the uniform
# distribution, which can be done using the torch.rand (not torch.randn)
# function.
#
# Here are the weight matrices and bias vectors I am trying to construct
# in STEP 2:
#
#   weight matrix for the first layer:
#       W1 = mu_W1 + log(1 + exp(rho_W1)) * epsilon_W1
#   bias vector for the first layer:
#       b1 = mu_b1 + log(1 + exp(rho_b1)) * epsilon_b1
#   weight matrix for the second layer:
#       W2 = mu_W2 + log(1 + exp(rho_W2)) * epsilon_W2
#   bias vector for the second layer:
#       b2 = mu_b2 + log(1 + exp(rho_b2)) * epsilon_b2

# STEP 2a
# sample mu matrix for the first layer with dimensions d_in x d_hidden,
# where d_hidden is the dimensionality of the hidden layer. This mu
# matrix will be used to construct W1, which is the d_in x d_hidden weight
# matrix for the first layer. Note that since I need to compute the
# gradient of f(W,theta) with respect to mu_W1, then I will need to
# set requires_grad = True for mu_W1.

mu_W1 = torch.randn(d_in,d_hidden,requires_grad = True)

# STEP 2b
# sample rho matrix for the first layer with dimensions d_in x d_hidden,
# where d_hidden is the dimensionality of the hidden layer. This rho
# matrix will be used to construct W1, which is the d_in x d_hidden weight
# matrix for the first layer. Note that since I need to compute the
# gradient of f(W,theta) with respect to rho_W1, then I will need to
# set requires_grad = True for rho_W1.

rho_W1 = torch.randn(d_in,d_hidden,requires_grad = True)

# STEP 2c
# sample mu column vector for the first layer with dimensions d_hidden x 1,
# where d_hidden is the dimensionality of the hidden layer. This mu column vector
# will be used to construct b1, which is the d_hidden x 1 bias
# vector for the first layer. Note that since I need to compute the
# gradient of f(W,theta) with respect to mu_b1, then I will need to
# set requires_grad = True for mu_b1.

mu_b1 = torch.randn(d_hidden,1,requires_grad = True)

# STEP 2d
# sample rho column vector for the first layer with dimensions d_hidden x 1,
# where d_hidden is the dimensionality of the hidden layer. This rho column vector
# will be used to construct b1, which is the d_hidden x 1 bias
# vector for the first layer. Note that since I need to compute the
# gradient of f(W,theta) with respect to rho_b1, then I will need to
# set requires_grad = True for rho_b1.

rho_b1 = torch.randn(d_out,1,requires_grad = True)

# sample the rest of the matrices and column vectors

mu_W2 = torch.randn(d_hidden,d_out,requires_grad = True)
rho_W2 = torch.randn(d_hidden,d_out,requires_grad = True)
mu_b2 = torch.randn(d_out,1,requires_grad = True)
rho_b2 = torch.randn(d_out,1,requires_grad = True)

for i in range(num_iter):

    # STEP 3a
    # sample epsilon matrix for the first layer with dimensions d_in x d_hidden,
    # where d_hidden is the dimensionality of the hidden layer. This epsilon
    # matrix will be used to construct W1, which is the d_in x d_hidden weight
    # matrix for the first layer. Note that since I do not need to compute the
    # gradient of f(W,theta) with respect to epsilon_W1, then I don't need to
    # set requires_grad = True for epsilon_W1. This will save memory.

    epsilon_W1 = torch.randn(d_in,d_hidden)

    # STEP 3b
    # sample epsilon column vector for the first layer with dimensions d_hidden x 1,
    # where d_hidden is the dimensionality of the hidden layer. This epsilon
    # matrix will be used to construct b1, which is the d_hidden x 1 bias vector
    # for the first layer. Note that since I do not need to compute the
    # gradient of f(W,theta) with respect to epsilon_b1, then I don't need to
    # set requires_grad = True for epsilon_b1. This will save memory.

    epsilon_b1 = torch.randn(d_hidden,1)

    # STEP 3c
    # sample epsilon matrix for the second layer with dimensions d_hidden x d_out,
    # where d_out is the dimensionality of the output layer. This epsilon
    # matrix will be used to construct W2, which is the d_hidden x d_out weight
    # matrix for the second layer. Note that since I do not need to compute the
    # gradient of f(W,theta) with respect to epsilon_W2, then I don't need to
    # set requires_grad = True for epsilon_W2. This will save memory.

    epsilon_W2 = torch.randn(d_hidden,d_out)

    # STEP 3d
    # sample epsilon column vector for the second layer with dimensions d_out x 1,
    # where d_out is the dimensionality of the output layer. This epsilon
    # column vector will be used to construct b2, which is the d_out x 1 bias
    # vector for the second layer. Note that since I do not need to compute the
    # gradient of f(W,theta) with respect to epsilon_b2, then I don't need to
    # set requires_grad = True for epsilon_b2. This will save memory.

    epsilon_b2 = torch.randn(d_out,1)

    # STEP 4a
    # compute W1 = mu_W1 + log(1 + exp(rho_W1)) * epsilon_W1

    W1 = mu_W1 + torch.mul(
                            torch.log(1 + torch.exp(rho_W1)),
                            epsilon_W1
                          )

    # STEP 4b
    # compute b1 = mu_b1 + log(1 + exp(rho_b1)) * epsilon_b1

    b1 = mu_b1 + torch.mul(
                            torch.log(1 + torch.exp(rho_b1)),
                            epsilon_b1
                          )

    # STEP 4c
    # compute W2 = mu_W2 + log(1 + exp(rho_W2)) * epsilon_W2

    W2 = mu_W2 + torch.mul(
                            torch.log(1 + torch.exp(rho_W2)),
                            epsilon_W2
                          )

    # STEP 4d
    # compute b2 = mu_b2 + log(1 + exp(rho_b2)) * epsilon_b2

    b2 = mu_b2 + torch.mul(
                            torch.log(1 + torch.exp(rho_b2)),
                            epsilon_b2
                          )

    # STEP 5a
    # compute log(q(W1|mu_W1,rho_W1))

    posterior_W1 = log_likelihood(W1,mu_W1,rho_W1).sum()

    # STEP 5b
    # compute log(q(b1|mu_b1,rho_b1))

    posterior_b1 = log_likelihood(b1,mu_b1,rho_b1).sum()

    # STEP 5c
    # compute log(q(W2|mu_W2,rho_W2))

    posterior_W2 = log_likelihood(W2,mu_W2,rho_W2).sum()

    # STEP 5d
    # compute log(q(b2|mu_b2,rho_b2))

    posterior_b2 = log_likelihood(b2,mu_b2,rho_b2).sum()

    # STEP 6a
    # compute log(P(W1)). Note that since sigma = log(1 + exp(rho)), and
    # if sigma = 1, then rho = log(exp(1) - 1)

    rho = torch.log(torch.exp(torch.tensor(1.)) - 1)
    prior_W1 = log_likelihood(W1,0,rho).sum()

    # STEP 6b
    # compute log(P(b1))

    prior_b1 = log_likelihood(b1,0,rho).sum()

    # STEP 6c
    # compute log(P(W2))

    prior_W2 = log_likelihood(W2,0,rho).sum()

    # STEP 6d
    # compute log(P(b2))

    prior_b2 = log_likelihood(b2,0,rho).sum()

    # STEP 7
    # compute log(P(D|W1,W2,b2,b1)), which is the negative of the
    # cross-entropy between all y's and y_hat's in the dataset D. Recall
    # that the cross-entropy between all y's and y_hat's in a dataset of
    # size N is:
    #
    #   - \sum_{i=1}^N y_i \cdot log(\hat{y}_i)

    cross_entropy = 0

    # iterte over each row of the X matrix to compute the cross-entropy,
    # since each row is a single x vector. Note that this method is
    # inefficient, since batch matrix multiplication would be more
    # efficient. However, this is for illustrative purposes only.

    for j in range(X.shape[0]):

        # extract the input vector x and convert it into a column vector
        x = X[j,:].unsqueeze(-1)

        # output of first layer. Note that W1.T means transpose of W1
        out1 = act_func(torch.matmul(W1.T,x) + b1)

        # output of second layer. Note that the logarithm of the softmax
        # function is computed here. The .squeeze() method is used to
        # remove any extra dimensions
        log_y_hat = log_softmax(torch.matmul(W2.T,out1) + b2).squeeze()

        # accumulate cross entropy
        cross_entropy = cross_entropy + (y[j] * log_y_hat)

    # STEP 8
    # compute f(w,theta). Note that since W1,b1,W2, and b2 are assumed to
    # be conditionally independent given their corresponding parameters mu
    # and rho, then:
    #
    #   log(q(W1,b1,W2,b2|mu_W1,rho_W1,mu_W2,rho_b2)) =
    #           log(q(W1|mu_W1,rho_W1)) + log(q(b1|mu_b1,rho_b1)) +
    #           log(q(W2|mu_W2,rho_W2)) + log(q(b2|mu_b2,rho_b2))
    #
    # Note that log(P(D|W1,W2,b2,b1)) is the cross-entropy computed above.

    f = (posterior_W1 + posterior_b1 + posterior_W2 + posterior_b2
       - prior_W1 - prior_b1 - prior_W2 - prior_b2
       + cross_entropy)

    # STEP 9
    # compute gradients as shown in steps 5 and 6

    f.backward()
    # Delta_mu = (W1.grad + b1.grad + W2.grad + b2.grad
    #           + mu_W1.grad + mu_b1.grad + mu_W2.grad + mu_b2.grad)
    # Delta_rho = ((W1.grad * (epsilon_W1 / (1 + torch.exp(-rho_W1)))
    #            + b1.grad * (epsilon_b1 / (1 + torch.exp(-rho_b1)))
    #            + W2.grad * (epsilon_W2 / (1 + torch.exp(-rho_W2)))
    #            + b2.grad * (epsilon_b2 / (1 + torch.exp(-rho_b2))))
    #            + rho_W1.grad + rho_b1.grad + rho_W2.grad + rho_b2.grad)

    # STEP 10
    # update mu and rho using gradient descent
    with torch.no_grad():
        mu_W1 -= mu_W1.grad * learning_rate
        mu_b1 -= mu_b1.grad * learning_rate
        mu_W2 -= mu_W2.grad * learning_rate
        mu_b2 -= mu_b2.grad * learning_rate
        mu_W1.grad.zero_()
        mu_b1.grad.zero_()
        mu_W2.grad.zero_()
        mu_b2.grad.zero_()
        rho_W1 -= rho_W1.grad * learning_rate
        rho_b1 -= rho_b1.grad * learning_rate
        rho_W2 -= rho_W2.grad * learning_rate
        rho_b2 -= rho_b2.grad * learning_rate
        rho_W1.grad.zero_()
        rho_b1.grad.zero_()
        rho_W2.grad.zero_()
        rho_b2.grad.zero_()
```

Comment: @JadaLovelace responded to your comments at the end of my answer.
Comment: @JadaLovelace edited to clarify.
Comment: So i managed to get the network partly running, which i think means that the math part is in order. The network learns for about 20 iterations and then returns NaN for the loss, so i need to figure that out. Thank you for all your help! I wouldn't have been able to get here without you.
Comment: Thank you for the corrections. I did indeed not implement the scale mixture prior yet, because i was trying to build this network step by step so that i would fully understand each part of it. But you are right of course, i will implement it.
Comment: What is still unclear to me is how to design the hidden layers of neurons. Am i correct in understanding that if i have 100 observations, for (2) this means i sample 100 $\epsilon$'s from a mv normal distribution with $\mu=0$ and a 100x100 identity matrix? for (3) i sample 100 $\mu$'s as a column vector and $\rho$ is 100x100 diagonal matrix of 100 samples of the same mv normal distribution? and for (4) i will get 100 samples $w$?

For my hidden layer i have specified 32 neurons. Where do these come into play?
Comment: since this method uses autograd, if this ends up working i don't need to compute the gradients and i'll still award the bounty.
Comment: For step 7, $\hat{y}$ is supposed to represent the predicted values, which are compared to the ground truth $y$. So i think we're talking about the same thing here. You mention both $y$ and $\hat{y}$ have to range between 0 and 1, does that mean i should apply a sigmoid activation function after the output layer?
Comment: `where these are now column vectors instead of matrices`.

wait i have matrices for $\mu$ and $\rho$. If i create $b$ in the same way as $w$ then i get matrices again, not column vectors. how are they turned into column vectors?
Comment: In your post you wrote $f(w,θ)=log(q(w|θ))−log(P(w))−log(P(D|w))$. Is that correct? Shouldn't it be $f(w,θ)=log(q(w|θ))−log(P(w))*P(D|w)$? That's the function that i see in the paper.
Comment: I have coded all your instructions into Torch for R, i've edited it at the end of the main post. I'm still working out what goes wrong, because when i run the training loop only the first loop gives a loss number (an incredibly big negative number like $-4.30*10^8$ or something similar) and every iteration after that just gives NaN.
Comment: @JadaLovelace I've added my attempt at the problem using Python and PyTorch. Please see the edit. Please let me know if you end up getting it to work properly. Good luck!
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/127207/discussion-between-mhdadk-and-jadalovelace).


Python Mixed Integer Linear Programming

Question: Are there any Mixed Integer Linear Programming(MILP) solver for Python?
Can GLPK python solve MILP problem? I read that it can solve Mixed integer problem.
I am very new to linear programming problem. So i am rather confused and cant really differentiate if Mixed Integer Programming is different from Mixed Integer Linear programming(MILP).
Comment: i am not too sure why the down vote, but i have actually searched for hours, and was really not sure whether MILP is same as MIP.
Answer: Pulp is a python modeling interface that hooks up to solvers like CBC(open source), CPLEX (commercial), Gurobi(commercial), XPRESS-MP(commercial) and YALMIP(open source).
You can also use Pyomo to model the optimization problem and then call an external solver, namely CPLEX, Gurobi GLPK and the AMPL solver library.
You can also call GLPK from GLPK/Python, PyGLPK or PyMathProg.
Yet another modelling language is CMPL, which has a python interface for MIP solvers (for linear programs only).
All the above solvers solve Mixed Integer Linear Programs, while some of them (CPLEX, GUROBI and XRESS-MP for sure) can solve Mixed Integer Quadratic Programs and Quadratically constrained quadratic programs (and also conic programs but this probably goes beyond the scope of this question).
MIP refers to Mixed integer programs, but it is commonly used to refer to linear programs only. To make the terminology more precise, one should always refer to MILP or MINLP (Mixed integer non-linear programming).
Note that CPLEX and GUROBI have their own python APIs as well, but they (and also) XPRESS-MP are commercial products, but free for academic research. CyLP is similar to Pulp above but interfaces with the COIN-OR solvers CBC and CGL and CLP.
Note that there is a big difference in the performance of commercial and free solvers: the latter are falling behind the former by a large margin. SCIP is perhaps the best non-commercial solver (see below for an update). Its python interface, PySCIPOpt, is here.
Also, have a look at this SO question.
Finally, if you are interested at a simple constraint solver (not optimization) then have a look at python-constraint.
UPDATES
More solvers and python interfaces that fell into my radar:
Update: MIPCL links appear to be broken.

MIPCL, which appears to be the fastest non-commercial MIP solver, has a python interface that has quite good documentation. Note, however, that the Python API does not include the advanced functionality that comes together with the native MIPCLShell. I particularly like the MIPCL-PY manual, which demonstrates an array of models used in Operations Management, on top of some small-scale implementations. It is a very interesting introductory manual in its own right, regardless of which solver/API one may want to make use of.

Google Optimization Tools, which include a multitude of functionalities, such as

A constraint programming solver and a linear programming (not MIP) solver
An interface for MIP solvers (supports CBC, CLP, GLOP, GLPK, Gurobi, CPLEX, and SCIP)
Specialized algorithms for graphs, for the Travelling Salesman Problem, the Vehicle Routing problem and for Bin packing & Knapsack problems

It has extensive documentation of several traditional OR problems and simple implementations. I could not find a complete Python API documentation, although there exist some examples here. It is somewhat unclear to me how other solvers hook up on the interface and whether methods of these solvers are available.
CVXOPT, an open-source package for convex optimization, which interfaces to GLPK (open source) and MOSEK
(commercial). It is versatile, as it can tackle many problem classes (notably linear, second-order, semidefinite, convex nonlinear). The only disadvantage is that it modeling complex problems may be cumbersome, as the user needs to pass the data in a "Matlab-y" fashion (i.e., to specify the matrix, rhs vectors, etc). However, it can be called from the modeling interfaces PICOS and...
CVXPY, a python-embedded optimization language for convex optimization problems, which contains CVXOPT as a default solver, but it can hook up to the usual MIP solvers.
Thanks to RedPanda for pointing out that CVXOPT/CVXPY support MIP solvers as well.
For a very comprehensive article on optimization modeling capabilities of packages and object-oriented languages (not restricted to Python), check this article.
Comment: By the way, there is vastly imporved Python interface for SCIP that can be found here: https://github.com/SCIP-Interfaces/PySCIPOpt
Comment: @OliverAngelil The complexity of a MIP solver is not comparable with that of regular numerical analysis subroutines, such as array operations, solving systems of equations etc. This is mainly for two reasons. First, there is a lot of historical knowledge embedded in the solvers, spanning decades of rigorous research and implementation-specific details. Second, solving MIP problems is an active area of research, with scholars pushing the boundaries of what can be solved year on year. Commercial solvers implement the new advances regularly, updating their versions about once per year.
Comment: I don't think Gurobi is open source as stated in the first sentence of the answer.
Comment: What's the basis for the claim that MIPCL is the fastest?  These benchmarks do seem to confirm your claim: http://plato.asu.edu/talks/ismp2018.pdf .  Unfortunately the list doesn't distinguish between commercial/non-commercial.  (As far as I can tell, GLPK, CBC, lp_solve, MIPCL are fully open-source, SCIP is non-commercial.)
Comment: Has MIPCL died? The links don't work any more.
Comment: That's a shame.  http://www.cvxpy.org/en/latest/tutorial/advanced/index.html#mixed-integer-programs is also broken (you have it as a link) So is http://www.cvxpy.org/en/latest/tutorial/intro/index.html
Comment: Thanks for noticing @Anush; I have updated the corresponding links.
Comment: `cvxopt` and `cvxpy` also support mixed integer programming. Here's an article benchmarking `cvxopt` as much faster than `pulp`: https://scaron.info/blog/linear-programming-in-python-with-cvxopt.html
Comment: @loannis "Note that there is a big difference in the performance of commercial and free solvers: the latter are falling behind the former by a large margin". I've also noticed. Do you know why this is? For almost any other computing task (array operations, machine learning, etc), I've found the free options to be competitive. Why not here?
Comment: @loannis If only the last one is open source, maybe also get rid of "the open source". Or is CBC also open source?
Comment: @Anush Indeed, MIPCL links appear to be broken. There is a github repo (see [this](https://github.com/onebitbrain/MIPCL), but installation seems to be troublesome [here](https://stackoverflow.com/questions/59851834/trouble-with-installing-mipcl-py-open-source-optimization-solver-for-python) SO post). At the time, it used to be a good choice, but I would opt for another alternative today.
Comment: @loannis Thanks for this excellent answer, I learned a lot. I have a follow-up questions about MILP solvers,  Is there an open source MILP solver that can output top N best solutions rather than the global best?
Comment: @Chemmyyu Glad you found the answer helpful. Some solvers (CPLEX, Gurobi) allow you to collect the solutions found through the search (e.g., check [here](https://www.ibm.com/docs/en/icos/20.1.0?topic=pool-example-calling-populate) and [here](https://orinanobworld.blogspot.com/2013/01/finding-all-mip-optima-cplex-solution.html)). For certain models, we may be able to retrieve other optimal solutions (e.g., if symmetries exist), or iteratively rule out solutions. Also, check Paul Rubin's blog (a treasure trove for MIP optimization) [here](https://orinanobworld.blogspot.com/).
Comment: hey, thanks for the reply. I have searched many of the stuffed you posted. Even installed two of them(PuLP and GLPK) before posting this. Because of the naming, i was rather confused between MILP and MIP. Anyway, thanks for the clarification. Appreciated.
Comment: @mattmilten Link updated. Thanks for working on this!
Answer: Soon there will be another option: Starting from version 1.9.0, SciPy will support MILP. See scipy.optimize.milp in the dev docs. The SciPy milp implementation is a wrapper of the HiGHS linear optimization software. It was added in this PR on February 16th, 2022.
Edit: SciPy 1.9.0 was released on July 29, 2022, with scipy.optimize.milp.
Answer: I have used Gekko Python Package to solve MILP problems. You can either solve your models locally or on their remote server. Below is an example after installing with pip install gekko:
from gekko import GEKKO
m = GEKKO()
x,y = m.Array(m.Var,2,integer=True,lb=0)
m.Maximize(y)
m.Equations([-x+y<=1,
             3*x+2*y<=12,
             2*x+3*y<=12])
m.options.SOLVER = 1
m.solve()
print('Objective: ', -m.options.OBJFCNVAL)
print('x: ', x.value[0])
print('y: ', y.value[0])

GEKKO is a Python package for machine learning and optimization of mixed-integer and differential algebraic equations. It is coupled with large-scale solvers for linear, quadratic, nonlinear, and mixed integer programming (LP, QP, NLP, MILP, MINLP). Modes of operation include parameter regression, data reconciliation, real-time optimization, dynamic simulation, and nonlinear predictive control. GEKKO is an object-oriented Python library to facilitate local execution of APMonitor.
Comment: Here are code examples of MILP solved with `gekko` using dense and sparse matrices: https://apmonitor.com/wiki/index.php/Main/IntegerProgramming

Python Mixed Integer Linear Programming


Customizable product bundles, user can add/remove products

Question: I'm looking for ways to implement an e-commerce for an organic farmer who sells their products, he offers products and product bundles. Every product bundles has its own price. Each bundle are predefined products group. But I want that when the user buys, can unchoose at most, two products of the bundle and change them for another product in a predefined group of products.
I wonder if anyone knows how I can work this way the product bundles. I've been searching, I saw Tech Talk about commerce bundles, I tried commerce_bundle... but I can't find anything to do this.
Any idea or clue is welcome!
Answer: My solution goes here based on Drupal Commerce-
Step 1: Create Product Bundles using Commerce Bundle module. (which you already know).
Step 2: Now you have bundles. The next step is your custom functionality to swap at most two products from the bundle.
Add a new step to your checkout process to implement the custom functionality refer Add a new step in Commerce checkout, explore commerce_checkout.api to get the hold on this.
Adding a new step is nothing but adding a new pane (inside a checkout page) in a Drupal Commerce checkout process where you can list the products in the cart and process them using hook_form_alter().
Refer this link for more details hook_form_alter.
You can make use of hooks provided by Drupal Commerce such as commerce_cart.api, commerce_order.api to edit/update the cart.
Step 3: After processing the cart contents you can update the order details/status using commerce_order.api.
You can alter the checkout pane settings from Home » Administration » Store » Configuration » Checkout Settings page.
Note: You must have Drupal 7 module development knowledge to implement the above solution.
I hope this helps! Thank You.


Where are admin/config/user-interface/archive setting stored

Question: Where are these settings stored at. On the disk? In a database table?
Answer: Generally, configurations in administrative pages are saved into variable table.
The path admin/config/user-interface/archive is not a Drupal core path. It must have been provided by a module. If you want to get a definitive answer, you should provide more information.
Comment: Generally you can just look at the inputs @Alex. The `name` attribute specifically will usually correspond to the name of the variable in the DB table, assuming the form's been built with `system_settings_form`. The values are stored as PHP serialised strings, though, so updating them via plain SQL could be quite a task
Comment: I need to update this data but I can only do it via a sql update. I can't do it via the user interface. The form tag uses the id tag of "archive-admin-settings" but I don't see that in the variable table.


Guice: what happens when I have excessive @Inject on a constructor

Question: I have a class SomeClass and a SomeModule to register it with Guice.
I found out that the constructor SomeClass is only called by SomeModule, and that SomeModule is the only place where binding for SomeClass happens.
This means that the @Inject on SomeClass's constructor is not needed since prop1 and prop2 are injected inside SomeModule and passed to the constructor. And the testing also seems to prove my findings.
My question is that what Guice will do when it sees a @Inject like this?
Also what side effect will there be if I have an excessive @Inject?
public static class SomeModule extends PrivateModule {

    @Provides
    @Singleton
    @Exposed
    private SomeClass someClass( SomeObject prop1, String prop2) {
        return new SomeClass(prop1, prop2);
    }
}

public class SomeClass {

    @Inject // unnecessary
    public SomeClass(SomeObject prop1, String prop2){
        ...
    }
}

If my understanding is correct, you @Inject a constructor when you want to inject objects managed by Guice into the parameters of the constructor.
e.g. if I have bind(SomeClass.class).in(Singleton.class) and bindings for prop1 and prop2, it makes sense to @Inject SomeClass constructor in order to inject prop1 and prop2 into the constructor.
But since this isn't the case, this makes the @Inject here unnecessary
Cheers
Comment: If you're using the @Provides then yes, it's just collecting dust.  If your company takes a stance that you'll never switch away from the provider, then you probably want to keep things consistent and remove them.
Comment: You need `@Inject` if you were defining a binding in the configure method instead of creating a Provider for this class.

When you define a binding in the configure method, you are telling Guice to instantiate the object for you. That is when it looks for a constructor with `@Inject` to figure out what dependencies it has to Inject to construct that object. In case of Provider, you are creating the object yourself by passing all the dependencies required for that class as arguments. So, `@Inject` has no meaning at that case.
Comment: Thanks @kendavidson .Yes I think it's be better to remove these '@Inject' to keep things clean
Answer: Quoting @Deepak's comment as the answer:

You need @Inject if you were defining a binding in the configure method instead of creating a Provider for this class. When you define a binding in the configure method, you are telling Guice to instantiate the object for you. That is when it looks for a constructor with @Inject to figure out what dependencies it has to Inject to construct that object. In case of Provider, you are creating the object yourself by passing all the dependencies required for that class as arguments. So, @Inject has no meaning at that case


Show the linear functional relating to the solution of ODE is bounded

Question: This is an exercise in the book by B. Daya Reddy.
For $f \in L^2(0, 1)$, let $u_f$ be the solution of the ODE: $u'' + u' - 2u = f$, $u(0) = u(1) = 0$. Define the functional $\ell$ by
$$
    \langle \ell, f \rangle = \int_0^1 u_f(x) dx \ \forall f \in L^2(0, 1)
$$
Show that $\ell$ is a bounded linear functional.
I have shown $\ell$ is linear, but struggle to bound $\ell$. From the ODE
$$
    u'' + u' - 2u = f, u(0) = u(1) = 0
$$
integrate from $0$ to $1$ for both side, we have
$$
    \langle \ell, f \rangle = \int_0^1 u_f(x)dx = \dfrac{u_f'(1) - u_f'(0)}{2} - \dfrac{1}{2}\int_0^1 f(x)dx
$$
Therefore,
$$
   \vert \langle \ell, f \rangle \vert \le \dfrac{\vert u_f'(1) - u_f'(0) \vert}{2} + \dfrac{1}{2}\lVert f \rVert_2
$$
Am I on the right track ? How to bound the term $\dfrac{\vert u_f'(1) - u_f'(0) \vert}{2}$ in term of $L^2$ norm of $f$ ? Any hints are appreciated. Thanks
Comment: ok never mind, we can simply re-word parts of the argument needed to apply Lax-Milgram (setting aside the question of existence), i.e essentially an 'energy estimate'.
Comment: does the MVT help?
Comment: @kipf The only way I can think of MVT for this problem is, $\vert u_f'(1) - u_f'(0) \vert = \vert u_f''(c) \vert$, for some $c \in (0, 1)$. Other than that, I don't know how
Comment: do you know about Sobolev spaces and Lax-Milgram? It tells us that the solution map $f\mapsto u_f$ from $L^2((0,1))\to H^1_0((0,1))$ is continuous, so without any further work, you can immediately bound the first line by the functional by the $L^2$-norm of $u_f$ which is bounded by a multiple of the $L^2$-norm of $f$. Otherwise, the only way I can think of is to use the explicit solution formula (I believe it's called the technique of *variation of parameters*? Here justifying the formula for $f$ continuous is easy, but for $f\in L^2$, you need some finessing).
Answer: Multiply your ODE by $u$ throughout, then integrate from $0$ to $1$:
\begin{align}
\int_0^1u''u +\int_0^1u'u-\int_0^12u^2&=\int_0^1fu.
\end{align}
Integrate by-parts on the first term to get $[u'u]_0^1-\int_0^1|u'|^2=-\int_0^1|u'|^2$, since $u(0)=u(1)=0$. For the second term, it can be integrated to give $\left[\frac{u^2}{2}\right]_0^1=0$. Hence, rearranging gives
\begin{align}
\int_0^1\bigg(|u'|^2+ 2|u|^2\bigg)\,dx&=-\int_0^1fu\,dx\leq \int_0^1|f|^2\,dx+\int_0^1|u|^2\,dx,
\end{align}
and so rearranging gives
\begin{align}
\|u'\|_{L^2}^2+\|u\|_{L^2}^2&\leq\|f\|_{L^2}^2,\tag{$*$}
\end{align}
meaning in particular the solution map $f\mapsto u_f$ from $L^2([0,1])\to L^2([0,1])$ is bounded (with operator norm at most $1$).
With this,
\begin{align}
|\langle\lambda,f\rangle|&\leq\|u_f\|_{L^1([0,1])}\leq\|u_f\|_{L^2([0,1])}\leq \|f\|_{L^2([0,1])}.
\end{align}

The only thing left to justify is why we are able to perform all these manipulations with the integration-by-parts and stuff, but for this, I cannot really do so without the language of Sobolev spaces.
But I guess one very weak way of saying it is that when $f\in C([0,1])$, you can write down an explicit integral formula involving exponentials and stuff, giving us an explicit formula for a $u\in C^2([0,1])$. In this case, our manipulations are completely justified, and our estimate $(*)$ showed that the map $f\mapsto u_f$ from $C([0,1])\to C^2([0,1])$, where the domain and target are equipped with the $L^2$ norm is continuous (actually even better, on the target we can use the norm $\|u\|_{H^1}:=\|u\|_{L^2}+\|u'\|_{L^2}$), and hence has a unique continuous linear extension to a map $L^2\to L^2$ (resp $L^2\to H^1_0$, where $H^1_0$ can be defined as the completion of $C^2$ or $C^{\infty}$ functions which vanish at the endpoints, using the Sobolev norm $\|\cdot\|_{H^1}$).


How to convert to an Object by handling null inputs

Question: How to convert to an Object by handling null inputs, is there a way to check for null inputs and return null without writing the if check for null;
public static Image convert(SyndImage syndImage) {
    return Image.builder()
        .title(syndImage.getTitle())
        .url(syndImage.getUrl())
        .build();
}

Comment: String s = null;
String trim = Optional.ofNullable(s).orElse("").trim();
Comment: How about the use the Optional  to pack your object ,the Optional class is supported after Java8
Comment: syndImage itself is null, how to handle it elegantly and return null
Comment: Like the up code , if don't use the Optional,it may say NullPointException,but like the code ,it can replace the empty object to a default object to avoid the NullPoint
Comment: syndImage is null and its not a string
Comment: The string can replace by your syndImage ... I don't defined a object like syndImage ...
Answer: You can use requireNonNullElse() like this:
return Image.builder()
.title(Objects.requireNonNullElse(syndImage.getTitle(), "Default Title"))
.url(Objects.requireNonNullElse(syndImage.getUrl(), "Default URL"))
.build();

Comment: I don't want to build the object if input is null and just return null
Comment: oh then this comes to mind 'return syndImage == null ? null : return Image.builder()...'
Comment: @user352290 Your title and question are not clear. They could use some editing.
Answer: Use the builder
You commented:

I don't want to build the object if input is null and just return null

Sounds like a job for your Builder. That is one purpose of a builder: Verify inputs before constructing an object.
So if you don't like one of the inputs (null, whatever), have Image.builder().….build() return null.
Or better, have it return an empty Optional< Image > object. The point of Optional is to flag to the calling programmer that a null value is a legitimate result.
public static Optional< Image > convert(SyndImage syndImage) {
    return Image.builder()
        .title(syndImage.getTitle())
        .url(syndImage.getUrl())
        .build();
}

Inside your builder code:
if ( Objects.isNull( url ) ) { return Optional.empty() ; }

The calling code checks to see if they received an Image object in the payload.
if( optionalImage.isPresent() ) { … }

Answer: Assuming you have limitations to update existing builder or refactor to optional return for getImage,you could implement as below
import lombok.Data;
import org.junit.jupiter.api.Assertions;

import java.util.Objects;
import java.util.Optional;

@Data
class SyndImage{
    String title;
    String url;

    public SyndImage(String title, String url) {
        this.title = title;
        this.url = url;
    }

    public SyndImage(String title) {
        this.title = title;
    }

    public SyndImage() {

    }
}
class Image{}
public class OptionalUtility {

    public static void main(String[] args) {

        Assertions.assertNull(getImage(new SyndImage()));//null fields
        //SyndImage allFileds = new SyndImage("MyTitle1", "Url1");
        Assertions.assertNotNull(getImage(new SyndImage("MyTitle1", "Url1")));//all fields

        //SyndImage title = new SyndImage("Title");
        Assertions.assertNull(getImage(new SyndImage("Title")));//only title field is available

        Assertions.assertNull(getImage(null));//null fields

    }

    private static Image getImage(SyndImage syndImage) {
       return Optional.ofNullable(syndImage)
                .filter(sImage-> Objects.nonNull(sImage.getTitle()))//if field checks are also required
                .filter(sImage->Objects.nonNull(sImage.getUrl()))
                .map(sImage->iamgeBuilderMethod(sImage))
                .orElse(null);//this is generally not recommended
    }

    private static Image iamgeBuilderMethod(SyndImage sImage) {
       /* return Image.builder()
                .title(syndImage.getTitle())
                .url(syndImage.getUrl())
                .build();*/
        System.out.println("Image created");
        return new Image();//this will be your existing convert method
    }
}


Token for field in field collection

Question: I have a field collection with 3 fields (title, image and link). I'd like to link the title and image to the value of the link field.
I'm using the linked field module to make the fields linkable and enabled the token module. Yet the fields from my field collection aren't in my available tokens list. They also can't be found in admin/help/token
How do I link a field from a field collection to the value of another field from that same collection? Or how do I find the correct token?
Answer: I found the solution in enabling the entity_token module. This expanded my available tokens list to include the fields within the field collection.

Token for field in field collection


Spring Boot project in Eclipse Oxygen

Question: I am creating a Spring Boot Application in eclipse oxygen.I have followed the following steps:
File->New->Spring Boot->Spring Starter Project
When the wizard opens up I am getting the following error::
SunCertPathBuilderException: unable to find valid certification path to requested target

I am connected to Internet and also able to connect to Eclipse Market Place
Can anyone figure out the issue??
Comment: I always go to the website, do some clicking, then I download the archive, unpack and import. Always works!
Comment: which archive you are referring to?as far as I know spring boot plugin is installed by default with oxygen version of eclipse
Comment: You can simply click Generate project and download the archive.
Answer: I too encountered the exactly same issue earlier.  To make it work, changed Network Connections mode to Manual. Follow this path.

Window -> Preferences -> Network Connections Change mode to Manual
Answer: I don't work with Eclipse anymore ... But, which Java version are you using ?
This error, not especially linked to Eclipse, appears when JCE (Java Cryptography Extension) is not present. Did you try to install it ?

Download JCE
Copy local_policy.jar and US_export_policy.jar to $JAVAHOME/jre/lib/security.
Comment: I am using Java 1.8.0_151.

Spring Boot project in Eclipse Oxygen


ODP to Powerpoint Software?

Question: Is there some sort of free and/or open source converter that could be used on a Linux or Windows server to convert ODP (Open Office presentations) into Powerpoint files?
Answer: You can use oood, from Nexedi. I've used it under Linux to convert odt documents to various formats, but it can be made working also for odp to ppt; it is an xml-rpc server, and client with samples are provided in a number of languages.
A new version of this is cloudooo, same company, but never actually tried it.
Answer: Why not just save the files in PowerPoint format from OpenOffice?
Comment: There are some scripting facilities available through the Python integration which may be utilized to automate your needs.
Comment: The files come in ODP, and I need them converted. These files aren't necessarily coming from Open Office directly and they need to be stored in ODP on my server before being downloaded in PPT.
Answer: You can run OpenOffice in a headless mode for doing batch conversions.
Comment: The idea makes sense, but it needs a sample command to make it useful.

ODP to Powerpoint Software?


VB.net Overridable property not same as c# Virtual property?

Question: Well its simple here you have my vb.net code:
Public Class Class1
    Public Overridable ReadOnly Property Name() As String
        Get
            Return Nothing
        End Get
    End Property
End Class

Public Class Class2
    Inherits Class1
    Public Overloads ReadOnly Property Name() As String
        Get
            Return "Class2"
        End Get
    End Property
End Class

Module Module1
    Sub Main()
        Dim c2 As New Class2()
        Console.WriteLine(c2.Name)
        Dim c1 As Class1 = CType(c2, Class1)
        Console.WriteLine(c1.Name)
    End Sub
End Module

And here comes the c# code:
class Class1
{
    public virtual string Name
    {
        get
        {
            return null;
        }
    }
}

class Class2 : Class1
{
    public override string Name
    {
        get
        {
            return "Class2";
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Class2 c2 = new Class2();
        Console.WriteLine(c2.Name);
        Class1 c1 = (Class1)c2;
        Console.WriteLine(c1.Name);
    }
}

i did expekt them to do the same thing but guess what thay dont!
C# Output
Class2
Class2
VB.NET output
Class2
{Nothing}
(It dosent print nothing it just prints an empty line)
Why does vb.net go for the base class implementation when its overriden?
Answer: Should it be overrides and not overloads?
Public Overrides ReadOnly Property Name() As String

Comment: Because overload is valid! I would google overload versus override for vb.net, there are quite a few good explanations.
Comment: Yes, its overrides, overloads is to add more parameters.
Comment: This is clearly the correct answer, but why doesn't the compiler give an error?


Pegar o id do valor máximo

Question: Em uma tabela Mysql
id valor
1    5
2    15
3    7

Quero pegar o valor do "id" que tem maior valor no campo valor. Quero que me retorne neste caso o valor 2.
Answer: Ordene o resultado pelo valor DESC e limite a 1:
SELECT
    id
FROM tabela
ORDER BY valor DESC
LIMIT 1;

Comment: Correto. estava tentando select id from tabela where MAX(valor);

Pegar o id do valor máximo


Custom module block holding DB calls

Question: I am working on a project where I first was only designing the theme. However, I now need to create 5 already made custom pages or blocks in a page, which will be available as soon as theme is enabled.
Each page should hold its own code for grabbing information from external database. Now I am having a huge dilemma on deciding how to create those pages. Since there are DB calls made on each of them, I was suggested to create a custom module, which would display a block with the printed content from database. I tried creating "pvpstats.module" and "pvpstats.info" as normal for custom modules, and displaying it in sidebar_first. After I enable the module, nothing happens. The block is just not visible. I used the code below.
function pvpstats_block_info() {
  $blocks['pvpblock'] = array(
    'info' => t('Example Block'),
    'status' => TRUE,
    'region' => 'sidebar_first',
    'weight' => 0,
    'visibility' => 0,
    'pages' => 'admin*
coder*
node/add*
node/*/edit
node/*/devel
',
  );

  return $blocks;
}

/**
  * Implements hook_block_view().
  */
function pvpstats_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'pvpblock':
      $block['subject'] = t('Block Name');
      $block['content'] = theblock_contents();
      break;
  }

  return $block;
}

function theblock_contents() {
  return '
    $dbserver="serveradress";
    $dblogin="username";
    $dbpass="dbpassword";
    $dbname="databaseserver";

                mysql_connect($dbserver, $dblogin, $dbpass);
                mysql_select_db($dbname);
                mysql_query("SET NAMES 'utf8'");

                $vypis =  mysql_query("SELECT * FROM characters WHERE (accesslevel < '1') AND (pvpkills > '0') order by pvpkills desc LIMIT 20");
                echo '

Nick		Kills
';
         while($row = mysql_fetch_array($vypis))
                    {
                echo '
';
                echo $row["char_name"];
                echo '	  	';
                echo $row["pvpkills"];
                echo '
';

                    }
    echo '
';
    ?>

The main point is that I need those blocks later included in my theme so that they are visible immediately after enabling the theme, if that is somehow possible.
Comment: As side note, you need to rewrite the code of `theblock_contents()`, since that is not Drupal code. You don't call any `mysql_` function in a module.
Answer: In theblock_contents you are trying to return php code. Don't. Do not echo or print in it. If anything, build a string. It should look about like that:
/**
* custom html block
* @return string
*/
function theblock_contents() {
  $dbserver="serveradress";
  $dblogin="username";
  $dbpass="dbpassword";
  $dbname="databaseserver";

  $result = '';

  mysql_connect($dbserver, $dblogin, $dbpass);
                mysql_select_db($dbname);
                mysql_query("SET NAMES 'utf8'");

  $vypis =  mysql_query("SELECT * FROM characters WHERE (accesslevel < '1') AND (pvpkills > '0') order by pvpkills desc LIMIT 20");
  $result .= '

Nick		Kills
';
  while($row = mysql_fetch_array($vypis))
  {
    $result .= '
';
    $result .= $row["char_name"];
    $result .= '	  	';
    $result .= $row["pvpkills"];
    $result .= '
';
  }
  $result .= '
';
  return $result;
}

It's worth to mention that your code was plainly invalid PHP, no matter if it was supposed to run in Drupal or not. You can't simply return PHP code as string and hope it'll be executed. Only the part about not using echo in hooks is really Drupal dependent.
Important note: if you want your modules to work stable, please don't use PHP SQL functions directly. Use db_select to obtain data. Connecting to multiple databases is pretty well documented too.
Comment: Of course after this you may encounter other bugs, but that will be another question.
Comment: Actually your answer resolved my issue without adding any to it. The PvP stats are now correctly displayed in the assigned region. Thank you very much!
Answer: You can associate a block automaticly with a theme's region. (I've done it in a module.install file.) By default a block in not visible; you have to choose manually a theme's region, or you can use code similar to the following one to add it programmatically.
  $default_theme = variable_get('theme_default', 'bartik');
  $values = array(
    array(
      'module' => 'block',
      'delta' => 'pvpblock',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => 1,
      'region' => 'sidebar_first',
      'pages' => '',
      'cache' => -1,
    ),
  );

  $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
  foreach ($values as $record) {
    $query->values($record);
  }
  $query->execute();

Comment: May i ask where should i include your code to make block visible? Is it in themes template.php or in pvpblock.module.php? Thank you
Comment: have you tried to set the block visible by the UI ? I mean on this page : "admin/structure/block/list/yourthemename"
Comment: The custom block is not visible for me to assing a region to it, under structure/blocks, after enabling the custom module. So i guess something went wrong in module code?
Comment: clear the cache :)
Comment: That was the first thing i tried but it didn't help.
Comment: carefull : TopPvP.module.php should be TopPvP.module and TopPvpP.info.txt should be TopPvpP.info
Comment: Yep the extension files are correct.
Comment: i've tried your code, hooks are correct. Pb is probably in theblock_contents() function. Try with a very simple theblock_contents() return 'test';
Comment: function theblock_contents() {
  return 'test';}     makes no difference :\
Comment: @rishariss If you are editing a module that is already enabled, you need to first disable it, and then re-enable it. Differently, Drupal will not know you changed your code.
Comment: @pico34 I resolved the issue, thank you for ur effort!

Custom module block holding DB calls


Adding response headers to media response

Question: I have a custom module which implements hook_ENTITY_TYPE_access to control access to particular PDFs (media objects).  When the user doesn't have access they are redirected to a login/register page.  This works successfully.
I now need to send specific HTTP response headers (cache-control, no-store) along with the response when the user does have access to the document.
From what I have been able to find, to get access to the response object and add headers I need to subscribe to the response event.  This seems excessive, I'm subscribing to the response event for every request, when I only need the to set headers for a very small number of requests.
Do I need to subscribe to the response event, is there a way to get hold of the response object when processing hook_ENTITY_TYPE_access?
Or, is there another way to set 'Cache-control: no-store' without needing to access the response object?
Is it a fair assumption, that when hook_ENTITY_TYPE_access has been called and allowed access, that any later response must be for the particular media object.  I.e., I need some way for the hook_ENTITY_TYPE_access handler and the response event subscriber to communicate, e.g. in the hook handler, retrieve my event subscriber service object and set some flag on it - to indicate it should add headers to the response event.
Or, Should I be looking at this another way?
Answer: Yes, there is a way to communicate via cache metadata. If you add cache metadata to the access results the router adds it to a CacheableAccessDeniedHttpException. When issuing the redirect response to the login/register page you can check if the exception class is an instance of CacheableDependencyInterface and get the cache metadata to set the response headers.
Comment: Thanks for answering.

Maybe I'm missing something, that enables me to set cache tags, cache contexts and/or max age.  None of which will get me a cache-control header with no-store.

For the redirect (on denied) I don't need need to set any headers.  no-store needs to be set when access is allowed.
Comment: Then the cache metadata ends up in the cacheable response. It should be possible to decide whether to add the no-store header based on the current route and the cache metadata. It would be best if you add the correct cache metadata anyway to define the cacheability of inbound routes and outbound links within Drupal, independently of the headers you are sending.


Xcode 4 - Go back to editor without mouse

Question: How can I return from Project to Editor without mouse?
Answer: Command+J will make Editor Active.


First AJAX request is working very well, next takes too much time

Question: I'm working on ajaxing my site, for a long time now.
I found a method that's working for me, but only the first AJAX request is working the right way; the next request takes at least twice the time.
I thought, that it behaves like that because I load the whole scripts from a template file with every request. I removed the print $scripts from that template file; the result is that the requests take the same time took before.
This is the content of the files I am using.
my_ajax.module
function my_ajax_init() {
  drupal_add_js(drupal_get_path('module', 'my_ajax') . '/my_ajax.js');
}

function acquia_slate_preprocess_page(&$vars, $hook) {
  if (isset($_GET['ajax']) && $_GET['ajax'] == 1) {
    $vars['template_files'][] = 'page-ajax';
  }
}

my_ajax.js
Drupal.behaviors.my_ajax = function (context) {
  $('#content-group-inner a').live('click', function (e) {
    $('#content-group-inner a').not('.my_ajax-processed').addClass('my_ajax-processed');
    var url = $(this).attr('href');
    $('#content-region-inner').empty().html('');
    $('#content-region-inner').load(url, 'ajax=1', function () {
      Drupal.attachBehaviors('#content-region-inner');
    });

    return false;
  });
};

page-ajax.tpl.php
<?php print $styles; ?>
<?php print $setting_styles; ?>

<?php if ($local_styles): ?>
  <?php print $local_styles; ?>
<?php endif; ?>
<title><?php print $head_title; ?></title>

<?php if (arg(0) != 'admin') { ?>

<?php } ?>
<?php  print $scripts; ?>

<?php print theme('grid_block', $tabs, 'content-tabs'); ?>

    <?php if ($title): ?>

<?php print $title; ?>

    <?php endif; ?>
    <?php if ($content): ?>

        <?php ?>
        <?php print $content; ?>
        <?php print $feed_icons; ?>

    <?php endif; ?>

<?php print $closure; ?>

Answer: I found a solution that's working for me.
I just changed that:
$('#content-region-inner').load(url, 'ajax=1', function () {
      Drupal.attachBehaviors('#content-region-inner');

into that:
var result = $('#content-region-inner:not(.my_ajax-processed)', context)
    .load(url,'ajax=1',function() {
               Drupal.attachBehaviors(result);

When using that, Drupal only fires up Drupal.attachBehaviors() once a time, no matter how much requests you do.
I hope this is working for you, too
Comment: That's good. I would also like to add for other users aid that happen to land on this question: I found that using Devel's dpm() in Ajax calls sometimes leads to large delays.


Homology of Mapping Cone is Trivial--Proof from scratch

Question: Let $(A_*,\partial )$ and $(B_*,\partial' )$ be chain complexes, $f:A_*\to B_*$ a chain map and suppose that the map $f_*$ it induces on homology is an isomorphism.
The mapping cone $(C_*,D)$ is defined by $C_n=A_{n-1}\oplus B_n$ and $D_n(a_{n-1},b_n)=(-\partial_{n-1}a_{n-1},f_{n-1}(a_{n-1})+\partial'_n b_n).$
Now, define $A_n^+=A_{n-1}.$ Then, using the maps $i:b\mapsto (0,b)$ and $p:(a,b)\mapsto a$, there arises in the usual way, the long exact sequence
$$ \cdots \rightarrow H_{n+1}(C)\overset{p_*}\rightarrow H_{n+1}(A^+)\overset{f_*}\rightarrow H_n(B)\overset{i_*}\rightarrow H_n(C)\overset{p_*}\rightarrow H_n(A^+)\rightarrow \cdots $$
Then, $H_n(C)$ is trivial for all $n$. The proof is easy (but hadn't occurred to me, ugh.) I wanted to do it using the basic facts about the chain complexes involved, to get an idea of how they work.
There is a proof from scratch of essentially the same thing here but I think I found a problem with it, and made a comment there. I would appreciate feedback on my comment there as well as on my attempt here at a from scratch proof, which follows.
What I have so far (dropping the subscripts and primes on the differentiation operators):
Let $(a,b)\in Z_{n}(C).$ Then, by exactness, we have have the following:
$1).\ p_*([(a,b)]=[a]\Rightarrow a\in Bd_{n}A^+$ and $2).\ i_*([b])=[(0,b)]\Rightarrow (0,b)\in Bd_{n}C.$
So, there is an $a'\in A_n$ and a pair $(a'',b')\in C_{n+1}$ such that
$3).\ \partial a'=a$ and $4).\ (-\partial a'',f(a'')+\partial b')=(0,b).$
Combining these facts we get $(a,b)=(a,0)+(0,b)=(-\partial (a'+a''),f(a'')+\partial b').$
If I could show that either $f(a')=0$ or $\partial a'=0$, I would then have $(a,b)\in Bd_nC,$  which is what I want.
Comment: It's your question why $C$ has trivial homology, or why the given sequence is well-defined and exact?
Answer: That is probably not the answer you are looking for, but here is a proof using model category theory. Somehow, it says that this is a result not about chain complexes per se, and it is a motivation not to dive into chain complexes details.
In any model category $\mathcal C$, the mapping cone $C_f$ of $f:X\to Y$ is defined as the homotopy pushout of $\ast \overset !\gets X \overset f\to Y$. Computing homotopy pushout in a model category goes as follow: take a cofibrant replacement $X'\overset q\to X$ of $X$. Factor $fq$ as $pi$ with $p$ acyclic fibration and $i$ cofibration. Factor $!q$ as $p'i'$ with $p'$ acyclic fibration and $i'$ cofibration. Then take the ordinary pushout of $\bullet \overset {i'} \gets X' \overset i \to \bullet$. This is summed up in the following diagram:

In particular, $q,p,p'$ are weak equivalences, so if $f$ is also a weak equivalence, we get that $i$ is an acyclic fibration. It follows that its pushout $j$ also is such. In the end, we have a commutative triangle:

where $p'$ and $j$ both are weak equivalences, which yields that $C_f \to \ast$ is also a weak equivalence. Otherwise put, $C_f$ is contractible.

If you play that proof in your favorite model structure on chain complexes for which weak equivalences are the quasi-isomorphisms, you get the result you wanted.
Comment: I do not know much about model categories, except for the basic acyclic models theorem. I do know some category theory, though, (I have read CWM and Awodey's book) so I wonder is there a way to make your proof more understandable on that basic level? If so, I will gladly accept your answer.
Answer: Assuming your question is why $C$ has trivial homology: $f_*$ is an isomorphism, so $p_*$ and $i_*$ must both be zero maps.
Comment: If, in an exact sequence, one of the terms $X$ has both its incoming and its outgoing maps equal to zero, then $X$ has to be 0. Proof: The image of the incoming map is 0 because the incoming map is 0. But that image is also the kernel of the outgoing map, and that kernel is all of $X$ because the outgoing map is 0.
Comment: Yes, I know, but then how is it immediate from this that $C$ has the trivial homology? I am using the fact that $i_*$ and $p_*$ are zero maps to prove that $Z_n(C)=Bd_n(C).$  If it's trivial, could you point out why?
Comment: I see. Thanks. I still would like to do a proof from scratch, to see how the "innards" of these chain complexes work. I am just starting to learn about homology theory, and am reading Rotman. If you critique my proof and/or the comment I made on a similar question (the link is cited above), I will be glad to accept your answer.