Sample for for an XSP Object data source that is writable

Question: I'm struggling with the Object datasource in an XPage:
  <xp:this.data>
    <xe:objectData var="demo" ignoreRequestParams="true"
        readonly="false" scope="view"
        createObject="#{javascript:return new demo.SampleBean();}">
    </xe:objectData>
  </xp:this.data>

When I execute save(); on the XPage in SSJS I get the error:
Error saving data source demo
The save method has not been implemented in the data source

The class is rather simple:
package demo;
import java.io.Serializable;

public class SampleBean implements Serializable {

private static final long serialVersionUID  = 1L;
private String name;
private String job;

public String getName() {
    return this.name;
}

public String getJob() {
    return this.job;
}

public void setName(final String name) {
    this.name = name;
}

public void setJob(final String job) {
    this.job = job;
}

public void dummyAction() {
        System.out.println("Here shall be logic");
}

}

I tried to add a public void save(); method to the class, but that doesn't do the trick.
So I'm looking for a sample of an Object datasource
Answer: You've defined the createObject attribute for the data source, but you haven't specified the saveObject attribute... that's what the error message you're getting ("save method has not been implemented") is referring to.
So, for example, if you want your dummyAction() method to run when a save is triggered, try this:
<xp:this.data>
  <xe:objectData var="demo" ignoreRequestParams="true"
      readonly="false" scope="view"
      createObject="#{javascript:return new demo.SampleBean();}"
      saveObject="#{javascript:return value.dummyAction();}">
  </xe:objectData>
</xp:this.data>

When the saveObject attribute is specified as a SSJS method binding, the variable value is  bound to the data object, and then the method binding is invoked. So you can either pass value to some other object to handle the business logic of saving the object, or you can use a syntax of value.anyMethod() to keep the business logic of object serialization internal to the object itself.
NOTE: whatever logic you do use in this method, you'll want to return a boolean (not void), so that a return value of false can be treated as a cancellation, just like the standard Domino document data source does.


Objective C - how to know if point is inside a quarter of circle?

Question: I drew a quarter of a circle inside a rectangle.
Rectangle:
UIView *Rectangle  = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height-292)];
    Rectangle.backgroundColor = [UIColor lightGrayColor];
    Rectangle.layer.zPosition = -5;

Quarter of Cirlce:
CGPoint center;
center.x = 0;
center.y = 0;
float radius = [[UIScreen mainScreen] bounds].size.width;

UIBezierPath *circle = [UIBezierPath bezierPathWithArcCenter:center
                                                      radius:radius
                                                  startAngle:0
                                                    endAngle:M_PI
                                                   clockwise:YES];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setPath:[circle CGPath]];

Then I added the rectangle in the view and added the circle inside the rectangle:
[self.view addSubview:Rectangle];
[Rectangle.layer addSublayer:circleLayer];

Then I started drawing little rectangles of 1 width and 1 height that I consider as points, and randomly added them into the view with a for loop, coloring the points inside the circle with green and points outside of the circle with red
int compteurPointsinCercle  = 0 ;
    int compteurPointsOutCercle  = 0 ;
    float       XcenterCircle   =   center.x;
    float       YcenterCircle   =   center.y;

    for (int i = 0 ; i < 50000 ; i++ )
    {

        float xvalue = arc4random_uniform([[UIScreen mainScreen] bounds].size.width);
        float yvalue = arc4random_uniform([[UIScreen mainScreen] bounds].size.height-292);

              // (x - center_x)^2 + (y - center_y)^2 < radius^2
        float valeurPoint = (xvalue - XcenterCircle)*2 + (yvalue -YcenterCircle)*2;

        NSLog(@"(Inside for), valeurPoint is : %f",valeurPoint);

        if ( valeurPoint <  (radius*2) )
        {
            // Point is inside of circle (green color)
            compteurPointsinCercle++;
            UIView *Rectangle2  = [[UIView alloc] initWithFrame:CGRectMake(xvalue,yvalue,1,1)];
            Rectangle2.backgroundColor = [UIColor greenColor];
            [self.view addSubview:Rectangle2];
        }
        else if ( valeurPoint > (radius*2) )
        {
            // Point is outside of circle (red color)
            compteurPointsOutCercle++;
            UIView *Rectangle2  = [[UIView alloc] initWithFrame:CGRectMake(xvalue,yvalue,1,1)];
            Rectangle2.backgroundColor = [UIColor redColor];
            [self.view addSubview:Rectangle2];
        }

    }

I test if the point is inside the circle using this :
 float valeurPoint = (xvalue - XcenterCircle)*2 + (yvalue -YcenterCircle)*2;

where xvalue and yvalue are the coordinates of the point that will be created, and XcenterCircle and YcenterCircle are the coordiantes of the center of the circle.
I have something wrong because it gives me this result (it dosent test correctly if the point is inside the circle or not: a part of the points inside of the circle are considered outside):

can you tell me what I am doing wrong here ? and how can I exactly the points inside the circle ?
Comment: Maybe something like this: http://stackoverflow.com/questions/10831370/how-to-check-if-touch-point-is-on-uibezierpath-ios
Comment: @Alex That's a good solution.
Answer: * is not power operation, it is multiplication.
float valeurPoint = (xvalue - XcenterCircle) * (xvalue - XcenterCircle) + (yvalue -YcenterCircle)*(yvalue -YcenterCircle);

if ( valeurPoint <  (radius * radius) )

Should fix your problem
or using the pow function:
float valeurPoint = pow((xvalue - XcenterCircle), 2) + pow((yvalue -YcenterCircle), 2);

You can also use hypot function directly (although the performance can be slightly worse because of sqrt calculation)
float distance = hypotf((xvalue - XcenterCircle), (yvalue -YcenterCircle));

if (distance < radius)

EDIT:
Thanks @Alex for suggestion. The best solution is to use the native method -[UIBerierPath containsPoint:]. Then you won't have to calculate the distance at all.
Comment: I am going to test this solution and  get back to you
Comment: that did it for me, I was having issues with power operation


Why do we study Cameron-Martin Space and what is the motivation behind it?

Question: I'm reading about Gaussian Measures and the chapters always define the Cameron-Martin space shortly after.
Typically they'll define a covariance operator first.  Let $U$ be a separable Banach space, and $\mu$ be a centered Gaussian measure on $U$. $U^*$ is the dual.  The Covariance operator $C:U^*\times U^*\to\mathbb{R}$ is first given by
$$C_{\mu}(f)(g):= \int_Uf(x)g(x)\mu(dx)$$
First define $|x|_{H(\mu)} = \sup_{l\in U^*}\{ l(x) : C_{\mu}(l)(l)\leq 1\}.$  The Cameron-Martin space is then defined as $$H(\mu) := \{x \in U : |x|_{H(\mu)} < \infty \}.$$
(At least that's one of a couple definitions)
Intuitively, I've also heard that the Cameron-Martin space is the set of all elements that make the null sets of $\mu$ translation-invariant (ie you can shift a null-set by that element and it will still be a null set). But I still feel like I'm missing a broader perspective.
Comment: Umm... so, @markvs, why did you "contribute" your comment?
Comment: Exactly one year ago I was reading something related to that. I eventually found the HDR thesis of J. Lehec, which mentions the Cameron--Martin space at page 2, and it was useful for me, maybe it can help you as well:

https://hal.archives-ouvertes.fr/tel-01428644
Comment: I saw this long ago, and I tried to give it a go then before doing not so well. I'll try better this time.
Answer: The infinite-finite dimension dichotomy , and Fourier coefficients
When we study (multivariate) normal random variables on $\mathbb R^d$, we often have equivalent ways of understanding them. One famous result is that any normal random variable can be specified by providing its mean and covariance matrix. While extending these concepts to infinite dimensions on separable Banach spaces, something analogous occurs : every Gaussian measure $\mu$ on $\mathcal{B}$ is determined by its "mean vector" (an element of $\mathcal{B}$) and a "covariance matrix" (a linear functional on $\mathcal{B}^* \times \mathcal{B}^*$). It is the "covariance matrix" that makes all the difference.
The proof goes via the "Fourier transform" and covers the finite dimensional case as well : let's assume that the mean vector is $0$ (one can easily carry over the argument to anywhere else) and suppose that the covariance operator is given by $C_{\mu}$.
One defines the Fourier transform of $\mu$, called $\hat{\mu} : \mathcal B^* \to \mathbb R$ by $$
\hat{\mu}(l) = \int_{\mathcal{B}}e^{il(x)}d\mu(x)
$$
Now, because $\mu$ is a Gaussian measure, it turns out that one can write $$
\hat{\mu}(l) = \exp\left(-\frac{1}{2} C_{\mu}(l,l)\right)
$$
[This in fact characterizes Gaussian measures on separable Banach spaces] Therefore, one sees that the map $\phi(l) = C_{\mu}(l,l)$ determines $\mu$, since it determines the Fourier transform of $\mu$ via the above formula, and Fourier transforms uniquely determine measures on separable Banach spaces.
The big difference, however, comes as follows. In finite dimensions, if $C_{\mu}$ is the covariance matrix of $\mu$, then the map $\phi$ is determined by the vectors $e_i^TC_{\mu}$ where $e_i$ are the standard basis vectors. But that's true of every other matrix as well : we didn't really use the fact that $C_{\mu}$ is special in any way ,we just used the fact that every finite matrix (and not just positive semidefinite ones) is determined by its rows. So knowing the map $\phi$ still reduces to knowing every single entry of $C_{\mu}$. To put it another way, you're using every possible bit of information you know about $C_{\mu}$, to determine $\mu$.
However, in infinite dimensions, $\phi(l)$ is determined by knowing the maps $l' \to \phi(l, l')$ from $B^*$ to $\mathbb R$ because these will give us the "rows" of $\phi$, hence a description of $C_{\mu}$. These maps belong to $B^{**}$ (linear functionals of $B^*$). It turns out, however, that because the speciality of $C_{\mu}$ counts for something in infinite dimensions, these maps in fact belong to $\mathcal{B}$ (i.e. they belong in the image of the map $\xi : \mathcal B \to \mathcal B^{**}$ given by $[\xi(x)](l) = l(x)$), and not just that : even as elements of $B$, they are very, very special.
That is, thanks to the special properties of $C_{\mu}$, not every element of $B$ comes from a map of the form $l' \to \phi(l, l')$. Only some of them do. A very , very small number of them. And YET, because of what we've said before, these very small number of elements determine $C_{\mu}$ and hence determine $\mu$.
Those elements, they form (i.e. are dense in a specific norm in) the Cameron-Martin space corresponding to $\mu$. A very small space, but a very powerful one, because the elements required to determine $\mu$ come from this space.

Definition and rigorous result

Definition : Given a Gaussian measure $\mu$ with covariance operator $C_{\mu} : \mathcal{B}^* \times \mathcal{B}^* \to \mathbb R$, we already saw that $\hat{C}_{\mu} \in B^{**}$ defined by $[\hat{C}_{\mu}(l)](l') = C_{\mu}(l,l')$ can be interpreted as an element of $B$. The closure of all such elements under the norm $\|h\|^2_{\mu} = C_{\mu}(h^*,h^*)$ (where $h^*\in B$ is any element such that $C_{\mu}(h^*,l) = l(h)$ for all $l \in B^*$) is called the Cameron-Martin space corresponding to $\mu$ and is denoted $\mathcal{H}_{\mu}$.

That is, given $\mu$, we collect all possible values in $B$ that the map $l' \to C_{\mu}(l,l')$ can correspond to, and close it under a norm corresponding to $\mu$. The closure is essentially done to ensure that the Cameron-Martin space obtains a Hilbert-space structure. Note that $\mathcal H_{\mu} \subset \mathcal{B}$ because of the relation $\|h\|_{\mu} \geq \|C_{\mu}\|^{-1} \|h\|^2$ for $h \in \mathcal B$, so the completion continues to have elements of finite $\mathcal B$ norm.
By making everything before a little more rigorous, we obtain the following result.

If $\mathcal B$ is a Banach space and $\mu,\nu$ are two Gaussian measures such that $\mathcal H_{\mu} = \mathcal H_{\nu}$ and $\|h\|_{\mu} = \|h\|_{\nu}$ for every $h \in \mathcal H_{\mu}$, then $\mu=\nu$ as measures on $\mathcal {B}$. In other words, a Gaussian measure is determined by its Cameron-Martin space and the norm on that space.

This is perhaps the most "broad" motivation for the Cameron-Martin space. How it ties in with the rest of the theory is also fascinating.

An equivalent description of $\mathcal {H}_{\mu}$
Recalling the Bochner integral, let $L^2(\mathcal B, \mu) = \{l : \int_{\mathcal B} (l(x))^2\mu(dx) < \infty\}$, where $l$ isn't an element of $B^*$, but instead is a $\mu$-a.e. equivalence class of functionals (i.e. $l_1 = l_2$ $\mu$-a.e. precisely when $l_1(x) \neq l_2(x)$ has $\mu$ measure zero).
While defining the CM space, we showed that every $h\in \mathcal H_{\mu}$ corresponded to some $h^* \in B^*$ in the way that $C_{\mu}(h^*,l) = l(h)$ for every $l\in B^*$. It's true that the map $h \to h^*$ is not unique. However, if we identify functionals up to $\mu$ a.e. equivalence, we have the following result.

$\mathcal H_{\mu}$ is isomorphic to the closure of $\mathcal{B}^*$ in $L^2(\mathcal{B},\mu)$. The latter space is called the reproducing kernel Hilbert space, or RKHS.

That is, the Cameron-Martin space can also be identified with the set of all ($\mu$ a.e. equivalence classes of) linear functionals $l$ such that knowing $C_{\mu}(l,l)$ is enough to determine $C_{\mu}$ completely.
The upshot of both the sections I've just written, is that one can associate to every Gaussian measure on a separable Banach space, another Hilbert space whose elements and their norms "capture" all possible entries of the covariance matrix $C_{\mu}$, thereby determining $C_{\mu}$. That space can either interpreted as a vector space in $\mathcal B$ under a different norm, or as a subspace of $L^2(\mathcal{B},\mu)$.
Another remarkable result which shows how important the Cameron Martin space is, lies in the pushforward of Gaussian measures. As Gaussian measures are determined by the Cameron-Martin space, one can specify how to push forward a Gaussian measure onto another Banach space, simply by specifying how the Cameron-Martin space is pushed forward under some linear map. The formal result is

Let $\mu$ be a Gaussian measure on $\mathcal B_1$ with CM space $\mathcal H_{\mu}$. Let $A : H_{\mu} \to \mathcal B_2$ be a bounded linear operator such that for some Gaussian measure $\nu$ on $\mathcal B_2$, it is true that $C_{\nu}(h,k) = \langle A^*h,A^*k\rangle_{\mu}$. Then, there exists a measurable map $\hat{A} : \mathcal B_1 \to \mathcal B_2$ such that $\nu$ is the pushforward of $\mu$ under $A^*$, $\hat{A} = A$ on $\mathcal H_{\mu}$, and $A$ is linear on a subspace of $\mu$-measure $1$. Furthermore, $\hat{A}$ is unique $\mu$ a.e.

That is, a linear map from the Cameron-Martin space that produces a covariance operator on the other Banach space, leads to a (unique) map which pushes forward the Gaussian measure. This is perhaps the broadest interpretation of the CM space : a space that is used not only to determine a Gaussian measure, but is used to push it forward onto other spaces.
Remember, the Hahn-Banach theorem tells you that there can be many, many possible extensions of a linear map $A$ on $\mathcal H_{\mu}$ to the domain $\mathcal B_1$. The Cameron-Martin space is so specially linked with $\mathcal B_1$, that defining how it transforms under a particular linear map is tantamount to saying how the entire Gaussian measure is pushed forward according to a particular extension of that map.
I hope I have provided a more "high-level" demonstration of how the CM space is linked very, very deeply with the underlying Gaussian measure. It is possible to deduce some other interesting properties of the CM space (including the Cameron-Martin theorem), but these are a little more specific compared to the above "by definition" exposition of the CM space.
Comment: This is a brilliant answer! I may also add (not really add, since it is actually there, though not very visible) that while a Gaussian measure may be considered on different Banach spaces and have full support on any of these spaces (e.g. the classical Wiener measure can be considered on $C_0[0,1]$ or on $C_0^{\gamma}[0,1]$ for any $\gamma<1/2$ and has full support in all of them), the Cameron–Martin space is an "invariant" of the measure.
Comment: @zhoraster Indeed, thanks for the informative comment. When we are studying linear transformations of Gaussian measures, the last result implies that we only need to focus upon how that linear transformation acts upon the Cameron-Martin space. The Banach spaces $\mathcal B_1,\mathcal B_2$ are irrelevant as long as they contain the Cameron-Martin space, and its image under the linear transformation respectively.
Comment: To give an example of what I meant by $C_{\mu}$ being "special", if $\mathcal B = H$ is a Hilbert space, then one can prove that $\hat{C_{\mu}}$ is a trace-class operator. Trace class operators are compact, and hence can't be surjective in infinite dimensions. Yet, it is very much possible in finite dimension that compact operators can be surjective, because any operator is compact! In fact, trace class operators are exactly what covariance operators can be for measures on Hilbert spaces. For Banach spaces, it is known that $\hat{C_{\mu}}$ must be compact, at least.
Comment: Commas go directly after the word; do not put a space between a word and the following comma.  Same for other punctuation.
Answer: As far as I know the reasoning goes as follows: a multivariate normal vector with non singular covariance matrix is absolutely continuous wrt to Lebesgue. If we translate this law we get a new law absolutely continuous to the original law.
This concept is then extented to the Brownian Motion by Girsanov Theorem. Through this we can prove the Cameron-Martin formula which tells us by translating  the Wiener measure $\mathcal{W}$ with a function $f \in D[0,T]$, we get a measure $\tilde{\mathcal{W}}$ absolutely continuous wrt $\mathcal{W}$. Here we have defined the following space: $D[0,T] := \{ f:[0,T] \to \mathbb{R}: \exists g \in L^2[0,T]: f(t) = \int_{0}^{t}g(s) ds, \quad \forall t \in [0,T] \}$.
Hence CM gives the infinite-dimensional counterpart of the Gaussian Measures.
Hope this helps.
Answer: Something I have not seen mentioned yet. The Cameron-Martin space is the unique Hilbert space which embeds continuously into the larger Banach space $U$ such that the following property holds true:
for any orthonormal basis $\{e_k\}_{k\ge 1}$ of $H$, and any iid sequence $\{\xi_k\}_{k\ge 1}$ of standard normals, the series $\sum_k \xi_k e_k$ converges almost surely (in the norm of $U$) to a random variable with law $\mu$.


Graphical Layout Editor not DROPPING

Question: The layout editor itself is working fine, but it doesn't want DROP where I want it to drop.
It's only dropped in the very top left corner, and gravity doesn't change anything. If I drag it elsewhere, it just doesn't work.
It's the same anywhere, whether it's a different xml or just a new xml.
Is this an eclipse problem or a Java problem?
Thanks, and Happy New Year!
Comment: Nothing related to java, it's an Eclipse problem.
Answer: Do you come from an XCode or Visual Studio background? If so, yes, the "graphical layout editor" for Android is weird, as it depends chiefly on relative positioning of elements, unless you user AbsoluteLayout controls, which give you pixel-by-pixel positioning power.
This looks to be a good overview of the different kinds of layouts available in Android... the thing you have to remember about Eclipse and its ilk is that they assume you want to use certain layouts (Linear appears to be the default), to make it easier for you to create layouts that scale appropriately to size, irregardless of pixel density or resolution.
http://www.sitepoint.com/know-your-layouts-in-android/
Good luck!

Graphical Layout Editor not DROPPING


Application cannot call .chm file unless help topic ID numbers are incremented by 1

Question: I am a help developer. My team submitted a .chm for a new C# application. The software developer could not get context-sensitive help to appear. Finally he discovered that if he put in the help IDs we gave him that we had defined in our help authoring tool, Author-it, the help would not work, but if he added 1 to the help ID for a topic and put that in the code, then the help would appear. For example, if we told him the help ID was 37182,and he put that in the code, help would not appear at all when invoked from the app, but if he put 37183 in the code, the correct help (that is, the help from our topic 37182) would appear.
The software developers insist that according to their testing the problem is on the help developers' end, but it is a bit hard to understand how this could be an Author-it problem. If anything on our end, I think it would have to be an MS HTML Help Compiler problem (Author-it is a database for single-sourcing; it calls hhc.exe to generate the actual help). But we've never had problems with the help/app connection using these tools before, so I still think the problem might be on the other end. Either way, even though there is a workaround (add 1 to all the help IDs!) we have to be able to fix this.
Does anyone have an idea why this might be happening? The application is a WPF application, if that matters.
Thank you very much - Sandy
Comment: Note that the problem seems to be answered already by Michele, but in such cases an easy test can show you whose side the problem is on. (generation or consumption) This is by done by decompiling the CHM with one of the many free tools.
Answer: I'm posting here the solution provided by our Support team.
The reason for the behaviour is that you've changed the Topic ID Offset from 0 to 1 on the Help > Linking Tab of the Book object.
Setting it back to 0 will prevent the context ID's from being increased by 1.
Cheers
Answer: Disclaimer: THE FOLLOWING IS MORE OF A COMMENT THAN AN ANSWER - only posting as an answer because it is too long for a comment!
I would strongly recommend to build a minimum sample WPF application - some "Hello World"-type of application plus an Author-It based help file (.chm) which shows this problem.
Send that including the respective Author-It-Database and all version-information (like help compiler version etc.) to the support staff of Author-It.
If they can reproduce it then I would expect that they will come up with some sort of workaround/fix...
Answer: I'm a developer at Author-it. I believe this is probably caused by the "Topic ID Offset" property. To change this property:

Right click on your Book
Select Properties from the context menu
Help
Linking
Topic ID Offset is probably 1, change it to 0

Save the Book and republish your CHM file.
http://kc.author-it.com/#b45755t1169n82756


How to invoke a Python script automatically when VLC media process starts everytime on Ubuntu?

Question: I'm making a Python script that will save the song path whenever VLC media player runs on a Ubuntu System. The script is ready but if I run this script at the startup, it continuously uses cpu share in the loop. I want to invoke the script automatically whenever the user starts vlc media player. How can I do it? Can it be possible with any shell scripting?
Answer: <code>import os,time

processname = 'enter the VLC process name'
tmp = os.popen("ps -Af").read()
proccount = tmp.count(processname)

while proccount > 0:
    print(proccount, ' processes running of ', processname, 'type')
    runsongpathfunction()
    time.sleep(1)
</code>
So what this does is it finds the process from the process name then when VLC is closed the script closes and it stops doing whatever it was doing while VLC was running because the while loop is no longer True. I added a time.sleep(1) to save resources its not really needed but it saves CPU.
Comment: So this script will invoke automatically whenever I run VLC on my system or I have to add this as the startup application?
Comment: You have to add the startup application and fill in the VLC process name variable. I would recommend using a chron job.
Comment: I'm facing two problems with this. First, if I add it as the startup application and if vlc is not running, it will break the while loop and the script will stop. Secondly, even if vlc is not running, it is giving me 4 proccesses running of 'vlc' type. WHY?


How to edit a message sent in a dm [Discord.JS version 14.14.1]

Question: When I attempt to edit the message I get the error:

const submit = new ButtonBuilder()
            .setCustomId('submit')
            .setLabel('Submit')
            .setEmoji('')
            .setStyle(ButtonStyle.Success);
const row = new ActionRowBuilder()
            .addComponents(submit);
await i.message.edit({ components: [row] });

I have tried fetching the message then trying to edit it but get the same error.
What I expect to see is just the message being edited to disable the button.
Answer: After doing a lot of looking up I tried simply searching for the dm channel then searching for the message. This worked and fixed the issue.
const targetChannel = client.channels.cache.find(channel => channel.id === i.message.channelId);
        const msg = await targetChannel.messages.fetch(i.message.id)
        const out = await msg.edit({ components: [row] }).catch(console.error);


Is there a maximum number of connections for Unix domain sockets?

Question: For TCP/IP connections there is a maximum of 64k connections between two hosts if there's a single destination port (source: What is the theoretical maximum number of open TCP connections that a modern Linux box can have). Is there a connection limit for Unix domain sockets or is it just limited by the number of file descriptors?
Answer: I found two limitations on unix domain sockets:
1 - limitation based on the number of concurrent file descriptor a process can open:
The number of concurrent clients is limited by the number of files a process can open take off some used file descriptors = 1014 concurrent connections by default, however this can be changed using the ulimit command.
read more here https://titanwolf.org/Network/Articles/Article?AID=2fbb967c-c507-4798-990d-edc3a9b132a8
2 - The autobind feature limit of address size, essentially, if you are using the autobind feature or the security flag, the address limitation is of 2^20 autobind addresses. From Linux 2.1.15, when the autobind feature was added, 8 bytes were used, and the limit was thus 2^32 autobind addresses.  The change to 5 bytes came in Linux 2.3.15.
read more here https://man7.org/linux/man-pages/man7/unix.7.html
Answer: If you are relying on autobind to supply addresses, there is a limit imposed by address space, 2^20 on modern kernels, which is addressed in the unix(7) man page.
I would think as a practical matter the constraint would be open files, as you state.


Difference between three technique and five technique defensive linemen

Question: During the predraft period, many scouting reports will mention that a defensive end can play a "three technique" or is better suited for a "five technique" role. Being unsure of what that meant, I did a little research to find out what these scouting terms meant. This article seemed to have the best explanation and a diagram:

Based on this article's explanation, three technique linemen are usually quick defensive tackles in a 4 man line, while five technique linemen are usually stout defensive ends in a 3 man line and are tasked to occupy blockers. Picking one type or the other seems to have implications for the type of defensive alignment for a team.
Is this understanding correct?
Answer: Yes, your understanding is correct. The "technique" technically only refers to the the positioning of the linemen before the ball is snapped. This refers to all defensive linemen, but is typically only used when referring to defensive tackles. Also, when someone is referring to a "5 technique lineman" you can typically just replace that with a "3-4 lineman". Likewise, when some is referring to a "3 technique lineman" you can typically just replace that with a "4-3 lineman". Now, this isn't always the case, but it can be helpful when trying to communicate with someone who is using these terms.
Teams that play primarily 3-4 defenses will be most interested in players that play the 5 technique because, as the article you referenced states, in the "true 3-4" defensive front you have one defensive linemen playing the 0 or 1 technique and two defensive linemen playing the 5 technique. These linemen will typically play the two-gap.
Teams that play primarily 4-3 defenses will be most interested in players that play the 3 technique because in the "true 4-3" you will have one defensive linemen playing the 3 technique and one defensive linemen playing the 1 technique while the two defensive ends will play outside the line either in a 4 technique or in a wide nine set. The two defensive tackles will typically switch between playing the 1 and the 3 technique throughout the game which is why it is important to have two defensive tackles that are good at playing the 3 technique. These linemen will typically play the one-gap.
While this "technique" terminology only technically refers to the positioning of the players on the field, it is important to note that in each of these alignments are typically used to do specific things (containing the run from the 3, rushing the passer from the 9, etc...). So, offensive linemen (and spectators) can typically determine what the defense is trying to do just by looking at the alignment of the defensive line.


How to overcome "flooded" sound when singing?

Question: Lately I've been learning to sing, nothing fancy but I've noticed that I sound flooded (that's a word from google translate so I'm not sure if it's accurate. It feels like I have "stuff" in my nose that doesn't let the full sound come out.)
I hope someone understands what I mean since I'm not a native English speaker. It's hard for me to explain what's really going on.
Any tips on that?
Comment: It is a bit hard to understand what you mean, unfortunately. But if you are singing "though your nose" that is one problem right there at least. One suggestion is to try and find some basic singing tutorials on line.
Comment: *How* do you notice?  Does it sound bad to *you* when singing, or does it sound bad when you *record* it and listen to it?  Does it sound bad to *others*?  Efficient singing does not necessarily give a good impression of the results to the singer himself.
Comment: That sounds like a clear enough description to me, but, gosh, I have no idea.  I've never heard of that problem before.  Are you certain you _don't_ have something clogging your nose?
Comment: I'm not singing through my nose, it's just like the sound doesn't want to resonate... I don't know how to describe it more accurately. I asked a friend as well if I sound "flooded" and she said yes, so it's not just my impression. And this problem is persistent it's not like I woke up one day and sound like that, every time I record myself I hear that same thing that makes me repulsed about my voice :/
Comment: you should do a recording of you singing put it on youtube and include it in your post its impossible to tell without hearing you...do you have asthma or copd?
Comment: Try breathing in some menthol and eucalyptus mixture (from the chemist) in steamy hot water for five minutes. If this helps then you've found the problem.
Answer: You need to get singing lessons from a qualified voice teacher, in person. You need the immediate feedback of singing for someone who can listen to what you are doing and patiently teach you to how sing differently and improve your tone. There is no point in trying to solve this problem by reading a written description of it online.
Comment: I think if we heard the problem we could diagnose what he was facing in this situation. You don't need to go to elementary  school to learn to color in the lines.
Answer: I have a lot of phlegm that builds up while I'm singing.  There are a few things you can do to prevent this, especially on days of a performance.

Stay hydrated.  The mucous builds up as a result of your throat becoming dry and your body responding.
Avoid dairy and overly sweet food and drink.  These will build up excess fluids, as well.
Exercise daily.  A strong cardiovascular system can help you achieve and hold fuller tones.

For singers, unlike players, practice isn't enough.  Physical discipline is required to achieve your best results.
Answer: Yes I second the rec for lessons. There are specific exercises which, practiced daily, will help you extend range and find the many things your voice is capable of without damage. There is really a lot to it. I play several instruments (guitar, clarinet, sax) reasonably well but singing was the longest journey for me. I started lessons some years ago, stopped, started again, and in the last year or so I find I have a voice that I can really use. It's been a big work, but well worth it.
There are so many small recommendations that people could make, but without actually hearing you it doesn't mean much and could even push you in the wrong direction. There is one thing I can say though. If it hurts or feels uncomfortable - stop. Your vocal chords are an incredible but sometimes quite delicate instrument. Take care of them. Technique really matters with singing, even more than other instruments.
Good luck.


How would I make a search view display results in a new page instead of on the same page?

Question: I have view (a block) that searches my products for various fields such as sell price, title, ect. It works except all the search results appear right in that block instead of on a new page. Here is an example of what I want to do: http://www.themesdojo.com/drupads/
Comment: It would be better if you explain more details about the new page and what is missing in the new page.
Answer: You can show your exposed views filter as a block, and place the block on your search page, then all submissions will show results on your view page.
Under the advanced field-set in the views UI, once you have created an exposed filter, an option will appear under the EXPOSED FORM heading that will allow you to create a block.
You can see an example of a front page search leading to a views results page here... http://www.myweddingfayre.com
Comment: So does my product search view need to be a page then and not a block?
Comment: Yeah, I would create the view as a page, then expose the filter as a block, and through Structure > Blocks, place the block on a separate page.


Insert Or Update in Android

Question: I have a method like that
private void addDataToDailyStatisticsIncome(String date, String money, int idUser){
    Cursor res1 = db.getDataTableDailyStatistic(idUser);
    int temp = 0;
    int money1, money2;

    res1.moveToFirst();
    while (res1.moveToNext()) {
        if (date.equals(res1.getString(1)) == true) {
            temp = 1;
            break;
        } else {
            temp = 0;
        }
    }

    if(temp == 1){
        money1 = Integer.parseInt(res1.getString(3));
        money2 = Integer.parseInt(money);

        money1 = money1 + money2;

        long isUpdated = db.updateTableDailyStatistic(res1.getString(1), Integer.toString(money1), idUser, Integer.parseInt(res1.getString(0)));

    } else{
        long isInserted = db.insertTableDailyStatistic(date, money, idUser);
    }
}

I want to check when new data insert to app. If "date" is available, i will update "money" in same date.
Or if "date" is not available, i will insert new Date with Money.
But i have 1 problem that it always inserts, it never updates although i have a same Date in Database.
Can you help me fix this problem?
Comment: what data are you getting from `date` and `res1.getString(1)` variable?
Comment: Use debugger and check if (date.equals(res1.getString(1)) == true) this condition
Comment: res1.getString(1) is a type of my Database. And date is a Text of EditText.
Comment: yes that i understand but what **values** are you getting in those variable?
Comment: A Date like 03/05/2016
Comment: and from `res1.getString(1)` ?
Comment: same Date. I display values of Date and res1.getString1. They are the same.
But i think **temp == 1** never works. So i cant update
Comment: Are formats of both dates same? Have you checked debugging the app?
Comment: Yes. I checked it. But i think
**while (res1.moveToNext()) {

        if (date.equals(res1.getString(1)) == true) {

            temp = 1;

            break;

        } else {

            temp = 0;

        }

    }**

Is not working. temp's values is 0.
Answer: Follow these steps:

Check if date is available and get money1:
int money1 = 0;
boolean isAvailable = false;
String query = "select count(*) from TableName where date = ?";
Cursor cursor = db.rawQuery(query, new String[] {date});
if (cursor != null && cursor.moveToFirst()) {
    isAvailable = true;
    money1 = cursor.getInt(money1_column);
    cursor.close();
}

Insert or Update row:
if (isAvailable) {
    // update
} else {
   // insert
}

Answer: You can do it by declaring date as primary key. Write both sql query (insert and update ) in the the above method.

Insert Or Update in Android


HTTP POST request from react client app to spring-boot server returns 400 error

Question: I have configured a POST request in my react app like this:
await fetch('http://localhost:8080/api/room', { credentials: 'include' }, {
  'method': 'POST',
  'headers': {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Access-Control-Allow-Credentials': true
  },
  'body': JSON.stringify(somedata),
})
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log('Error', error);
});

In my spring-boot app I have a controller:
@RequestMapping(value = "/room", produces = "application/json", method = {RequestMethod.GET, RequestMethod.POST})
public ResponseEntity<Room> createRoom(@Valid @RequestBody Room room) throws URISyntaxException {
    Room result = roomService.saveOrUpdate(room);
    return ResponseEntity.created(new URI("/api/room/" + result.getRoomClientId()))
            .body(result);
}

In my browser console I get this response:
Response {type: "cors", url: "http://localhost:8080/api/room",
redirected: false, status: 400, ok: false, …}

In my spring-boot app console I get this error:
2019-04-08 16:31:42.742  WARN 86177 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<com.sbpoc.app.db.entity.Room> com.sbpoc.app.db.controller.RoomController.createRoom(com.sbpoc.app.db.entity.Room) throws java.net.URISyntaxException]

Error says, request body is missing?
Update
import uniqid from 'uniqid';
const someData = {
  roomNumber: uniqid(),
}

Room Entity:
@Entity
public class Room implements Serializable {

private static final long serialVersionUID = 3433823138738252949L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String roomNumber;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getRoomNumber() {
    return roomNumber;
}

public void setRoomNumber(String roomNumber) {
    this.roomNumber = roomNumber;
}

}
Comment: Could you add the output of `JSON.stringify(somedata)` and the contents of the `Room` class?
Comment: @Misantorp updated!
Comment: If the `body` is empty you'll need to show the output of `JSON.stringify(somedata)`, ie. by doing a `console.log(JSON.stringify(somedata))` if I am to try to help you out
Comment: @Misantorp I have managed to resolve the issue, the body definitely had data. The request ignored the second block of '{ method: ... }', so I merged the objects to one
Comment: Glad you figured it out :-)
Answer: I needed to include the credentials option into one object as such:
fetch('http://localhost:8080/api/room',
  {
    method: 'POST',
    credentials: 'include',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Access-Control-Allow-Credentials': true
    },
    body: JSON.stringify(data)
  }
)
.then(res => console.log(res));

I also made use of @PostMapping instead in the springboot app:
@PostMapping("/room")
public ResponseEntity<Room> createRoom(@Valid @RequestBody Room room) throws URISyntaxException {
    log.info("Request to create room: {}", room);
    Room result = roomService.saveOrUpdate(room);
    return ResponseEntity.created(new URI("/api/room/" + result. getRoomClientId()))
            .body(result);
}


Deny client code access to a third-party library in .NET

Question: I am writing a Library using C# and my library uses a third party .NET library. When I roll out my Library I have to include the third-party library as well along with my library. However I want to prevent users of my library (client code) from directly using the third-party library. Is there any way to enforce that at compile time or run time?
Comment: No, If I can access it, I can use it.
Answer: If there are no legal issues with doing so, you can use ILMerge to merge the 3rd party library into your own one.
You have the option to make public classes from the 3rd party library internal. This way, clients of your code will not be able to access functionality from the 3rd party library (unless they do some kind of hack. For example, I am not sure if someone can use an internal class using some kind of Reflection code).
Take a look at this article
Comment: If you make the public 3rd party classes internal via ILMerge, you have more security. Security is a relative thing. You can also [obfuscate](https://en.wikipedia.org/wiki/Obfuscation_(software)) your library (with the merged 3rd party library). This will make it even more secure. Other users will not even know that the third party library is in use.
Comment: I agree with you. If someone is is determined to hack it, he will. However, as I said, security is a relative thing. Maybe for the user who asked this question (user2242746), making it hard to use the 3rd party is enough (satisfies his requirement).
Comment: @user2242746, what level of security would you accept?


Como delimitar um campo de telefone (Oracle DataBase)

Question: Bom dia,
Galera estou precisando de uma ajuda para delimitar o campo de telefone no meu banco de dados, sendo mais especifico, se hoje alguém digitar um numero de telefone com 4 dígitos (por exemplo) esse telefone será salvo, eu preciso que seja aceito somente números entre 8 e 9 dígitos, estou usando o Oracle 11g.
Comment: Isso não pode ser feito na aplicação? Você tem que tratar no BD?
Da pra usar uma `trigger` de `before insert`, se realmente precisar fazer no BD.
Answer: Uma solução pode ser criar uma check constraint.
Por exemplo
<code>ALTER TABLE  CLIENTES
ADD CONSTRAINT CHK_CLIENTES_TEL
  CHECK (TELEFONE IS NULL OR LENGTH(TRIM(TELEFONE)) IN (8,9));
</code>
Aqui por exemplo irá se permitir telefone nulo , cliente não tem não telefone , etc,  mas se preenchido deverá ter tamanho de 8 ou 9.
Comment: E que devem existir valores no campo TELEFONE que violam a CONSTRAINT , 'limpe" estes campos antes de criar a mesma.
Comment: Então cara eu estava tentando algo BEM parecido porém não com a mesma sintaxe e estava obtendo um erro, fazendo dessa maneiro que você descreve eu recebo o seguinte erro: ORA-02293 não é possível validar (chk_clientes) - restrição de verificação violada


Why does default.settings.php suggest writing the value of the hash salt in an external file?

Question: The default.settings.php file contains the following comment:

For enhanced security, you may set this variable to the contents of a file outside your document root; you should also ensure that this file is not stored with backups of your database.
Example:
$settings['hash_salt'] = file_get_contents('/home/example/salt.txt');

Assuming the attacker was able to create a PHP file inside my Drupal installation, in which way does writing the hash salt in a file increases the security? In any case, the attacker would be able to read the value for $settings['hash_salt']. Eventually, the attacker could change the value for $settings['hash_salt'], but does doing this make any sense for the attackers to do?
Answer: The main reason to do this is to prevent the salt from accidentally being committed to your content versioning system.
Most advanced hosting environments (both Drupal hosts and all-in-one Drupal stacks) provide ways to do this.
Similarly, it is useful to go through the trouble of abstracting DB settings and the like, both for security and to make it easier to move your code from one environment to the next.


Special number " 279146358279" we found in calculation for personal id number in Romania

Question: We, in Romania, do not understand how is calculated the special mathematical number 279146358279 , which is prezent in calculation of CNP (personal identification number) received by all citizen from romanian authorities.
This number refers to Modulo 11.
This number is described for Romania also in Wikipeda. Please lookdown (search) for Romania:
https://en.m.wikipedia.org/wiki/National_identification_number
Thank You!
Ioan Rudu
Comment: The wiki article shows only that the number is chosen, and fixed for all checksum computations. It is not "computed". (Possibly, people made a statistics of all numbers that can occur, then some "weights modulo eleven" were chosen so that it is easy to distinguish cases of "simultaneous errors".)
Comment: Thank you! Thank you! Thank you!
Answer: This is a fairly standard way of making sure a number "makes sense" and is not gibberish.  You might look up on Google IBAN codes, used in banking: https://en.wikipedia.org/wiki/International_Bank_Account_Number
The basic idea is to add some digits that come from some 'scrambling' of the original digits in such a way that you can determine with a high probability if the number is valid without requiring looking it up.
As a really simple example, suppose it were like the U.S. and everyone has an 9 digit number, which we write like this:  050-27-1234.  One way to do this would be to add up all the digits, take the last digit in the result, and add that to the end.  In this case,
$$0+5+0+2+7+1+2+3+4= 23 $$
So the "check" digit is 3 (we drop the '2' from 23), and we now say the number is 050-27-1234-3.
Now you can see it is easy to check if a number is valid by looking at it.  For example, if someone mis-wrote it as 060-27-1234-3, we know right away it is an error since the number doesn't 'check' properly.
The approach used in your country is a bit more complex; this is a simple 'sum of the digits modulo 10' checksum.  But it wouldn't catch some errors that might easily cancel each other out.  For example 050-27-1235-3 checks out OK, but this might indicate two different numbers were written wrong.  The whole thing of "multiply each digit by a digit in the magin number and divide modulo 11" just makes it harder for simple little errors to offset each other, and makes the checksum more effective.
Why it works and how these get built is covered in a field (in engineering) called "error-correcting codes" you could Google if you want an explanation, but the actual math gets pretty complex.
Comment: Thank you! Thank you! Thank you!


Can excel get a inverse function from a self-defining function? If it cannot, what kind of software can do it?

Question: if I have a self-defining function y=2x+1, I want to get its inverse function X=(Y-1)/2, Can excel help? or other software like sigmaplot or Matlab?
Answer: With MATLAB, if you have Symbolic Math Toolbox , you can do this using:
syms x y;
eqn = y == 2*x - 1 ;

x = solve(eqn,x)


Trying to get last x unique entries, and a corresponding value an another column via script

Question: So I'm trying to solve a problem with a sheet I have,
it contains a Vlookup where people enter the date, their Intials, the custID and it displays
a value from that row. (the original data is form responses, with emails, time stamps and 2 formulas working on the end)
This hasn't been the best solution so I was thinking about trying to automate this by showing the looked up value on the submission page, but this is quite beyond me.
So then I thought of perhaps an auto lookup via script that works as follows, (bearing in mind Col A is timestamp, B is email addy & column X has the value we want to return.)
I want it to start from last row, find the last x unique values from B, (possibly using A to find the greatest or last entry by date/time) and add row to an array for later (entire row isn't actually needed I guess, just B and X.)
Once it has it's 10 in the array, output Column B of array and Z of array)
Using my limited scripting knowledge and posts on here, this is where I'm at so far.
 function lastUniqueList(){
 var sheetName = "SheetNameHere"; // sheet name with data
 var columnNo = 2; // A = 1, B = 2 Column that we want the unqiue values from. IE last x unique email addresses.
 var uniqueToReturn = 10; //how many unique entries, ie, how many unique emails.
 var uniqueLookup = 24; // Column number that has the value we want looked up and returned.

 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
 var data = sheet.getDataRange().getValues();
 var targetData = [];
 var counter = data.length;

while (targetData.length < uniqueToReturn)
  {

    if (data[counter][columnNo - 1] != "" && data[counter][columnNo - 1] != targetData.getDataRange.getValues()[columnNo - 1])
    {

      targetData.push(data.splice(counter, 1)[0]);
    }
    else
    {

      counter--;
    }
  }

 }

So now I'm at a brick wall as to how to compare entries in my datarange to the array to avoid duplicates.
The 2nd part of the if statement, after the && isn't right. I'm trying to work it out.
If anyone could give me any pointers, or some good links, I'd appreciate it.
Also, I hope I've got it right and it's going backwards from last row. Logger did show correct row numbers etc when testing data.length
Will carry on searching the boards.
Answer: So here's what I came up with (assuming I understood the problem correctly):
function lastUniqueList(){
    var sheetName = "SheetNameHere"; // sheet name with data
    var columnNo = 2; // A = 1, B = 2 Column that we want the unqiue values from. IE last x unique email addresses.
    var uniqueToReturn = 10; //how many unique entries, ie, how many unique emails.
    var uniqueLookup = 24; // Column number that has the value we want looked up and returned.

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
    var data = sheet.getDataRange().getValues();
    var targetData = [];

    for (var counter=data.length-1; counter >= 0 && targetData.length < uniqueToReturn; counter--) {
        if (data[counter][columnNo - 1] == "") {
            continue;
        }
        if (targetData.some(function (element, index, array) {
            return data[counter][columnNo - 1] == element[columnNo - 1];
        })) {
            continue;
        }
        targetData.push(data[counter]);
    }
}

The secret sauce is in that Array.prototype.some method.  It's asking if there are any elements in targetData that pass some test.  The test is having a value at columnNo matching the one we're thinking of inserting.  That's how we guarantee the unique values.
I also converted the while loop to a for loop because it's neater.
Here's the jsFiddle where I tested my solution.
Comment: Thanks, I'll go have a play now and let you know how I get on :)
Comment: Awesome, everything works, I've added to the code to output the results on a page.  theres a few extra vars etc

   targetSheet.getRange(2, 1, archiveLength,   archive[0].length).setValues(archive);

So it's constantly overwriting from row 2. Ideally I only want columns B & X shown, so that's what I'm looking into next. Many thanks.
Comment: Thank you @Jeffery managed to work out the last bit, i'm sure there is a tidier solution, but here are my efforts :)
http://jsfiddle.net/DXyB8/


Executing Process.Kill code when WCF service is being disposed

Question: I have a WCF service hosted on IIS, which references a legacy tool, via COM interface.
This legacy tool is a black box for me, so I can't change its code or fix bugs in it.
When I restart my WCF service, or stop it via the IIS management tool, the referenced legacy tool should shut down as well, along with all other Unmanaged data it uses, and start again on the first call from the restarted WCF service.
Everything works fine, except one function call on this legacy tool. If I call this specific function, there is probably some bug inside the tool, which prevents it from going down, even if I stop my WCF service.
I thought about a workaround in which I'll call a Process.Kill function every time my WCF service app is being disposed. My difficulty is that I can't find the suitable place to call it.
First I tried to make my Service behavior class to inherit from IDisposable, and then implement a Dispose method. The problem is that the Dispose is called every time a function finished it's flow, but I can't kill the legacy tool every call of course.
My WCF service communicates with the client app via a Net Pipe channel, so from what I understand, I can't use a Global.asax file.
My question is - Where is the suitable place to call the Process.Kill code, if I want it to be called only once, when the whole WCF service application is being disposed.
Thanks.
Comment: if IDisposable is being called with every function, it means your service instance context mode is PerCall ? if so, would it be ok to switch it to Single ? this way your service instance lifetime will be aligned with the runtime lifetime?   http://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode(v=vs.110).aspx
Comment: The client which I use to communicate with the service is closing the channel after every call, and I can't and shouldn't change it.
If I change the Service context mode to Single, can it cause a problem in communication between the two?
Comment: It should not, if it creates a new instance for every call, it should work fine with one single instance for all calls, it means the service is completely stateless, .. usually the opposite scenario is the one that could lead to problems, switching it from Single to Percall, coz it will kill any state used across calls.  So I would try switching that service to Single, IDisposable then will be called once.
Answer: At the end, I solved the problem by calling the Kill process code in the static constructor of the service behavior. This constructor is called once even for PerCall context mode, so actually for me it has the same effect as if I called the code on the Dispose phase in single context mode.
Thanks for all of you.
Answer: You would need to implement a custom service factory to intercept the closing event see:
Performing action before WCF service is shutdown


Conditional probability with three events

Question: If I know the conditional probability of one event P(A|B) and then I know the probability of the event B given another event C, i.e. P(B|C), can I easily calculate P(A|C) ?
Answer: No. Consider the following: Let $U$ be a standard uniform random variable (uniformly distributed on $[0, 1]$).

$A=C=\{0\le U\le1/2\}$, $B=\{1/4\le U\le3/4\}$.
$A=\{0\le U\le1/2\}$, $B=\{1/4\le U\le3/4\}$, $C=\{1/2\le U\le 1\}$.

In both cases, $P(A|B)=P(B|C)=1/2$, but $P(A|C)=1$ in the first case and $P(A|C)=0$ in the second case. In general, $P(A|B)$ and $P(B|C)$ are not sufficient for calculating $P(A|C)$.


How to Compute fields on the fly

Question: I have content type having fields as follows.
first
second
third
Sum1 (first + second +third) - computed filed
xxxx
yyyy
zzzz
Sum2 (xxxx+yyyy+zzzz) - computed field
Grand Total (sum1 + sum2) - computed field
I could achieve this by using computed field module. When I save node, I could able to find sums and Grand Total.
My problem is client wants to see  sum1 filed in edit/create form and also wants to update sum1/sum2/Grand Total fields on the fly (dynamically).
There are couple of content types having the same requirement.
Before start to write a custom module, I would like to know alternates for this.Any suggestions?
Comment: When you say your client wants to update the sum fields dynamically, how is that even logically/mathematically plausible?  Or, are you asking that after your client modifies any of the first, second, or third fields that the sum fields should automatically update?
Comment: @Jimajamma I mean, whenever client enter/modify the values of first, second,or third fields the sum of these should automatically update 'total' filed.But here my problem is, if I use computed filed (to compute first second third) we can't see that filed in add/edit node form.
Answer: This sounds like cashier functionality. There has been some effort for D6, see Drupal POS (Point of Sales) System. I haven't used this.
Ideally, this would be done with JavaScript (I have once done it for a non-Drupal system).
A cheap workaround would be the Save & Edit module, where you just save the node and continue editing it, effectively updating the calculations. On demand, not automatically.
A smoother workaround would be updating (on demand) the form with ajax. Ajax module  has no D7 version, but in the issue queue is mentioned this sandbox project which supposedly can ajaxify the node edit form:
Asaf (ajax submit for any form).
You need one extra module for actually displaying Computed field on node edit form: Field extra widgets. Select the widget "read only".
Comment: Thanks Kari Kääriäinen, I'm going to work around your suggestions. Here my problem is If I use computed filed I can't see the filed in node form. I can see that field value at node output only.
Comment: You're right. There is a module for that ;) Answer edited.
Comment: Kari Kääriäinen - As you said I've just installed and tried that for computed filed (by selecting widget type Read only). it's showing up in edit form. but not in add form.  thanks for the idea:)
Comment: Oh no. Giving the field a default value would be the next logical step, but that doesn't work either.
Comment: Kari Kääriäinen - Your modules helped me to solve my problem, by using computed field, field_extrawidgets, & save_edit modules. Ofcourse my way is not the exact solution for my question but client agreed to this :)

How to Compute fields on the fly


Django Migrations: How to resolve error of charfield to datetimefield

Question: What im doing is i clone my prod database to sandbox. and I'm getting error while migrate Cause of previously I made charfield to datetime field. And then datetime field to Charfield after few migrations.
So how do I solve below error:
Error :
python manage.py migrate
System check identified some issues:

WARNINGS:
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, pro_auth, service_list, sessions, token_blacklist
Running migrations:
  Applying pro_auth.0002_alter_userorder_order_booking_time...Traceback (most recent call last):
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.InvalidDatetimeFormat: invalid input syntax for type timestamp with time zone: "2021-10-10 10.00"

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 244, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 608, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/postgresql/schema.py", line 196, in _alter_field
    super()._alter_field(
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 765, in _alter_field
    self.execute(
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 145, in execute
    cursor.execute(sql, params)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/root/sandbox/sandbox_venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: invalid input syntax for type timestamp with time zone: "2021-10-10 10.00"

And Here is Migration file 0002_alter_userorder_order_booking_time:
from django.db import migrations, models
import django.utils.timezone

class Migration(migrations.Migration):

    dependencies = [
        ('pro_auth', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='userorder',
            name='order_booking_time',
            field=models.DateTimeField(default=django.utils.timezone.now),
        ),
    ]

Answer: I think, you need fake migration.
Try this
./manange.py migrate --fake <app_name_of_userorder_is_inclueded> <last_migration_number_which_synced_current_order_booking_time>
This make skip applying alter table.
Answer: You're not passing a valid DateTimeFormat. I think you should write
field=models.DateTimeField(default=django.utils.timezone.now()),

based on what is shown here: Django default=timezone.now() saves records using "old" time


Equal temperament

Question: Practically speaking: Does equal temperament only mean arranging a set of intervals (scale) such that it can be repeated (similarly t o what is defined in 12 tone equal temperament as an octave)?
Continuing in a practical vein: Do non-equal temperament scales essentially do away with the concept of "repeating scales"?
Are all non-equal temperament scales just intonation scales. In other words, if I were to construct a scale made up of progressive (low to high) random sounds (and those sounds didn't by some wild coincidence result in an 'octaved' scale) would that by definition be a just intonation scale?
Comment: I'm not sure what you mean by "repeating" scales; most (non experimental) note systems repeat in octaves, i.e. if a given note is in the system, then the note an octave above and below it is in the system too.  This is true for 12TET, just intonation, various meantone tunings etc.
Answer: No, no, and no.

Equal temperament means you take some given interval – usually the octave – and divide it into an integer number of equal steps (in log-frequency domain). This obviously implies that everything repeats, but that alone doesn't characterise an ET tuning.
It's also perfectly possible for a non-tempered scale to repeat. An example is the Fibonacci scale: perfectly regular and repeating, but not based on equal divisions of a given fixed interval.
If by "repeat" you actually mean that it's completely "translation invariant", then the statement is kind of true: a scale that's symmetric under any interval transposition is necessarily either equal-stepped or infinitely dense.
If any non-tempered scale were by definition just intonation then that term would be pretty useless. No, just intonation is generally taken to mean the intervals are tuned to integer frequency ratio. You could create a just-intonation scale with random numbers, but not by choosing random independent frequencies – rather you would need to choose a single base frequency and random rational numbers that specify the intervals between the note's scale. Technially, if you choose random frequencies with integer Hertz number then the intervals are in fact all rational, but still couldn't really be called just intonation because the numbers are far to high. Generally only small fractions are considered (3:2, 5:4 and compounds thereof, more rarely 7:4, in more exotic genres possibly 11:9 or 13:8): what's important that the integer ratio can actually be heard, by means of coincident overtones.
Answer: Equal temperament, historically, specifically refers to dividing the octave into 12 notes equally. This allows you to play in different keys without sounding too out of tune. Before equal temperament, tuning was based more on how the notes of the scale, and out of the scale, relate to the tonic of the instrument. This is just intonation.
So since these two terms refer to specific things in western classical music I would think it would be confusing to use them to describe other tuning systems.
So by default, if a scale is not equal  temperament it does not mean it is just intonation.
If you did want to use these terms you might want to qualify them, like "new equal temperament". Or something like that.
Comment: What's the tonic of a pipe organ?
Answer: Conceptually, you could have a equal-interval scale that never "repeats" a note. For example, suppose the pitch difference between each note is 103 cents not 100 (103 happens to be a prime number).
Historically there were many practical non-equal temperament scales that repeated notes in each octave - for example the various mean-tone temperaments.
A just intonation scale has pitches which are exact fractional multiples of each other (e.g. 3/2, 5/4, 5/3, etc) and in practice the fractions much be made from fairly small integers, otherwise you can't recognise the "just intonation" by ear - and my personal definition of "music" is basically "sounds that are intended to be understood by listening to them", not by some abstruse theoretical argument about them.
You could argue the position that any possible interval can be approximated as accurately as you like by a fractional ratio between big enough integers, therefore all scales (both equal and non-equal temperament) are really just intonation scales, but I think that's just playing a mathematical game so far as music is concerned.
Comment: 103 being prime is (largely) irrelevant -- this tuning system would repeat notes every 1200 octaves, but a 49-cent step would also repeat every 1200 octaves. In practice, the way the ear works (physics and maths) means that any scale will repeat notes every octave.
Comment: When you say: "in practice the fractions much be made from fairly small integers, otherwise you can't recognize the "just intonation" by ear" do you (also) mean 'otherwise you can't distinguish the sounds by ear. If not, why?
Comment: Your ear can distinguish any sufficiently different frequencis, but it can't "hear" them as a particular (harmonising) interval unless the ratios is one of small integers, so that for example a major 6th is a 5:3 ratio. ~~~~
Comment: @BrianChandler I'm quite sure that a frequency ratio of 8409/5000 will also be perceived as a major sixth, as will 9999/5999.
Answer: The temperaments used at Bach's time were experimental (and not only "just interval scales ") with an eye on creating satisfying chords and intervals .The experiment mainly was to divide and distribute the Comma around the scale in different ways. There are dozens of temperament scales.
The Equal Temperament is amazingly accepted as normal by the majority even though the dischords and ugly intervals are many and various. The more you listen for them the worse it gets. I hear bad intervals more than out of tune chords (personally). I like to imagine the earliest Stoneage attempts at scales and chords using lumps of bone /stone / wood which made pleasant chords when played together. This process would soon have revealed an octave and then the other satisfying combinations which would lined up to make an early scale. That's basically where scales started. Just use that as a background idea before education and technology arrived. So with Equal Temperament any scale is better than none.
Answer: Equal Temperament is a Mathematical division of an octave using logarithms. A natural scale that a singer would use would not match the same frequencies. Higher or lower scales in Equal Temperament sound pretty much the same apart from the pitch. Basically it's an unnatural construction to make Piano Manufacturing less complicated.
It's strange that such an unpopular subject as Mathematics has overwhelmed the Art of Music so effectively.
Comment: It is not strange that mathematics has "overwhelmed" the art of music: mathematics is about patterns, and is the best way to explain the patterns that make music. "Popularity", by the way, has nothing to do with whether things are true or not.

Equal temperament


Prove that for any $n$, we have $\sum_{i=1}^n \frac{a_i}i \ge a_n $.

Question: Consider the sequence $(a_n)_{n \ge1}$ of real nos such that $$a_{m+n}\le a_m+a_n,\ \forall m,n \ge 1$$
Prove that for any $n$, we have $$\sum_{i=1}^n \frac{a_i}i \ge a_n .$$
Comment: Is $a_n$ nonnegative ?
Comment: not necessarily
Answer: This is Problem 2 from the Asian Pacific Mathematical Olympiad (APMO) 1999.
I took the following proof essentially from AoPS:
We will prove this by strong induction. Note that the inequality holds for $ n=1$. Assume that the inequality holds for $ n=1,2,\ldots,k$, that is,
$$ a_1\ge a_1,\quad  a_1+\frac{a_2}{2}\ge a_2,\quad  a_1+\frac{a_2}2+\frac{a_3}3\ge a_3,\quad  \dotsc,\quad  a_1+\frac{a_2}3+\frac{a_3}3+\cdots+\frac{a_k}k\ge a_k.
$$
Sum them up:
$$ ka_1+(k-1)\frac{a_2}2a_2+\cdots+\frac{a_k}{k}\ge a_1+a_2+\cdots+a_k.$$
Add $ a_1+\ldots+a_k$ to both sides:
$$ (k+1)\left(a_1+\frac{a_2}2+\cdots+\frac{a_k}k\right)\ge (a_1+a_k)+(a_2+a_{k-1})+\cdots+(a_k+a_1)\ge ka_{k+1}.$$
Divide both sides by $ k+1$:
$$ a_1+\frac{a_2}2+\cdots+\frac{a_k}k\ge\frac{ka_{k+1}}{k+1},$$
i.e.
$$a_1 + \frac{a_2}{2} + \frac{a_3}{3} + \cdots + \frac{a_{k+1}}{k+1} \geq a_{k+1}$$
which proves the induction step.


Lyapunov stability

Question: i have a question regarding Lyapunov stability and basin of attraction.
Let
$${x}'=-x-y$$
  $${y}'=2x-y+y^3$$
Use $$V(x,y)=x^{2}+\frac{1}{2}y^{2}$$ to determine the stability of (0,0) and a basin of attraction.
We have $$\dot{V}(x,y)=2x(-x-y)+y(2x-y+y^3)$$ or
$$\dot{V}(x,y)=-2x^2-y^2+y^4$$.
Now

V is positive definite and continuously differentiable
V(0,0)=0

Now,$$\dot{V}(x,y)$$ is negative definite if $$-y^2+y^4<0$$ or
$$-1<y<1$$ .
Now the textbook says that in order for $$x^{2}+\frac{1}{2}y^{2}=c$$ to stay in the strip $$ \kappa  = \left \{  \right.(x,y):x \in R,y \in (-1,1) \left.  \right \}$$ it must hold true that $$c\in (0,1/2)$$.
This is where i stuck ,i don't understand the last step from which he concluded that $$c\in (0,1/2)$$.Can someone help me?
Answer: $$
%
  f(x) =
%
\left[ \begin{array}{c}
  \dot{x} \\ \dot{y}
\end{array} \right]
%
=
%
\left[ \begin{array}{c}
  -x -y  \\ 2x - y + y^{3}
\end{array} \right]
%
$$
Before looking at the Lyapunov function, let's take a peek at the phase portrait. The red dashed line represents $\dot{x} = 0$, purple $\dot{y}=0.$

The critical points are the origin and $(\pm \sqrt{3}, \mp \sqrt{3})$

Lyapunov function
$$
 V(x,y) = x^{2} + \frac{1}{2} y^{2}
$$
Gradient of Lyapunov function
$$
 \nabla V = \left[ \begin{array}{c}
   2x \\ y
 \end{array} \right]
$$
Time derivative
$$
 \dot{V} = \nabla V \cdot f =
\left[ \begin{array}{c}
   2x \\ y
 \end{array} \right]
\cdot
\left[ \begin{array}{c}
  -x -y  \\ 2x - y + y^{3}
 \end{array} \right]
= -2x^{2} - y^{2} + y^{4}
$$
This function is shown below. The red contour line is the $\dot{V}=0$ contour. The region between these two red lines is the region where $\dot{V}<0$. The band for $-1 < y < 1$ is now apparent.
Answer: This problem can be handled with an optimization procedure, having in mind that generally is a non convex problem. The result depends on the test Lyapunov function used so we will generalize to a quadratic Lyapunov function
$$
V(p) = p^{\dagger}\cdot M\cdot p = a x^2+b x y + c y^2,\ \ \ p = (x,y)^{\dagger}
$$
and
$$
f(p) = \{-x - y, 2 x - y + y^3\}
$$
with $a>0,c>0, a b-b^2 > 0$ to assure positivity on $M$. We will assure a set involving the origin $Q_{\dot V}$ such that $\dot V(Q_{\dot V}) < 0$. The optimization process will be used to guarantee a maximal $Q_{\dot V}$.
After determination of $\dot V = 2 p^{\dagger}\cdot M\cdot f(p)$ we follow with a change of variables
$$
\cases{
x = r\cos\theta\\
y = r\sin\theta
}
$$
so $\dot V = \dot L(a,b,c,r,\theta)$. The next step is to make a sweep on $\theta$ calculating
$$
S(a,b,c, r)=\{\dot V(a,b,c,r,k\Delta\theta\},\ \ k = 0,\cdots, \frac{2\pi}{\Delta\theta}
$$
and then the optimization formulation follows as
$$
\max_{a,b,c,r}r\ \ \ \ \text{s. t.}\ \ \ \ a > 0, c> 0, a c -b^2 > 0, \max S(a,b,c,r) \le -\gamma
$$
with $\gamma > 0$ a margin control number.
Follows a MATHEMATICA script which implements this procedure in the present case.
<code>f = {-x - y, 2 x - y + y^3};
V = a x^2 + 2 b x y + c y^2;
dV = Grad[V, {x, y}].f /. {x -> r Cos[t], y -> r Sin[t]};
rest = Max[Table[dV, {t, -Pi, Pi, Pi/30}]] < -0.5;
rests = Join[{rest}, {r > 0, a > 0, c > 0, a c - b^2 > 0}];
sols = NMinimize[Join[{-r}, rests], {a, b, c, r}, Method -> "DifferentialEvolution"]
rest /. sols[[2]]

dV0 = Grad[V, p].f /. sols[[2]]
V0 = V /. sols[[2]]
r0 = 2;
rmax = r /. sols[[2]];
gr0 = StreamPlot[f, {x, -r0, r0}, {y, -r0, r0}];
gr1a = ContourPlot[dV0, {x, -r0, r0}, {y, -r0, r0}, ContourShading -> None, Contours -> 80];
gr1b = ContourPlot[dV0 == 0, {x, -r0, r0}, {y, -r0, r0}, ContourStyle -> Blue];
gr2 = ContourPlot[x^2 + y^2 == rmax^2, {x, -r0, r0}, {y, -r0, r0}, ContourStyle -> {Red, Dashed}];
Show[gr0, gr1a, gr1b, gr2]
</code>
Follows a plot showing in black the level sets $Q_{\dot V}$ an in blue the trace of $\dot V = 0$. In dashed red is shown the largest circular set $\delta = 1.31977$ defining the maximum attraction basin for the given test Lyapunov function's family.

Lyapunov stability


New South Wales and cricketing talent

Question: New South Wales has produced a lot of top-level cricketers. However, not all of them have gone on to represent NSW at a first-class/List A level. Many have switched loyalties, such as Sir Don Bradman (SA), Phil Hughes (SA), Adam Gilchrist (WA), Adam Zampa (SA) and Usman Khawaja (QLD). Others, such as Nathan Lyon, may have started their careers elsewhere (Lyon himself started at SA, although he is at NSW now).
Is this a function of population (NSW is the most populous state in Australia), or is there something deeper? Or am I wrong about this "phenomenon" (that NSW has produced a lot of cricketers of whom many have gone on to play elsewhere) in the first place?
Answer: At a simple level, this is a numbers game. Australia has a population of approximately 25 million, and 6 state sides. Very, very roughly this means that if you are in the top (66 / 25,000,000) = 0.0003% of the population you are good enough to play state level cricket.
New South Wales has a population of approximately 8 million. If we assume that cricketing talent is equally distributed throughout the country, this means that it will have around (8,000,000 * 0.0003%) = 21 "state level" cricketers. These can't all play for NSW, so half of them will have to play elsewhere.
This is a dramatic oversimplification for all sorts of reasons, but the same principles will apply even if you take a more accurate model: the ratio of people : available places is higher in NSW than the average for the country.

New South Wales and cricketing talent


Type error if link.has_attr('href'): TypeError: 'NoneType' object is not callable

Question: I was trying to execute the following python code
import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer

http = httplib2.Http()
status, response = http.request('http://www.nytimes.com')

for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')):
    if link.has_attr('href'):
        print link['href']

Edit:
I changed the code to this
for link in BeautifulSoup(response).find_all('a', href=True):
    print link['href']

But still getting same error
I  am getting the error
Traceback (most recent call last):
  File "/home/user1/Documents/machinelearning/extract_links.py", line 8, in <module>
    if link.has_attr('href'):
TypeError: 'NoneType' object is not callable

What is the reason for this error?
How can I solve this?
Answer: Your list is returning a bunch of values along with None in it.
You're better off using find_all() here, in my opinion:
for link in BeautifulSoup(response).find_all('a', href=True):
    print link['href']

The href=True will only find a tags with a href value, so you won't need your conditional.
Comment: @Haido, Same error Traceback (most recent call last):
  File "/home/user1/Documents/machinelearning/extract_links.py", line 7, in
    for link in BeautifulSoup(response).find_all('a', href=True):
TypeError: 'NoneType' object is not callable
Comment: @Haido, yes you are true
Comment: @karu Can't help you there I'm afraid :). But glad we resolved that :). So what `BeautifulSoup()` is doing is creating something empty. (i.e `None`)
Comment: @Haido  yes, :) Right. Problem exists still :)


check if "change" event is from keyboard

Question: I have on change trigger that gets the event :
  $(document).on('change', '.class', function (eve) {

How can I know if the change is from the mouse or from the keyboard by the event variable?
Comment: use `mousekeyup , mousekeydown` ,`keyup,keydown`.
Comment: https://www.w3schools.com/jsref/dom_obj_event.asp
Comment: https://www.bennadel.com/blog/2369-mouse-vs-keyboard---determining-click-initiator-using-a-jquery-custom-event.htm
Comment: May I know the reason it matters?
Answer: No, that is not possible because a change event may only fire when the concerned element loses focus, after several changes were made with several different input methods, including mouse and keyboard.
Test it with this input element: enter some text with keyboard, paste some text using the context menu (right click), and maybe move some characters by dragging them with the mouse into a different position. Then press the button. Only now you get the change event:

$(document).on('change', '.class', function (eve) {
    console.log('change event occurred');
});

<input class="class">
<button>Button</button>
Answer: To make sure the event is coming from the keyboard, use keydown. To make sure it comes from the mouse, use mousekeydown. So you would need to register 2 different events, for each device:
$(document).on('keydown', '.class', function (e) {
   // From keyboard
}
$(document).on('mousekeydown', '.class', function (e) {
   // From mouse
}


Log of Softmax function Derivative.

Question:
Could someone explain how that derivative was arrived at.
According to me, the derivative of $\log(\text{softmax})$ is
$$
\nabla\log(\text{softmax}) =
\begin{cases}
1-\text{softmax},  & \text{if $i=j$} \\
-\text{softmax}, & \text{if $i \neq j$}
\end{cases}
$$
Where did that expectation come from?
$\phi(s,a)$ is a vector, $\theta$ is also a vector. $\pi(s,a)$ denotes the probability of taking action a in state s.
Answer: The derivation of the softmax score function (aka eligibility vector) is as follows:
First, note that: $$\pi_\theta(s,a) = softmax =
 \frac{e^{\phi(s,a)^\intercal\theta}}{\sum_{k=1}^Ne^{\phi(s,a_k)^\intercal\theta}}$$
The important bit here is that the slide only identifies the proportionality, not the full softmax function which requires the normalization factor.
Continuing the derivation:
Using the $\log$ identity $\log(x/y) = \log(x) - \log(y)$ we can write $$\log(\pi_\theta(s,a)) = \log(e^{\phi(s,a)^\intercal\theta}) - \log(\sum_{k=1}^Ne^{\phi(s,a_k)^\intercal\theta}) $$
Now take the gradient:
$$\nabla_\theta\log(\pi_\theta(s,a)) = \nabla_\theta\log(e^{\phi(s,a)^\intercal\theta}) - \nabla_\theta\log(\sum_{k=1}^Ne^{\phi(s,a_k)^\intercal\theta})$$
The left term simplifies as follows:
$$left= \nabla_\theta\log(e^{\phi(s,a)^\intercal\theta}) = \nabla_\theta\phi(s,a)^\intercal\theta = \phi(s,a)$$
The right term simplifies as follows:
Using the chain rule: $$\nabla_x\log(f(x)) = \frac{\nabla_xf(x)}{f(x)}$$
We can write:
$$right = \nabla_\theta\log(\sum_{k=1}^Ne^{\phi(s,a_k)^\intercal\theta}) = \frac{\nabla_\theta\sum_{k=1}^Ne^{\phi(s,a_k)^\intercal\theta}}{\sum_{k=1}^Ne^{\phi(s,a_k)^\intercal\theta}}$$
Taking the gradient of the numerator we get:
$$right = \frac{\sum_{k=1}^N{\phi(s,a_k)}e^{\phi(s,a_k)^\intercal\theta}}{\sum_{k=1}^Ne^{\phi(s,a_k)^\intercal\theta}}$$
Substituting the definition of $\pi_\theta(s,a)$ we can simplify to:
$$right = \sum_{k=1}^N{\phi(s,a_k)}\pi_\theta(s,a_k)$$
Given the definition of Expected Value:
$$\mathrm{E}[X] = X \cdot P = x_1p_1+x_2p_2+ ... +x_np_n$$
Which in English is just the sum of each feature times its probability.
$$X = features = {\phi(s,a)}$$
$$P = probabilities =\pi_\theta(s,a)$$
So now we can write the expected value of the features:
$$right = \mathrm{E}_{\pi_\theta}[\phi(s,\cdot)]$$
where $\cdot$ means all possible actions.
Putting it all together:
$$\nabla_\theta\log(\pi_\theta(s,a)) = left - right = \phi(s,a) - \mathrm{E}_{\pi_\theta}[\phi(s,\cdot)]$$
Comment: Great answer, If I have the right to vote up 100 times, I would do that!
Comment: I'm just curious but, how far in a university education does one have to go to see this kind of math? Is this reserved for graduate students?
Comment: Thanks for such an elaborate explanation :)
Comment: What will the equation in case I use State as input and W[state size, actions count] to calculate the SoftMax?
Comment: For me, the main insight was to simplify the gradient of the log sum from the denominator of the softmax using the definition of the softmax: $$\pi_{\theta}(s,a)$$. Thanks!
Comment: Thanks for the great explanation!

Log of Softmax function Derivative.


How do I get a bucket by name in the .Net S3 SDK?

Question: I'm trying to find a method using the Amazon S3 SDK for .Net to get a bucket by its name. All I can find is ListAllBuckets(), but I really don't want to have to do that then try to find it from the response.
Answer: Use the ListObjects() method
using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSKey, AWSSecretKey)) {

    object req = new Model.ListObjectsRequest { BucketName = "MyBucket" };
    object resp = client.ListObjects(req);

}

Comment: I've noticed most sample code does not use `using` when initializing the `AmazonS3Client`. Not even Amazon's own sample code. I wonder why.
Answer: This worked for me:
public static S3Bucket GetS3Bucket(string bucket)
{
    try
    {
        AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID);
        return client.ListBuckets().Buckets.Where(b => b.BucketName == bucket).Single();
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        throw amazonS3Exception;
    }
}

Comment: I ended up doing something just like this for my solution. I'm marking this as the answer.
Comment: @Ryan It would be slightly better to use this:
return client.ListBuckets().Buckets.Single(b => b.BucketName == bucket);

No need to use unnecessary Where function.


Best way to go about my news menu item

Question: Drupal newb here.
I'm trying to rebuild an existing site into the drupal architecture.
For the sake of context, it is a site for a non-profit organisation that wants to make insights in a certain field of scientific research accessible to the general public.
Because the organisation is not large I would like to keep the current structure of the site. Now, my first problem is with creation of a menu item for news item. Clicking on that item is supposed to display news items. That's all I want for now.
What I've been trying to figure out reading pfds, surfing the web, watching videos about menu creation and taxonomy et.. for a little more than a DAY now, what is the best way plant this in Drupal (7.7 by the way)?
1.
 - Create content type called "news item"
 - Create a node using the basic page content type and give as title "News" enter "Latest    news" in the body field
 - Go to admin>structure > Menu and create a menu item and assign
   as path www.domain/news/news item
 - Each time a news item would be created, the author could then choose "News" as parent menu item.
This approach is obviously ignoring taxonomy. But how does one apply taxonomy to this anyway? As I understand, you can't create a vocabulary and then assign a menu item to that vocabulary name itself, simply because there doesn't seem to be a node that represents a vocabulary name. Correct me if I'm wrong(I kind of hope I'm wrong here).
Of course, You could create a vocabulary called "News" but and then one level below          "News" would have to be something like types of news as terms and then use those terms as nodes that I can assign menu items to. The problem is the types of news would be already too granular for the site that I want to rebuild with Drupal. I just want to have a content type newsitems to be children of of a menu item in my main menu called "news".
2. Just create a "content" taxonomy and have terms like News, and About, media, bios as terms (and thus menu items) and branch off from there.
This approach also seems besides the point of what taxonomy is trying to accomplish. The uberparent of any vocabulary is "content" for each and every site on the web anyway and doesn't describe anything at all, so what would be the use making a vocabulary out of it then? But then again, does it have to describe anything just as long is works and would it be a relatively decent "creative" solution?
Answer: Ok, I kind of found my way through it eventually. Still pretty amazing the amount of possibilities. Makes me dizzy.
Answer: I wanted an automatic way for content to be listed like a "menu". So I created the content type "page". Any time someone post a page I know this is a primary piece of content. So now on the sidebar I list out the pages A-Z. I'm using organic groups, so each page is posted to a group and those pages are only listed on that group, but you could do something similar with taxonomy.
Just my two cents to get around this problem.

Best way to go about my news menu item


Host website on a different domain, but leave the mail

Question: I would like to host my website on a different server (Google Cloud), but leave the mail and DNS in my current server. See below my current Zone Editor. I think I should change the indexambiental.com.br destination to the new host IP and the type to A, and leave the mail.indexambiental.com.br the way it is, but I'm not sure. Can you help me please?
Answer: Point the A record to your google cloud server and since you are using Google emails servers I think there is no need to change any thing with it.


Joining two tables on the max date of table b that's less than or equal to a date in table A

Question: I have a table that contains a list of all of the dates of a date range (table a), as well as another table that contains just business dates and their respective asset values (table b). I'd like to query for all values in table a, and join in the asset values from table b. In the event table A's date is not in table B, then bull in the asset value for the max date in table B that's less than or equal to the date value in table A
Table A:
DATE
1/1/2021
1/2/2021
1/3/2021
1/4/2021
1/5/2021
1/6/2021
1/7/2021
1/8/2021
1/9/2021
1/10/2021

Table B:
DATE    Assets
1/1/2021    6
1/3/2021    6
1/5/2021    3
1/7/2021    9
1/9/2021    10

Desired Results:
DATE    Assets
1/1/2021    6
1/2/2021    6
1/3/2021    5
1/4/2021    5
1/5/2021    10
1/6/2021    10
1/7/2021    9
1/8/2021    9
1/9/2021    2
1/10/2021   2

I've attempted something along the lines of the below with no luck. Any input would be greatly appreciated:
select a.[DATE],
        b.[Assets]
from A as a
left join B as B on a.DATE <= b.DATE

Answer: One possibility is to use outer apply to get the relevant asset for a given date.
select A.[date], B1.Assets
from TableA A
outer apply (
    select top 1 Assets
    from TableB B
    where B.[date] <= A.[date]
    order by [date] desc
) B1


Display contents of PHP variable after Joomla RSForm submission

Question: I have created a RSForm in Joomla which uses cURL to connect to an API and create a ticket on a third party system.
After the form is submitted, a ticket number is generated and stored in a variable $ticket. This variable is in the "Script called after form has been processed" section of the form.
I need to display the value of this variable along with a thank you message after form submission.
Something like "Thank you for you submission. Your ticket number is :......"
Answer: This is documented in the Getting Started/PHP Scripts section of the documentation.
You just need to modify $thankYouMessage, like:
$thankYouMessage .= "Your ticket number is: $ticket"

Comment: Perfect, that worked like a charm! Thank a lot Grisha, appreciated.


vanishing gradient and gradient zero

Question: There is a well known problem vanishing gradient in BackPropagation training of Feedforward Neural Network (FNN)(here we don't consider the vanishing gradient of Recurrent Neural Network).
I don't understand why vanishing gradient does not mean the zero gradient namely the optimal solution we want? I saw some answer said vanishing gradient is not exactly the zero gradient, just means the update of parameter is very slow. However, in the gradient decent, we don't want to achieve the exact zero gradient and we will stop when the parameter unchanges within $\epsilon,$ which is the same case of vanishing gradient.
So can anyone give me a clear answer?
Answer: Relatively low gradient doesn't always mean we have reached a critical point
Having a low value in a component of the gradient doesn't neccesarily mean that we are close to a critical point for that parameter. It means that the function will change little if we make an update based solely on that gradient component.
For example think about the function $f(w_1,w_2) = 0.001w_1^2 + w_2^2\rightarrow$ for a point with similar coordinates we will have a gradient component $1000$ times bigger in the $w_2$ direction than in the $w_1$ direction.
So in that function (that we can interpret as our cost function) if we initialize our parameters to a similar value this will lead to a way slower improvement in the $w_1$ direction as we can see in the next contour plot for a learning rate of $0.5$:

As we can see, even being far from the minimum at $(0, 0)^T$,  the improvements on $w_1$ are very little, so we need a lot of iterations to reach the minimum on $w_1$. And it reached the minimum after $1000$ iterations! So if we have initialized the algorithm with a farer point it wouldn't have reached the minimum at all.
So now we understand that even if we have a low value of the gradient, our parameters don't necessarily have to be close to the value that reach the minimum (or a critical point in general) of the cost function. In contrast, they may see their learning process severely slowed down.
In practice we can avoid this undesired behaviour using some modifications on the gradient descent method. For example see Adagrad. With this method, the components of the gradient are normalized based on the current and previous values of the gradient magnitude at each direction. Thereby we have a specific learning rate for each dimension.

Reasoning with backpropagation using a simple NN
To see why this smaller gradient components can happen also in neural networks, we can make use of a simple NN consisting only of one neuron per layer, just like the next one:

As we know, the element of the gradient given by the derivative of the cost function, $C$, with respect to a weight $w^l$ of the layer $l$, in a fully connected NN is given by the left term:
$$\frac{\partial C}{\partial w^l}= \delta^l (a^{l-1})^T \,\,\,\,\,\,\,\,\xrightarrow[]{\text{in our simple NN}}\,\,\,\,\,\,\,\, \frac{\partial C}{\partial w^l}=\delta^l a^{l-1}$$
Where $\delta^l$ is the "error" term ($\partial C/\partial z^l$), and $a^{l-1}$ represents the vector of activations of the neurons from the previous layer ($l-1$). Note that in the simple NN presented above we don't need to transpose $a^{l-1}$ as it is a scalar (one neuron per layer $\rightarrow$ one activation per layer).
Tipically, we are able to calculate easily the "error" term of the last layer ($\delta^L$), but we don't know how to calculate it for the previous layers so we make use of backpropagation:
$$\delta^l = \left((w^{l+1})^T\,\,\delta^{l+1}\right)\odot g(z^l) \,\,\,\,\,\,\,\,\xrightarrow[]{\text{in our simple NN}}\,\,\,\,\,\,\,\, \delta^l=w^{l+1} \,g(z^l) \,\,\delta^{l+1} $$
Where $g(z^l)$ represents the activation function of the neuron given the term $z^l$.
So, for any layer $l$, how is the term $\partial C/ \partial w^l$ computed?. Using the previous reasoning for the simple NN, now we know that:
$$ \begin{align}
\delta^{l} &= w^{l+1} \,g(z^{l}) \,\,\color{blue}{\delta^{l+1}}\\ \\
& = w^{l+1} \,g(z^{l}) \,\,\color{blue}{w^{l+2} \,g(z^{l+1}) \,\,\delta^{l+2}}\\ \\
&= w^{l+1}\color{blue}{w^{l+2}...w^{L}}\,\,g(z^{l})\color{blue}{g(z^{l+1})...g(z^{L})\,\,\delta^L}
\end{align}
$$
Where the blue terms are equivalent to $\delta^{l+1}$.
As we saw earlier, this term $\delta^l$ multiplied by the activation of the neuron from the previous layer $a^{l-1}$, gives us our desired $\partial C/\partial w^l$:
$$ \frac{\partial C}{\partial w^l} = \delta^{l}a^{l-1} = w^{l+1}\color{blue}{w^{l+2}...w^{L}}\,\,g(z^{l})\color{blue}{g(z^{l+1})...g(z^{L})\,\,\delta^L} \,\,a^{l-1}$$
So now we can see clearly that the updates that are made on a weight $w^l$ depend directly on the values of all the weights and activations of the subsequent layers.
This means that, for any value of $w^l$ (it could be very far from the optimum like in the situation plotted at the beggining), its component of the gradient may tend to zero if any of the weights or activations, $g(z)$ (or a combination of them) of the subsequent layers tend to zero. This undesired effect, as you said in the question, is known as vanishing gradient.
Now we understand that even if a weight presents a value of $\partial C/\partial w^l$ close to zero this doesn't mean that  we have reached a critical point for that weight. What's more, the learning of these parameters will slow down significantly because their updates are proportional to their respective component of the gradient. So they may get stuck in a value far from a minimum!
Finally note that this undesired effect may become more important as the number of subsequent layers grows.
Comment: Thanks for you deduction, but I still confused. Apart from the representation of $\partial C/\partial w^l,$ $C$ is a differential function of $w^l,$ $\partial C/\partial w^l \approx 0$ for
all $w^l$ is not the local optimum only when it is near the 'bad point' like saddle point, so could I understand that the vanishing gradient is the case that we are near 'bad point'?
Comment: but for your example the point $(...,w^L = 0)$ is a critical point of $C,$ right? Or could I understand, the vanishing gradient is relatively small compared with the current parameter value? And it contributes little on the update of parameter?
Comment: The edit on the post may help you know

vanishing gradient and gradient zero