Prove that in any category a product(if exists) is associative up to isomorphism.

Question: I have a question regarding an exercise(p.38, ex.5.9) from Aluffi's Algebra textbook.

Let C be a category with products. Find a reasonable candidate for the universal property that the product $A \times B \times C$ ought to satisfy, and prove that both $(A \times B) \times C$ and $A \times (B \times C)$ satisfy the universal property.Deduce that $(A \times B) \times C$ and $A \times (B \times C)$ are necessarily isomorphic.

$A \times B \times C$ is defined an object of category C satisfying( together with morphisms $\pi''_A:A \times B \times C \to A, \pi''_B:A \times B \times C \to B$ and $\pi''_C:A \times B \times C \to C$ the following universal property:
$\forall$ objects $X$ of the category C and morphisms from $X$ to $A(f_0),B(g_0)$ and $C(h_0)\ \exists!$ morphism $\omega$ such that
$\pi''_A\omega=f_0\\
\pi''_B\omega=g_0  \ \ (1)\\
\pi''_C\omega=h_0$
Now, I want to prove that $(A \times B) \times C$ also satisfies this property together with morphisms $\pi_A\pi'_{A \times B}, \pi_B\pi'_{A \times B}$ and $\pi'_C$.
$(A \times B) \times C$ has it's own property. $\forall$ objects $X_1$(of the category C) and morphisms $f_1:X_1 \to A \times B, h_1:X_1 \to C \ \exists! \ \phi:X_1 \to (A \times B) \times C$ such that the following diagram
$$ \begin{array}{ccccc}
A \times B & \stackrel {\pi'_{A \times B}} \leftarrow & (A \times B) \times C & \stackrel{\pi'_C}  \to & C \\
 & {}_{f_1} \nwarrow & \ \ \ \ \ \uparrow {}_{\phi} & \nearrow {}_{h_1} & \\
  & & X_1 & &
\end{array} $$
commutes.
And, finally, we have the universal property of $A \times B:
\forall X_2$ and morphisms $f_2:X_2 \to A, g_2:X_2 \to B$ we have a unique morphism $\upsilon:X_2 \to A \times B$ such that the following diagram
$$ \begin{array}{ccccc}
A & \stackrel {\pi_A} \leftarrow & A \times B & \stackrel{\pi_B}  \to & B \\
 & {}_{f_2} \nwarrow & \ \ \ \ \ \uparrow {}_{\upsilon} & \nearrow {}_{g_2} & \\
  & & X_1 & &
\end{array} $$
commutes.
From the universal properties of $A \times (B \times C)$ and $A \times B$ follows that $\forall$ objects $X$ and morphisms $f,g$ and $h$ we have a unique morphism $\psi:X \to (A \times B) \times C$ such that
$\pi_A\pi'_{A \times B}\psi = f\\\pi'_C\psi = h$.
Now, how do we prove that an analogue of $(1)$ goes for $(A \times B) \times C$ as well?
Comment: Why is $\psi\colon X\to A\times(B\times C)$ and not $\psi\colon X\to (A\times B)\times C$? And I assume $f,\ g,\ h$ have domain $X$ and codomains $A,\ B,\ C$ respectively? If I'm reading correctly you proved that $(A\times B)\times C$ is product and your question is how to prove it for $ A\times(B\times C)$? Please clarify.
Comment: I mistyped codomain of $\psi$, it is $(A \times B) \times C$. Yes, $f,g,h$ are as you say(like $f_0,g_0,h_0$). No, I couldn't finish the proof for $(A \times B) \times C$. I proved that $\forall$ objects $X$ and morphisms $f,h$ we have a unique morphism $\psi:X \to (A \times B) \times C$ such that
$\pi_A\pi'_{A \times B}\psi = f\\\pi'_C\psi = h$
I couldn′t prove that $\psi$ exists as well for any $g$ such that $\pi_B\pi_{A \times B}\psi = g$.
Answer: Theorem: $(X_1 \times X_2) \times X_3$ together with the obvious projection arrows forms a ternary product of $X_1, X_2, X_3$.
Proof Assume $[X_1 \times X_2, \pi_1, \pi_2]$ is a product of $X_1$ with $X_2$, and $[(X_1 \times X_2) \times X_3, \rho_1,  \rho_2]$ is a product of $X_1 \times X_2$ with $X_3$.
Take any object $S$ and arrows $f_i\colon S \to X_i$. By our first assumption, (a) there is a unique $u \colon S \to X_1 \times X_2$ such that $f_1 = \pi_1\circ u$, $f_2 = \pi_2\circ u$. So by our second assumption (b)  there is then a unique $v \colon S \to (X_1 \times X_2) \times X_3$ such that $u = \rho_1 \circ v$, $f_3= \rho_2\circ v$.
Therefore $f_1 = \pi_1\circ \rho_1 \circ v$, $f_2 = \pi_2 \circ \rho_1 \circ v$, $f_3 = \rho_2 \circ v$
So now consider $[(X_1 \times X_2) \times X_3, \pi_1 \circ \rho_1, \pi_2 \circ \rho_1, \rho_2]$. This, we claim, is indeed a ternary product of $X_1, X_2, X_3$. We've just proved that $S$ and arrows $f_i\colon S \to X_i$ factor through the product via the arrow $v$.  It remains to confirm $v$'s uniqueness in this new role.
Suppose we have $w \colon S \to (X_1 \times X_2) \times X_3$ where $f_1 = \pi_1 \circ \rho_1 \circ w$, $f_2 = \pi_2 \circ \rho_1 \circ w$, $f_3 = \rho_2 \circ w$. Then $\rho_1 \circ w\colon S \to X_1 \times X_2$ is such that $f_1 = \pi_1\circ (\rho_1 \circ w)$, $f_2 = \pi_2\circ   (\rho_1 \circ w).$ Hence by (a), $u = \rho_1 \circ w$. But now invoking (b), that together with $f_3 = \rho_2 \circ w$ entails $w = v$. $\quad\Box$
(That's Theorem 76 in these Notes on Basic Category Theory.)
Comment: These Notes seem to be very nicely written.
Answer: We have answer using projections to prove universal property by Peter Smith, and I'm going to show another way to prove this, although, depending how one defines products (limits), this may actually be longer to work out from scratch. But, the characterization of product I'm going to use is very useful in itself:

$P$ is product of the family $\{A_i\}_{i\in I}$ if and only if for any
  $X$ we have isomorphisms
  $$\operatorname{Hom}(X,P)\cong \prod_{i\in
 I}\operatorname{Hom}(X,A_i)$$ which are natural in $X$.

Now, to the question. It's obvious that $(A\times B)\times C$ is product of $A, B, C$ in $\mathbf{Set}$: there is an obvious bijection $(a,b,c)\mapsto ((a,b),c)$ which commutes with projections. Now in any category $\mathcal C$ we have
$$\begin{align}
\operatorname{Hom}(X,(A\times B)\times C) &\cong \operatorname{Hom}(X,A\times B)\times \operatorname{Hom}(X,C)\\
                            &\cong (\operatorname{Hom}(X,A)\times \operatorname{Hom}(X,B))\times \operatorname{Hom}(X,C)\\
                            &\cong \operatorname{Hom}(X,A)\times \operatorname{Hom}(X,B)\times \operatorname{Hom}(X,C)
\end{align}$$ This is obviously natural in $X$, thus, we can conclude that $(A\times B)\times C$ is product of objects $A, B, C$.
Answer: I've finally realized it works the same way for any $g$ as it works for $f$ That is, we get from the universal property of $(A \times B) \times C$ that $\forall$ morphisms $f_1: X \to A \times B$ there is a unique morphism $\phi: X \to (A \times B) \times C$ such that
$\pi'_{A \times B}\phi = f_1$
But it follows that for all morphisms $\pi_Af_1 = f, \pi_Bf_1 = g$
$$\pi_A\pi'_{A \times B}\phi = \pi_Af_1\\
\pi_B\pi'_{A \times B}\phi =  \pi_Bf_1$$
The fact that any morphisms of $X \to A$ and $X \to B$ can be decomposed as $\pi_Af_1$ and $\pi_Bf_1$ respectively for some $f_1:X \to A \times B$ follows from universal property of the product $A \times B$. Can say that the initial question is resolved, all that remains is to do the same for $A \times (B \times C)$(which I imagine to be analogous ) and deduce they are isomorphic.


this.setState not updating the state unless I pass the whole object to it

Question: I have a component where I have a state with some properties. The constructor looks like this:
constructor (props, context) {
  super(props, context)

  this.state = {
    property1: '',
    property2: '',
    property3: '',
  }
}

Say I want to update property1, what I would usually do, is something like this:
this.setState({property1: 'my new property1'})

This however, is not working, the value of property1 will not change. For me to update any of the states, I have to do the following:
let obj = this.state
obj.property1 = 'my new property1'
this.setState(obj)

I know for a fact that the previous way used to work without a problem. Has something changed? Are there certain circumstances which I can't simply set each state property?
Comment: Can you please define "not working"?
Comment: Can you please show where in your code you are calling `setState`? The entire function and how that function is called
Comment: `property1` doesn't update. It continues as an empty string.
Comment: As @MatthewHerbst mentioned, the full code would need to be looked at. Your problem with `setState` is very atypical.
Comment: This is this.setState({property1: 'my new property1'}) a truly valid one. There is no way that it don't work
Answer: setState works asynchronously.  If you call setState and then immediately check the value, it will not have updated yet.
this.setState({property1: 'my new property1'});
console.log(this.state.property1); //old value

if you want to wait for the new value, you can use the callback parameter in setState
this.setState({property1: 'my new property1'}, () => {
    console.log(this.state.property1);
});

The reason why you see the state updating when you use the whole object is because you are mutating the state by editing it, which is bad.
Comment: turns out I was having some issues due to having many `this.setState()` being called at once, so it seems that when I thought it was working, it was because I was mutating the whole object and NOT `this.setState`. I will have to rethink the code for this component, but at least I know what was going on.


Access protected var of superclass in php

Question: short question to a little php problem.
class topclass {
protected $test;
//....
}

class childclass extends topclass {`
public static function accessVariable(){

//HOW CAN I ACCESS THE $Test VARIABLE OF THE SUPERCLASS HERE?

}...

Can somebody help me?
Thx very much in advance
Answer: Use self::$test or parent::$test for static function and $this->test for regular function. Protected variables are available within the scope of the extending classes, private variable are not.
The difference between using self::$test and parent::$test is that if you have overridden $test in the child class you would get the overridden value when using self::$test.
Of course if you wish to access the property statically, you need to declare it as static (i.e. protected static $test).
Comment: `self::test` statement assumes `test` is a class constant


Excel Cannot Close Word When Stepping Through Code (F8)

Question: When running the following code from Excel, it does what I expect it to do:
It opens Word, raises an error which gets handled via CleanError and closes Word via CleanExit.
Sub testWord()

    ' VBE > Tools > References > Microsoft Word 1?.0 Object Library
    Dim wdApp As New Word.Application
    ' 'Open' Word.
    wdApp.Visible = False
    ' To prevent Word from staying open if an error occurs.
    On Error GoTo CleanError

    ' Raise an error.
    Err.Raise 13

CleanExit:
    ' Close Word.
    wdApp.Quit

Exit Sub

CleanError:
    MsgBox "Run-time Error '" & Err.Number & "': " & Err.Description, vbCritical
    Resume CleanExit

End Sub

When stepping through the code using F8, Excel hangs (is still running the code) i.e. doesn't close Word after wdApp.Quit. When closing Word via Task Manager, the line Exit Sub is highlighted and stepping further is 'allowed' again.
Am I doing something wrong here?
Comment: Is that the complete Code?
Comment: @Yes, that is the complete code. What ever you put instead of `Error.Raise 13` to raise an error should be irrelevant. It only hangs when stepping through.
Comment: Hmm, code is working for me in both ways. I can step through with F8 it and I can run it with F5. Sorry, no idea, why it is not working at your side. Maybe you should change `wdApp.Visible = False` to `wdApp.Visible = True` just to see what is going on in Word.
Comment: Do you have a Watch set on wdApp in the Watches window?  That may be the problem (at least, I saw that behaviour only with a Watch set...)
Answer: I'm guessing you have a Watch set on wdApp in the Watches window, or something along those lines.
I only see that behavior with a Watch set on wdApp.
Comment: I didn't see that one coming. Mystery solved. I'm glad the code is correct. Removing the watch or reopening the workbook solve the issue.
Comment: I thought at first it was the "As New", which is sometimes a problem.  Took a bit of trial and error to find the Watch issue.


png image blurry when loaded onto texture

Question: I have created a png image in photoshop with transparencies that I have loaded into and OpenGL program. I have binded it to a texture and in the program the picture looks blurry and I'm not sure why.
alt text http://img685.imageshack.us/img685/9130/upload2.png
alt text http://img695.imageshack.us/img695/2424/upload1e.png
Loading Code
// Texture loading object
nv::Image title;

// Return true on success
if(title.loadImageFromFile("test.png"))
{
    glGenTextures(1, &titleTex);
    glBindTexture(GL_TEXTURE_2D, titleTex);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    glTexImage2D(GL_TEXTURE_2D, 0, title.getInternalFormat(), title.getWidth(), title.getHeight(), 0, title.getFormat(), title.getType(), title.getLevel(0));
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
}

else
    MessageBox(NULL, "Failed to load texture", "End of the world", MB_OK | MB_ICONINFORMATION);

Display Code
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, titleTex);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTranslatef(-800, 0, 0.0);
glColor3f(1,1,1);

glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex2f(0,0);
    glTexCoord2f(0.0, 1.0); glVertex2f(0,600);
    glTexCoord2f(1.0, 1.0); glVertex2f(1600,600);
    glTexCoord2f(1.0, 0.0); glVertex2f(1600,0);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);

EDIT: I don't think i'm stretching each pixel should be two in the co-ordinate system
int width=800, height=600;
int left = -400-(width-400);
int right = 400+(width-400);
int top = 400+(height-400);
int bottom = -400-(height-400);
gluOrtho2D(left,right,bottom,top);

Comment: I should check opengl definitions, but as far as I know you cannot guarantee it won't be resized with this code. My best bet is the final render phase will assume that your image is 'a little' too small for the target and it will try to resize based on the 1:1 and 1:4 mipmap.
Comment: What's the size (# pixels high & wide) of the texture in the PNG file?
Comment: It looks like it might be something to do with the scaling of the image.  Does resizing it in the game window change the quality?
Comment: every thing is the same size when the window is resized, because of my gluOrtho2D
Answer: OpenGL will (normally) require that the texture itself have a size that's a power of 2, so what's (probably) happening is that your texture is being scaled to a size where the dimensions are a power of 2, then it's being scaled back to the original size -- in the process of being scaled twice, you're losing some quality.
You apparently just want to display your bitmap without any scaling, without wrapping it to the surface of another object, or anything like that (i.e., any of the things textures are intended for). That being the case, I'd just display it as a bitmap, not a texture (e.g. see glRasterPos2i and glBitmap).
Comment: I'd be interested to know _which_ implementation does this. As far as I know, this is not compliant with GL. Either you support NPOT textures, and you're supposed to load them just fine, or you don't, and you're supposed to fail the glTexImage call.
Also, glBitmap has this nasty side-effect of transferring the data on each call by default. Make sure to use a Buffer Object to avoid it (GL_PIXEL_UNPACK_BUFFER).
Comment: @Bahbar: note that not everything involved is part of OpenGL proper -- his `nv::Image::LoadImageFromFile` is entirely separate -- and my guess is that it's what's doing the initial conversion to a POT size (either initially, or in a conversion when you take what it loaded and use it as a texture).
Comment: It could also just be the video drivers. I've been working on an OpenGL powered 2D game and many ATI GPU's don't properly support NPOT textures. They appear fuzzy just as they do in the screenshot even though they're not being scaled.

Basically, you have two options. Make the PNG POT dimensions or get a different GPU.
Answer: Why would you want to use mipmapping and anisotropic filtering for a static image on your start screen in the first place? It looks unlikely the image will be rotated (what anisotropic filtering is for) or has to be resized many times really fast (what mipmapping is for).
If the texture is being stretched: try using GL_NEAREST for your GL_MAG_FILTER, in this case it could give better results (GL_LINEAR is more accurate, but has a nature of blurring).
If the texture is minimized: same thing, try using GL_NEAREST_MIPMAP_NEAREST, or even better, try using no mipmaps and GL_NEAREST (or GL_LINEAR, whichever gives you the best result).
Comment: Can't get any of these to make it any better :(
Comment: You have absolutely no difference? For example, because of the blending I would suspect the white borders still to be 'ugly', but the text inside the boxes should be sharp. If none of this changes the image, I was probably wrong and your texture is not being resized. Did you try rendering it to a screen that is just a little too large (e.g. screen of (824x618)) and using GL_NEAREST for MAG?
Comment: It was different but didn't look better
Answer: I'd suggest that you make the png have a resolution in a power of 2's. Ie 1024x512 and place the part you want to drawn in the upper left corner of it still in the resolution for the screen. Then rescale the texcoords to be 800.0/1024.0 and 600.0/512.0 to get the right part from the texture. I belive that what is going on is glTexImage2D can sometime handle width and height that are not a power of 2 but can then scale the input image thus filter it.
An example of handling this can be viewed here (a iPhone - OpenGLES - project that grabs a screen part non-power of 2 and draws that into a 512x512 texture and rescales the GL_TEXTURE matrix, line 123, instead of doing a manual rescale of the texcoords)
Here is another post mentioning this method.
Answer: Here is an hypothesis:
Your window is 800x600 (and maybe your framebuffer too), but your client area is not, because of the window decoration on the sides.
So your frame-buffer gets resized when being blitted to the client area of your window. Can you check your window creation code ?
Comment: This is my create window code, which has the same variables CreateGLWindow("Star Racer",width,height)
Comment: Actually, that's incorrect thinking. When you create a window, the client area is created to the size that you need. The frame is added afterward by Windows.

So if he says that he created his window at 800x600, his client area is indeed 800x600. I don't know about Linux but I know that at least MacOS X and Windows are smart enough to manage that properly.

To prove the point, a quick copy/paste into any image editor shows that the image he posted is 816x638. That would be 800x600 + the window frame.
Answer:
Beware of exact size of the client area of your window. Double check it is what you expect it to be
Beware of pixel alignment rules. You might need to add 0.5 to your x/y coordinates to hit the pixel centers. Description for DirectX can be found here - OpenGL rules may be different, though.


Convolution of two functions doesn't fit my data as I thought it would

Question: I have simulated a Gaussian curve in 50 bins of data. I have then repeated this many times, drawing the amplitude of the Gaussian from a log-normal distribution. Here are a 10 realizations:
(IMAGE 1)

I actually create 10,000 realizations, I take the sum of the 50 bins (that you see above) for each of the 10,000 realizations and then I plot a histogram of the result:
(IMAGE 2)

This histogram is a log-normal distribution, which I think makes sense because I am drawing the amplitudes (of my Gaussian curves) from a log-normal distribution.
So when I plot a log-normal curve over the top, it fits well:
(IMAGE 3)

Now to make the situation more realistic, I am adding lots of noise to my Gaussian signals, so now they look more like:
(IMAGE 4)

Now when I plot my histogram, the log-normal curve no longer matches the data. This is because of the noise that I have added of course:
(IMAGE 5)

To find the function that best describes my histogram now, I believe that I have to take the convolution of my log-normal curve and a Gaussian curve that describes the distribution of the noise that I added.
Here is the histogram of the noise I added along with a Gaussian curve fitting the data (mean -0.07 standard deviation 42.89):
(IMAGE 6)

So I believe that to describe the shape of the histogram in IMAGE 5, I need to convolve the dashed line in IMAGE 5 with the dashed line in IMAGE 6:
(IMAGE 7)

So I try to do that convolution and I get the blue curve:
(IMAGE 8)

So then to check if this convolution does indeed describe my histogram from IMAGE 5 I plot everything together:
(IMAGE 9)

As you can see the convolution still doesn't quite fit the histogram. The curve is a little too wide.
Can anyone see what I am doing wrong here? Am I correct to think that to get the distribution of the sum of my 50 noisy data bins I need to convolve the log-normal distribution in IMAGE 2 with the Gaussian distribution in IMAGE 6?
Thank you!
Comment: I'm not sure I fully understand this either, but you'd get the convolution if you add Gaussian noise to lognormally distributed data, i.e., the amplitudes. But you're saying that you add the noise to the "Gaussian signals" rather than directly to the lognormally distributed amplitudes, if I understand things correctly (of which I'm not sure).
Comment: For your image 1 it might be better to plot dots along with the curves, since you are talking about bins. The use of curves is confusing (although defensible because it makes it easier to compare them) and make it appear as if they are functions of a continuous variable instead of discrete variable.
Answer: There is a discrepancy between the lognormal from image 3 and image 5. Check the peak height and position. (Not just the histogram, but also the red curve)
It seems like you have refitted a log-normal distribution to the data with noise and used that log-normal distribution for the convolution, instead of the original log-normal distribution.
Answer: As Sextus Empiricus already pointed out, the discrepancy is quite likely due to the wrong density for the amplitudes. I don't believe this stems from refitting to the data with noise, but suspect it could come from a minor typo or glitch in the parameter $\sigma$ of the lognormal.
The figure below illustrates how the outcome of the convolution chances when you replace the true distribution for the amplitudes by one with a larger standard deviation parameter. Here: if lognorm(4.8, 0.4) is used instead of lognorm(4.8, 0.2):


Connect of exchange vector field .

Question: $D$ is Levi-Civita connect. $U,V,W,Z$ are vector field. $S$ is (0,4) tensor.
And $D^2_{X,Y}S=D_XD_YS-D_{D_XY}S$.
Are there any easy way to compute the below equation ?
If I unfold it, it is complex to compute ,so I hope there is some easy way.
Answer: Assume that $S$ is
$(0,1)$-tensor (Other case are similar). If $E_i$ is coordinate
vector field, first we will enumerate definitions and notations :
$$ D_k S_m:= (D_kS)_m $$
Here by definition of covariant derivative, $$ (D_kS)_m:=
E_k (S_m) - S_n\Gamma_{km}^n $$
So
$$ D_iD_jS_k
 =
E_i( D_jS_k)- D_jS_m \Gamma_{ik}^m  $$
(Here $T:=D_jS$ is $(0,1)$-tensor That is above is $D_iT_k$)
$$ =
E_iE_j(S_k)- E_i (S_m\Gamma_{jk}^m)- E_j(S_m) \Gamma_{ik}^m+
S_n\Gamma_{jm}^n  \Gamma_{ik}^m   $$
Hence by using symmetry of index $i, \ j$ and direct computation,
$$ D_iD_jS_k - D_jD_i S_k
 = -S_m (E_i\Gamma_{jk}^m-E_j\Gamma_{ik}^m) + S_n(
 \Gamma_{jm}^n  \Gamma_{ik}^m -\Gamma_{im}^n  \Gamma_{jk}^m
) $$
$$ = S_m (-R_{ijk}^m) $$
And note that $ R_{ijk}^m=(D_i D_j E_k-D_jD_i E_k)^m $ Hence we
complete the proof
Comment: How to get $D_iD_jS_k
 =
E_i( D_jS_k)- D_jS_m \Gamma_{ik}^m=
E_iE_j(S_k)- E_i (S_m\Gamma_{jk}^m)- E_j(S_m) \Gamma_{ik}^m+
S_n\Gamma_{jm}^n  \Gamma_{ik}^m$  ?
Comment: I will add more explanation

Connect of exchange vector field .


Load class from exploded module using custom classloader

Question: I have been playing around with simple custom classloaders in Java, and so far everything works as expected for non-module related classes. However, I can't seem to find any way to load a class from a module using my classloader, even though the module-related find*() methods have been overloaded. What I'm attempting to do is load a class from a module "HelloModularWorld" and run it's main. However, when I specify the directory where the package would be, it's loaded "normally" and reports as being in the unnamed module. What am I missing?
The classloader is just loading classes from elsewhere on the filesystem, nothing particularly special.
Classloader implementation:
package arch.classloaders;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class ModifiableClassLoader extends ClassLoader {
    private File searchPath;
    //...other ctors...
    public ModifiableClassLoader(File path, String name, ClassLoader parent) {
        super(name, parent); //Delegate to parent classloader as required.
        if (!path.isDirectory()) throw new IllegalArgumentException("Path must be a directory");
        searchPath = path;
    }

    public void setPath(File newPath) {...}

    public File getPath() {...}

    //Responsible for actual loading
    public Class<?> findClass(String binName) throws ClassNotFoundException {
        File classfile = new File(searchPath.getPath() + File.separator
                + binName.replace('.', File.separatorChar) + ".class");
        byte[] buf;
        FileInputStream fis;
        try {
            fis = new FileInputStream(classfile);
            buf = fis.readAllBytes();
            fis.close();
        } catch (IOException e) {
            throw new ClassNotFoundException("Error in defining " + binName + " in " + searchPath.getPath(),e);
        }
        return defineClass(binName, buf, 0, buf.length);
    }

    //Modules are expected to be in a folder with the same name as the module.
    //e.g. module hellomodularworld is expected to be in a folder
    //<SEARCHPATH>/<MODNAME>/
    //NOTE: Module-overloaded methods return null rather than throw when class isn't found.
    public Class<?> findClass(String modName, String binName) {
        if (null == modName) {
            try {
                return findClass(binName);
            } catch (ClassNotFoundException e) {
                return null;
            }
        }
        File classfile = new File(searchPath.getPath() + File.separator
                + modName + File.separator
                + binName.replace('.', File.separatorChar) + ".class");
        byte[] buf;
        FileInputStream fis;
        try {
            fis = new FileInputStream(classfile);
            buf = fis.readAllBytes();
            fis.close();
        } catch (IOException e) {
            return null;
        }
        return defineClass(binName, buf, 0, buf.length);
    }

    //Non-module
    public URL findResource(String resPath) {...}

    //Module version
    public URL findResource(String modName, String resPath) throws IOException {...}

    //Enumeration version; does nothing.
    public java.util.Enumeration<URL> findResources(String resPath) {...}
}

Test code:
public class Test {
    public static void main(String[] args) {
        ModifiableClassLoader mcl = new ModifiableClassLoader(
                new File("C:\\Users\\archd\\Desktop\\"));
        try {
            Class<?> clazz = mcl.loadClass(
                    "hellomodularworld/com.archdukeliamus.hellomodularworld.HelloWorld"
            );
            java.lang.reflect.Method mth = clazz.getMethod("main", String[].class);
            mth.invoke(null,new Object[] {null});
            System.out.println(clazz.getModule().getName());
        } catch (...) {
                //omitted
        }
}

On line Class<?> clazz = mcl.loadClass("hellomodularworld/com.archdukeliamus.hellomodularworld.HelloWorld"); I have tried:

"hellomodularworld/com.archdukeliamus.hellomodularworld.HelloWorld" -
Does not work. ClassNotFoundException.
"hellomodularworld.com.archdukeliamus.hellomodularworld.HelloWorld" -
NoClassDefFoundException.
com/archdukeliamus/hellomodularworld/HelloWorld (wrong name: hellomodularworld/com/archdukeliamus/hellomodularworld/HelloWorld)
"com.archdukeliamus.hellomodularworld.HelloWorld" - Works as expected (with classloading folder changed appropriately) but uses the unnamed module.

EDIT: module-info.java
module hellomodularworld {

}

The test class is not in any modules.
(I'm not entirely sure why this would matter, I should get an exception to the effect of "this package isn't exported" which I am not getting.)
EDIT 2:
Modified module to include exports com.archdukeliamus.hellomodularworld;. No changes in results.
Comment: The reason I was asking for that is *If a request is made to load a type whose package is not defined in any known module then the module system will attempt to load it from the **class path**. If this succeeds then the type is considered to be a member of a special module known as the unnamed module, so as to ensure that every type is associated with some module*
Comment: While that does clarify that a module doesn't seem to be allowed in the binary name, it still begs the question of how the classloader gets the module to begin with. Nothing in the ClassLoader page seems to specify this.
Comment: The code fragment is just a class loader that loads classes that are deployed as modules. Where is your code to create the configuration and map the module to the class loader, you must be calling ModuleLayer's defineModule somewhere, right?
Comment: As a side note, why are you going that `FileInputStream` detour? You can simply use `byte[] buf = Files.readAllBytes(searchPath.toPath().resolve(binName.replace('.', File.separatorChar) + ".class"));`. Since Java 7.
Comment: I haven't really used Java 9 modules yet, but I'm pretty sure you can't pass that kind of string to loadClass. See [here](https://docs.oracle.com/javase/9/docs/api/java/lang/ClassLoader.html#loadClass-java.lang.String-).
Comment: Could you update the question with the module declaration of the `hellomodularworld` and of the module from which the code above is shared?
Comment: @nullpointer Updated question appropriately.
Comment: I didn't yet see about ModuleLayer but it seems where I need to be going. Also I did the "detour" since I haven't yet made much use of NIO.
Answer: Module loading is a separate process from class loading. To load a module at runtime, you will need to create a new module layer using the ModuleFinder class, provided with a Path from a FileSystem (in this case the disk). You will then need to create a Configuration that you can use to resolve your modules. To ensure the sanity of the module loading process, you will need to derive the boot configuration. Then you will need to create your configuration, including a ModuleFinder to tell where modules are to be found, and a set of modules to be resolved. Then you instantiate the classloader you wish to use to load those modules' classes, and pass this to the define modules method. Finally you instantiate your class.
New test:
package arch.classloaders;

import java.lang.module.*;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.io.*;
import java.nio.*;
import java.nio.file.*;
import java.util.*;

public class Test2 {
    public static void main(String[] args) {
        //Get paths to module, and instantiate a ModuleFinder.
        Path pth = FileSystems.getDefault().getPath("C:\\Users\\archd\\Desktop");
        ModuleFinder mf = ModuleFinder.of(pth);
        //Create a new Configuration for a new module layer deriving from the boot configuration, and resolving
        //the "hellomodularworld" module.
        Configuration cfg = ModuleLayer.boot().configuration().resolve(mf,ModuleFinder.of(),Set.of("hellomodularworld"));
        //Create classloader
        ModifiableClassLoader mcl = new ModifiableClassLoader(
                new File("C:\\Users\\archd\\Desktop\\"));
        //make the module layer, using the configuration and classloader.
        ModuleLayer ml = ModuleLayer.boot().defineModulesWithOneLoader(cfg,mcl);
        //Show the configuration.
        System.out.println(ml.configuration()); //prints "hellomodularworld"
        try {
            //load and run class
            Class<?> clazz = ml.findLoader("hellomodularworld").loadClass(
                    "com.archdukeliamus.hellomodularworld.HelloWorld"
            );
            java.lang.reflect.Method mth = clazz.getMethod("main", String[].class);
            mth.invoke(null,new Object[] {null});
            //show the module this class is part of and list packages
            System.out.println(clazz.getModule()); //prints "module hellomodularworld"
            for (String pkgn : clazz.getModule().getPackages()) {
                System.out.println(pkgn); //prints "com.archdukeliamus.hellomodularworld"
            }
        } catch (ClassNotFoundException e) {
            ...omitted...
        }
    }
}

Interestingly this still doesn't call the overloaded module-based findClass() methods, although it seems to work.
Comment: There is nothing in the API to restrict modules to the file system, they can of course be loaded from anywhere. In any case, the code fragment in this answer doesn't mention anything about dynamic configuration and module layers - these are the concepts that are needed before using a custom class loader to load classes from modules.
Comment: I see. The JEPs weren't entirely enlightening on how custom classloaders interacted with the module system. Is there a link where the module layer is properly explained?
Comment: In the javadoc search, look for "ModuleLayer". Note that this is a very advanced topic so take the time to get familiar with the concepts.


Will the set be open or closed?

Question:
The doubt I have is that as (0,0) being included in the set it can be considered as a boundry point. Hence I think it might be neither open nor closed
Comment: @janmarqz: $\Bbb N$ isn't being topologized here; it's just being used to define $A$. $A$ is a subset of $\Bbb R^2$, presumably with the usual topology.
Comment: @BrianM.Scott...  give Gudu Gudu a chance
Comment: What is the topology you took on $\mathbb N$?
Comment: The fact that $A$ contains one of its boundary points does show that $A$ is not open, but you still have to explain why it's not closed.
Comment: It is closed. Topological argument: take any point in the complement and see if you can find an open ball around that point that remains in the complement. Analytic argument: show that it contains all its limit points (in essence, this is the same as showing that $(0,0)$ is the only aggregation point).
Comment: @janmarqz: I am: that's why I merely pointed out what still needs to be done instead of doing it myself.
Comment: @BrianM.Scott you are a clever savvy man
Answer: I presume that we are dealing with the standard $\mathbb{R}^2$. Otherwise any of the answers can be true depending on the overlying space.
The answer is: neither open nor closed.
Not open: we will show that by showing that the complement is not closed. Indeed, consider the sequence $v_n=(1+1/n,1)$ which is fully contained in $\mathbb{R}^2\backslash A$ but converges to $(1,1)$ which is in $A$ (corresponding to $n=m=1$). Thus $\mathbb{R}^2\backslash A$ is not closed.
Not closed: note that for a fixed $m\in\mathbb{N}$ we have the sequence $v_n=(1/n,1/m)$ which converges to $(0,1/m)$ which is not in $A$.
Side note: if $A$ were additionally to include all points of the form $(1/n,0)$, $(0,1/m)$ then it would be closed.

Will the set be open or closed?


how to determine the coefficient to simplify this expression

Question: As I am not good at math, I would like to construct an expression having the form like:
$$ a^n (\sum_{i=0}^{n-1} \lambda_i \cdot b^i ) +(\sum_{i=0}^{n-1} \lambda_i \cdot a^i)\cdot b^n + \lambda_n\cdot a^n \cdot b^n, \quad (1)$$
where $\lambda_i$ is a free parameter to be determined.
My goal is to determine $\lambda_i$ such that Express (1) has a neat and simple form. $\lambda_i \ (i=1,...,n)$ can be any real positive number, but they should satsify $\lambda_i < \lambda_{i+1}$ for $i=0,...,n-1$. For example, when $n=3$,
$$ 1a^3 + 6a^3b+15a^3b^2+20a^3b^3+15a^2b^3+6ab^3+1b^3.$$
Currently, I choose $\lambda_{i} = {2n\choose{i}}$ which is a bionormal coefficient. But it seems that Express (1) is hard to be simplied in this case.
My question is that can anyone give me a hand to find a good coefficient $\lambda_i$ to simply Express (1) in a neat form. Thanks!
Comment: You can get parentheses (or any other paired delimiters like brackets, braces, ...) of the appropriate size by preceding them with `\left` and ˚\right`.
Comment: Is there a reason why the $a^nb^n$ term is written separately instead of extending the second sum to $n$?
Comment: What would you consider to be a "neat form"?
Comment: For example, $(a+b)^3$ is a neat form of $a^3+3a^2b+3ab^2+b^3$.
Comment: Well, there's no way you're going to get a form *that* neat. What you have is not going to be a power of a binomial.
Answer: You could take $\lambda_i={n\choose i}n^i$. Then your expression would be $$(a+abn)^n+(b+abn)^n-(abn)^n$$
Answer: There is a nice formula for the partial sum of a geometric series:
$\sum_{i=0}^{n-1}{x^i} = \frac{1 - x^n}{1-x}$
So your expression will simplify if you let $\lambda_i = c^i$ for some choice of $c$.  Since you want the $\lambda_i$'s to be increasing, you could choose $\lambda_i = 2^i$.  Then you get:
$$ a^n (\sum_{i=0}^{n-1} \lambda_i \cdot b^i ) +(\sum_{i=0}^{n-1} \lambda_i \cdot a^i)\cdot b^n + \lambda_n\cdot a^n \cdot b^n$$
$$= a^n (\sum_{i=0}^{n-1} 2^i \cdot b^i ) +(\sum_{i=0}^{n-1} 2^i \cdot a^i)\cdot b^n + 2^n\cdot a^n \cdot b^n$$
$$=a^n\frac{ 1-(2b)^n}{1-2b} +b^n\frac{ 1-(2a)^n}{1-2a}  + (2ab)^n.$$
That seems relatively simple to me - at least, it's closed form.


Identificando a chave estrangeira no Entity Framework CodeFirst

Question: O Entity Framework identifica a chave estrangeira através de propriedades que possuem Id como parte do nome ou pelo Data Annotation [Key]. Minha dúvida é, como ele identifica as chaves estrangeiras e faz o relacionamento entre outra tabela?
public class Predio {
    public int Id { get; set; }
    public string Nome { get; set; }
    public Endereco Endereco { get; set; }
}

public class Construtora {
    public Construtora()
    {
        this.Predios = new List<Predio>();
    }
    public int Id { get; set; }
    public string Nome { get; set; }
    public string RegistroCivil { get; set; }
    public List<Predio> Predios { get; set; }
}

[ComplexType()]
public class Endereco {
    public string Logradouro { get; set; }
    public string Numero { get; set; }
    public string Bairro { get; set; }
    public string Cidade { get; set; }
    public string CEP { get; set; }
}

public class Contexto : DbContext
{
    public DbSet<Predio> Predios { get; set; }
    public DbSet<Construtora> Construtoras { get; set; }
}

Answer: Objetivamente falando, através de uma classe chamada ForeignKeyPropertyDiscoveryConvention. O funcionamento é explicado aqui.
Grosso modo, a maneira que o Entity Framework realiza a descoberta da chave estrangeira é da seguinte forma (considere que várias dessas etapas usam Reflection):

É verificado no Model se há propriedades de navegação compatíveis. Por exemplo:
public virtual EntidadeEstrangeira EntidadeEstrangeira { get; set; }

Se sim, é verificado no Model se há alguma propriedade candidata a ser o componente de informação dessa chave estrangeira. Por exemplo:
public int EntidadeEstrangeiraId { get; set; }

2.1. Pode acontecer desse campo não ser localizado. Se não for, o Entity Framework tenta procurar por alguma propriedade que esteja marcada com o ForeignKeyAttribute. Este Attribute pode decorar tanto uma propriedade de navegação quanto uma propriedade de dados. Por exemplo:
[ForeignKey("EntidadeEstrangeira")]
public int OutroNomeDeEntidadeEstrangeiraQualquerId { get; set; }

ou ainda:
[ForeignKey("EntidadeEstrangeiraId")]
public virtual EntidadeEstrageira UmNomeQualquerDePropriedadeDeNavegacao { get; set; }

Ainda, é verificado se existe alguma anotação de Fluent API no contexto. Por lá é possível também configurar chaves estrangeiras.

Não vou entrar em pormenores do funcionamento porque é bastante coisa, mas em linhas gerais, é isso.

Seu Caso
Vou analisar as entidades uma a uma.
public class Predio {
    public int Id { get; set; }
    public string Nome { get; set; }
    public Endereco Endereco { get; set; }
}

Este é um caso clássico que cai na primeira etapa. Normalmente o padrão é virtual para propriedades de navegação (expliquei isso aqui).
Como você não definiu a chave estrangeira, se o objeto não fosse um tipo complexo, o Entity Framework definiria uma chave estrangeira pra você na tabela correspondente ao Model Predio. Se não me engano, ficaria Endereco_Id.
No entanto, você definiu Endereco como um objeto complexo. Como a relação é de 1 para 1, no fundo é como se você estivesse definindo uma extensão de Predio. Repare como ficou o esquema ao gerar a Migration:
    CreateTable(
            "dbo.Predios",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Nome = c.String(),
                    Endereco_Logradouro = c.String(),
                    Endereco_Numero = c.String(),
                    Endereco_Bairro = c.String(),
                    Endereco_Cidade = c.String(),
                    Endereco_CEP = c.String(),
                    Construtora_Id = c.Int(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Construtoras", t => t.Construtora_Id)
            .Index(t => t.Construtora_Id);

Observe também que você não definiu uma propriedade para Construtora_Id, mas realizando a leitura do Model Construtora, que possui uma relação de 1 para N com Predio, o Entity Framework concluiu que seria necessário uma chave estrangeira aí, e a definiu como Construtora_Id (regras 1 e 2, acima).
Por fim, este construtor não tem utilidade:
public Construtora()
{
    this.Predios = new List<Predio>();
}

Predios é definida na carga de dados de Construtora automaticamente. Não é preciso inicializá-la.
Comment: Cigano, no exemplo que postei eu não defini como virtual e nem decorei nada na entidade Construtora com o Data Annotation [ForeignKey()], foi utilizado Fluent API neste contexto?
Comment: @KellySoares Então, algumas construções ali estão um pouco problemáticas. Se for do seu desejo, posso comentá-las uma a uma.
Comment: Eu adoraria, porque a princípio, eu achei que fosse por causa do construtor da classe Construtora que está passando uma referência a entidade Prédios, achei que isso é que estava fazendo o relacionamento entre as entidades.
Comment: @KellySoares Atualizei a resposta.


Use check box in view and get its value in model

Question: can I use it like this in View?
<%= Html.CheckBoxFor(user => user.Role, "Administrator")%>

and then just read a value of property in model if checkbox is checked:
string role = user.Role;
(role = "Administrator")

Answer: In your model...
public class UserEditModel {
  public string UserName { get; set; }
  public string[] Role { get; set; }
}

In your view...
<%: Html.TextBoxFor(x = x.UserName)

<input type="checkbox" name="role" id="role_Administrator" value="Administrator" />
<input type="checkbox" name="role" id="role_Create_Project" value="Create Project" />

Yes, you need to write raw HTML for this scenario. This will bind correctly with the MVC defaults. The default CheckBoxFox really only works well with True/False bindings. Something like
<%: Html.CheckBoxFor(x => x.AcceptLicenseTerms) %>

or
<%: Html.CheckBoxFor(x => x.RememberMe) %>

Comment: sorry i am a bit confused what i all have here to do. thanks for introduction:)!
Comment: I think this particular scenario (your code) is better fit for a radio-button group type of choice selection.
Comment: I want to make dropdownlist with ability to add new role, thatsway i need to use check box i think it makes more sense or??
Answer: Checkboxes are read as "true" or "false", that is they are a boolean type. What you can do is have a viewmodel with a IsAdmin property and then check in your controller if the property is true.
// View
<%: Html.CheckBoxFor(model => model.IsAdmin)

// Controller
If(viewModel.IsAdmin)
{
    doYourStuff();
}


Bing Search API with Ajax

Question: Trying to make fast queries to the Bing search API (azure marketplace).
The goal is to call the bing api and return the search results to the page. However, I need to make it really fast, possibly as fast as the end-user search on bing.com. Here is my code:

The password is filled with my account key. User should be blank. The above code does not return a result - not sure why. I included Ajax.
I'm aware of same-origin policy, but I assume for the API they set appropriate X-headers.
Answer: It looks to be problem with your ajax call, try doing it as follows:
$.ajax({
        url: "your-url",
        type: 'GET',
        dataType: 'jsonp',
        beforeSend: function (xhr) {
            xhr.setRequestHeader('Authorization', 'Basic bHVpZ2lAZ21haWwuY29tOmFiYzEyMzQ1');
         },
        success: function(data){
                  $("id01").html(data);
         },
        error: function(jqXHR, textStatus, errorThrown) {
                 console.log(textStatus, errorThrown);
         }

    })

Where second field inside xhr.setRequestHeader() is calculated as follows:

The username and password are combined into a string separated by a
colon, e.g.: username:password
The resulting string is encoded using the RFC2045-MIME variant of Base64, except not limited to 76 char/line.
The authorization method and a space i.e. "Basic " is then put before the encoded string.

For example, if the user agent uses Aladdin as the username and OpenSesame as the password then the field is formed as follows:
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l

In your case you may need to use your api key as username as well as password both.

Bing Search API with Ajax


Change 2 values with Jquery text replace

Question: I want to change two prices on the page rather than one. so change if £65 or £35.
How do I add a either/or to this function?
$('#shipping-method > table > tbody > tr:nth-child(2) > td.price > label')

 .filter(function () {

return $.trim(this.innerHTML) == "£65.00";

}).text('Quote Price');

Comment: Use [logical operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators)? I'm not sure I understand the question.
Comment: `return this.innerHTML.match(/£(65|35).00/);` is a solution.
Comment: I want to have something like        == "£65.00 or £35.00";
Answer: Try using RegExp.prototype.test()
$("#shipping-method > table > tbody > tr:nth-child(2) > td.price > label")
.filter(function () {
  return /£65\.00|£35\.00/.test(this.innerHTML);
}).text("Quote Price");

Answer: You can use simple or || operator
$('#shipping-method > table > tbody > tr:nth-child(2) > td.price > label')

 .filter(function () {

return ($.trim(this.innerHTML) == "£65.00" || $.trim(this.innerHTML) == "£35.00");

}).text('Quote Price');


Django Image field doesn't update image

Question: I recently got a problem that my django image field in my django app doesn't update the image after pressing "Post". In admin panel everything works fine. Maybe I missed something... Let me know.
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    categories = models.ManyToManyField('Category', related_name='posts')
    image = models.ImageField(upload_to='images', default="images/None/no-img.jpg")

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})

class Category(models.Model):
    name = models.CharField(max_length=20

)

views.py
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .models import Post
from django.contrib.auth.models import User
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView

def home(request):
    content = {
        'posts': Post.objects.all()
    }

    return render(request, 'blog/home.html', content)

def blog_category(request, category):
    posts = Post.objects.filter(categories__name__contains=category).order_by('-date_posted')
    content = {
        'category': category,
        'posts': posts
    }
    return render(request, 'blog/blog_category.html', content) #<--(didn't add content block) bug found 05.11.19

def upload_pic(request):
    if request.method == 'POST':
        form = ImageUploadForm(request.POST, request.FILES)
        if form.is_valid():
            m = ExampleModel.objects.get(pk=course_id)
            m.model_pic = form.cleaned_data['image']
            m.save()
            return HttpResponse('image upload success')
    return HttpResponseForbidden('allowed only via POST')

...

class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    fields = ['title', 'content', 'categories', 'image']

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Post
    fields = ['title', 'content', 'categories', 'image']

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

I just have recently added Category and Image section in my web-application, the Category section works fine, but the image field just post default even if I'm selecting the current image. In the admin field it works well, so I decided to ask community about this annoying problem.
Answer: Make sure the form in your template has enctype set correctly:

Note that request.FILES will only contain data if the request method was POST and the  that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.

Django Image field doesn't update image


Functional Equation f(n+2023)=f(f(f(f(n))))

Question: Consider the functional equation $$ f(n+2023) = f(f(f(f(n))))$$ for all $n \in\Bbb N$.
Find all strictly increasing functions $f: \Bbb N \to \Bbb N$ satisfying above conditions.
I assume it's a linear function as it is supposed to be strictly increasing. Don't have any clue how to properly write proof for this question and solve it.
Comment: What is the source of this problem? Questions involving the current year frequently appear in contests and such.
Comment: Welcome to [math.se] SE. Take a [tour]. You'll find that simple "Here's the statement of my question, solve it for me" posts will be poorly received. What is better is for you to add context (with an [edit]): What you understand about the problem, what you've tried so far, *etc.*; something both to show you are part of the learning experience and to help us guide you to the appropriate help. You can consult [this link](https://math.meta.stackexchange.com/q/9959) for further guidance.
Comment: A strictly increasing function over the naturals do not have to be linear, e.g. $f(n) = 2^n$.
Comment: I encountered this question while going through this year's question in National Olympiad Nepal.
Answer: We claim that such $f$ doesn't exist:
Assume it exists, first notice that $f(f(f(n)))=n+2023$, as $f$ is strictly increasing. Assume $\exists k\in\mathbb{N}$, $f(k)\ge k+675$. Then inductively it's clear that for any $l\ge k$, $l\in\mathbb{N}$, we must have $f(l)\ge l+675$. In particular, $f(f(f(l)))\ge f(f(l))+675\ge f(l)+1350\ge l+2025>l+2023$, contradiction.
Therefore, $f(n)\leq n+674$, $\forall n\in\mathbb{N}$. Then $f(f(f(n)))\leq f(f(n))+674\leq f(n)+1348\leq n+2022<n+2023$, contradiction, allowing to conclude.
Comment: This might be tight as with $2022$ instead $n+674$ works. Could this be someone actualized the problem for $2023$ not realizing that for $f(n+a)$ to work (i.e. having affine solution) $a$ should be divisible by $3$ ?
Comment: I think the solution is unique iff $3|a$. The "$\mathbb{N}\to\mathbb{N}$" condition is a good restriction of the number of solutions.
Answer: Since strictly increasing implies injective, our functional equation gives
$$n+2023 = f(f(f(n)))\tag{$\star$}$$

Lemma. Let $a, b\in\mathbb N$ be such that $f(a)=f(b)+1$. Then $a = b+1$.

Proof. Of course $f(a)>f(b)$ so $a>b$. Now suppose $a>b+1$. So $f(b)+1 = f(a) > f(b+1) > f(b)$, i.e., there is a natural number between $f(b)$ and $f(b)+1$. Contradiction! $\square$
Now just notice that $f(f(f(n+1))) = n+2024 = f(f(f(n)))+1$, so
$$\begin{aligned}
&f(f(f(n+1))) = f(f(f(n)))+1\\
\implies &f(f(n+1)) = f(f(n))+1\\
\implies &f(n+1) = f(n)+1
\end{aligned}$$
for all $n\in\mathbb N$. By induction we show that $f(n) = f(0)+n$. Therefore $$f(f(f(n))) = f(0)+f(f(n)) = 2f(0)+f(n) = 3f(0)+n$$
but, in order for that to work, we should have $3f(0) = 2023$, which can't be solved over the naturals.
We conclude there can not be such a function.
Comment: I didn't understand the f(b)+1=f(a)>f(b+1)>f(b) part of the solution.
Comment: $f(b)+1 = f(a)$ because that is our hypothesis.

$f(a) > f(b+1)$ because we are supposing $a>b+1$ and our function is strictly increasing.

$f(b+1) > f(b)$ because $b+1>b$ and, again, our function is strictly increasing.

As this leads to a contradiction, our hypothesis must be false, i.e., $a$ can't be greater than $b+1$. Because $a > b$, the only possibility left is $a = b+1$.


Incorrect proof

Question: Consider the following

Result:   Let   $n   ∈  \mathbb{Z}$. Then $5n+7$ is even only if $n$ is odd.

Next, consider the following proof.
Assume first that $n$ is an odd integer. Then $n=2a+1$ for some integer $a$. Therefore, $5n+7=5(2a+1)+7=10a+5+7=10a+12=2(5a+6)$.  Since $5a+6$ is an integer, $5n+7$ is even.
Next, assume that $n$ is an even integer. Then $n=2b$ for some integer $b$. Hence $5n+7=5(2b)+7=10b+7=2(5b+3)+1$.  Since $5b+3$ is an integer, $5n+7$ is odd.

this proof is solved incorrectly but I can't figure out what is wrong with it. It is solved as if it were a bi-conditional statement and it seems to be taking the correct steps to solving one but I see nothing wrong with it. I was also  thinking that It might not be a bi-conditional statement to begin with. I'm at a loss here. Any ideas?
Comment: Why do you think this is solved incorrectly? How would you solve it differently?
Comment: The proposition uses "only if," not "if and only if," so it's not a biconditional. It claims nothing about $5n+7$ when $n$ is even.
Comment: @amd I would be more inclined to say that the statement is incomplete, not that the proof is wrong.
Comment: You have correctly proved a different result that is stronger than the one you were requested to prove. But, unfortunately, this proof doesn't show if you have understood the meaning of "only if", which probably was the purpose of the exercise.
Comment: @David I didn't say that the proof was incorrect, but that half of it is extraneous. The proposition *as stated* by the OQ isn't a biconditional, so proving "if $n$ is odd, then $5n+7$ is even" is irrelevant. It's the wrong direction. I'm with egreg—understanding the meaning of "only if" was likely part of the point. I'll cop to an error in my previous comment, though, namely that the proposition *does* make a claim w/r $n$ even; it doesn't say anything about what happens when $n$ is odd, though.
Answer: The assertion "$5n+7$ is even only if $n$ is odd" means "if $5n+7$ is even, then $n$ is odd". Instead of proving this, the easy way is to prove the contrapositive, i.e. that if $n$ is even, then $5n+7$ is odd, which is the second part of the argument you wrote.
The first part of the argument shows the reverse implication, so in the end we have an "if and only if": $5n+7$ is even if and only if $n$ is odd".

Incorrect proof


How do apriori based algorithms avoid finding spurious relationships in their rules?

Question: I'm taking a class on data mining and of course, we went over the Apriori algorithm (Fast Algorithms for Mining Association Rules).

Something that I want to know is how do apriori based algorithms avoid finding spurious relationships when generating association rules?

Is this a case of being aware of the difference between predictive vs. causal analysis? Meaning there could be some hidden third factor causing the association but as long as the relationship holds we don't care what that third factor is. Association rules don't say that A causes B, they just say that A and B happen together often.
Is it because these algorithms are often run on VERY large data sets so the probability of having an excessive number of false positives is very low. Meaning, if you only have a sample size of 50 you could get a associations that complete a low support just by change but that is unlikely to happen when you have 500,000 or 5,000,000 samples.
Answer: A short answer is that you can't avoid spurious rules if you use only Apriori.
The traditional approach for association rule mining uses only two search criteria: minimum frequency and minimum "confidence" (precision). So, the dependence (association in the statistical sense) is not necessarily positive even in the sample. In addition, you may find a lot of rules that express statistically insignificant positive dependence (are not likely to hold in the population). Usually, only the latter are called "spurious" but obviously the independence rules and those expressing negative dependence are also false discoveries.
It is possible to search for only statistically significant positive associations (in the statistical sense, i.e. dependencies) but it is more difficult. A simple approach is to search first for traditional association rules but with so small minimum frequency and confidence thresholds that no true associations are missed. This may be computationally impossible, but if you succeed, you can afterwards select only the significant positive associations with other goodness measures and/or statistical tests. A more feasible approach is to search directly statistical associations (also called dependency rules and classification rules) with statistical goodness measures but without any minimum frequency requirements (or at most minimal). For a short review of such approaches, see e.g.
Hämäläinen: Kingfisher: an efficient algorithm for
searching for both positive and negative dependency rules with statistical significance measures, Knowledge and Information Systems
August 2012, Volume 32, Issue 2, pp 383–414 (also https://pdfs.semanticscholar.org/59ff/5cda9bfefa3b188b5302be36e956b717e28e.pdf).
Another problem in association rule mining (and pattern discovery, in general) is the multiple testing problem. Even if you check that each pattern is statistically significant (not likely due to chance), you may end up with many spurious associations. The problem is that each test can make an error (declare a spurious pattern significant) and when many patterns are tested, the errors cumulate. As a solution, you should adjust the p-values or significance levels so that the overall probability of errors is sufficiently small.
The large data size affects the problem but the effects can be unpredictable. In large data Apriori usually requires large minimum frequency thresholds (the search is often otherwise infeasible) and frequent patterns tend to be also significant (if they just express positive dependence). However, in large data even weakest dependencies may appear statistically significant (see discussion here  Statistical significance versus sample size), while in the same time you may miss the most significant (but less frequent) associations altogether.


Erro: argumento não-numérico para operador binário

Question: Estou trabalhando no seguinte código:
temperatura<-function(t){6/pi*atan(40*t-740)+7}#temperatura em função do tempo

Ratkowski<-function(t){0.04291^2*(temperatura(t)-3.57877)^2*(1-exp(1.13744*(temperatura(t)-41.64632)))^2}#modelo dinâmico

fq<-function(t){0.1*exp(Ratkowski(t))}

funcao<-function(t){integrate(fq,0,t)}

Estou tentando construir uma nova função definida como
funcao02<-function(t){funcao(t)/(funcao(t)+1)}

Está sendo gerado o seguinte erro:

Error in funcao + 1 : argumento não-numérico para operador binário

Acredito que o valor de funcao (resultado da integral) não está sendo considerado como numérico. É isso mesmo? Como resolver o problema?
Answer: Note que o resultado de funcao não é numérico:
funcao(10)
1.000333 with absolute error < 1.1e-14

É necessário extrair o valor numérico (1.000333) deste resultado. Uma maneira de fazer isto é descobrir onde o R, dentro de funcao(10), armazena este valor numérico. Faça isto através do comando names:
names(funcao(10))
[1] "value"        "abs.error"    "subdivisions" "message"      "call"

Para extrair apenas o valor numérico gerado por funcao, rode
funcao(10)$value
[1] 1.000333

Assim, basta corrigir a funcao02 para refletir esta mudança:
funcao02 <- function(t){funcao(t)$value/(funcao(t)$value+1)}
funcao02(10)
[1] 0.5000831


Django sqlite - how to change schema

Question: I have made changes to my  model.py in Django and now I want to syncronize these changes. It's fine to delete the tables and re-create them. However, nothing seems to work. I am using sqlite3:
syncdb: only works first time, not with changes
"python manage.py sql my_site", followed by syncdb: I thought this would 'redo' it all, but the table still only has the old columns (or so I assume as I get an error when I try to access the table using my model).
Then I figure that I can access the database directly and delete the tables that way. However, I don't know how to get "in" to the DB where I can execute commands. Typing sqlite3 from the command prompt is not recognized. I also tried "python manage.py sql my_site", but I again get the message that sqlite3 is not recognized.
Suggestions?
Comment: I guess u are posting in Wrong category pls specify which OS you are using and also Django does not recreate/alter table on second run of syncDB
Answer: First you have to install the command line tool for sqlite. On Ubuntu/Debian, you can simply do
sudo apt-get install sqlite3

On windows, you can download it from here: http://www.sqlite.org/download.html. Look for the one that looks like sqlite-shell-win32-xxx.zip.
Use it like this:
> sqlite3 /path/to/your/database
;show some help
.help
; list all databases
.databases
; clear the contents of a table
DELETE FROM <tablename>;

See also the command line reference: http://www.sqlite.org/sqlite.html
and the sqlite SQL reference: http://www.sqlite.org/lang.html.
Using the "ALTER TABLE" sql command, you can also add columns without deleting the entire contents of the table. To do this, compare the output of .schema  in sqlite3, and the output of  manage.py sql my_site to find out which columns you need to add.
An example:
ALTER TABLE "buildreport_series" ADD COLUMN "parameters" text

Answer: Use Django's built in database management tool:
python manage.py dbshell

And issue the required sql commands. The sql command will only print to stdout what the required sql is to create the current tables (as defined by the current models).

Django sqlite - how to change schema


uso de regex em um switch

Question: Estou tentando usar expressões regulares para validar um switch, porém, me ocorre no console que "day.match" is not a function:
function dayOfWeek(day){
   var regex = /[1-7]/g;
   if(!day.match(regex)){
      day == 8;
   }
   switch(day){
      case 1: return "Segunda";
      break;
      case 2: return "Terça";
      break;
      case 3: return "Quarta";
      break;
      case 4: return "Quinta";
      break;
      case 5: return "Sexta";
      break;
      case 6: return "Sábado";
      break;
      case 7: return "Domingo";
      break;
      case 8: return "Dia inválido";
      break;
   }
}

O que há de errado, e como posso corrigir ?
Comment: Verifique o "day", a mensagem esta informando que o match não existe no "day", é possível que você esteja mandando algo de errado no "day".
Comment: justamente como é minha pergunta, como vou verificar se não sei o que isso pode significar ?
Comment: Coloque assim console.log(day), e veja no console o que esta chegando em "day"
Comment: sua var `day` é uma instancia do objeto `Date`?
Comment: nao é @GuilhermeLautert , apenas um dia inserido pelo usuário
Answer: O problema é que você pode estar tentando usar match em um number, o correto seria:
var regex = /[1-7]/;
if( !regex.test(day) ){
    day = 8;
}

Exemplo

function dayOfWeek(day){
   var regex = /([1-7])/;
   if( !regex.test(day) ){
      day = 8;
   }

   switch(day){
      case 1: return "Segunda";
      break;
      case 2: return "Terça";
      break;
      case 3: return "Quarta";
      break;
      case 4: return "Quinta";
      break;
      case 5: return "Sexta";
      break;
      case 6: return "Sábado";
      break;
      case 7: return "Domingo";
      break;
      case 8: return "Dia inválido";
      break;
   }
}

console.log(dayOfWeek(5))
console.log(dayOfWeek(9))

Dessa forma a regex irá testar se o parâmetro é true ou falso. Mas o seu caso dispensa o uso de regex, poderia usar o default do switch:

function dayOfWeek(day){

    switch(day){
      case 1: return "Segunda";
      break;
      case 2: return "Terça";
      break;
      case 3: return "Quarta";
      break;
      case 4: return "Quinta";
      break;
      case 5: return "Sexta";
      break;
      case 6: return "Sábado";
      break;
      case 7: return "Domingo";
      break;
      default: return "Dia inválido";
      break;
   }
}

console.log(dayOfWeek(5));
console.log(dayOfWeek(9));

uso de regex em um switch


How can I use System::ErrorDescription system variable in the expression builder

Question: I'm using BIML to create SSIS packages, and one section of a package deals with the errors occurred. I'm using an Execute SQL Task which calls a stored procedure to log the error details in the database, and the stored procedure expects a parameter @Comments which holds information of the error description and some other custom info.

I'm trying to create a variable Error_Details and use an expression to build the value for @Comments param.

<Parameters>
    <Parameter Name="ParentPkgID" DataType="Int32">0</Parameter>
</Parameters>

<Variables>
    <Variable Name="TableName" DataType="String">
        tblEmployee
    </Variable>
    <Variable Name="Error_Details" DataType="String" EvaluateAsExpression="true">
        "Package execution failed, Error details :: " + @[System::ErrorDescription]
    </Variable>
</Variables>

The problem is the package creation fails when I use the above code to create the variable, I understand System::ErrorDescription is not available in the expression builder, but what are my options here.

How can I use some system variables which are only available on some specific context in BIML code.

Thanks much for looking at my question. Please suggest a solution.
Answer: This is due to a limitation in SSIS, and the SSDT designer will give you a similar error if you try to use the above expression there.
SSIS only makes the System::ErrorDescription and System::ErrorCode variables available in the context of certain event handler types, such as OnError.
You could rearrange your package to create an event handler on the ExecuteSQL task, obtain the error description therein, and then process it using your stored procedure call from within the event handler. Here is some minimal sample code that will build as expected:
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
    <Packages>
        <Package Name="Package1">
            <Tasks>
                <Dataflow Name="DataflowTask1">
                    <Events>
                        <Event Name="OnError" EventType="OnError">
                            <Variables>
                                <Variable Name="Error_Details" DataType="String" EvaluateAsExpression="true">"Package execution failed, Error details :: " + @[System::ErrorDescription]    </Variable>
                            </Variables>
                        </Event>
                    </Events>
                </Dataflow>
            </Tasks>
        </Package>
    </Packages>
</Biml>

Comment: Thanks scott very helpful. In the end I have created an Expression Task inside the Event Handler to assign the required value to the variable Error_Details.


igraph in R: Directed graphs (with arrows) causes vertex color transparency

Question: I am using the package igraph in R to be able to plot a network. However, when passing the directed = TRUE flag within the graph_from_data_frame command, it appears exactly how it should in R Studio's plot pane. However, once I preview as a PDF (Export, Save as PDF, Preview) OR Save Plot as PDF, all vertices of the graph are transparent. However, setting the directed = FALSE flag, the colors are as expected.
Setting up the data
stack.vtx <- data.frame(Dept = c("C-IIAC",  "EES-16",   "EES-17",   "A-1",  "ISR-1",    "ISR-2",    "ISR-6",    "NEN-5",    "AOT-AE",   "MPA-CINT", "MPA-MAGLAB",   "MPA-Q",    "MST-8",    "P-1",  "P-4",  "SIGMA-1",  "CCS-2",    "CCS-3",    "CCS-6",    "T-1",  "T-2",  "T-3",  "T-4",  "T-5",  "E-6",  "J-4",  "J-6",  "M-3",  "M-6",  "Q-5",  "AMPP-4",   "PT-DO",    "XCP-3",    "XCP-4",    "XCP-8",    "XTD-PRI"),
                        Count = c(2,    2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  1,  1,  2,  1,  2,  3,  4,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2),
                        Cols = c("#8B4513", "#8B4513",  "#8B4513",  "#FFB90F",  "#FFB90F",  "#FFB90F",  "#FFB90F",  "#FFB90F",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#CDC8B1",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#FF6347",  "#9AC0CD",  "#9AC0CD",  "#9AC0CD",  "#9AC0CD",  "#9AC0CD",  "#9AC0CD",  "#00CDCD",  "#00CDCD",  "#8A2BE2",  "#8A2BE2",  "#8A2BE2",  "#8A2BE2"))

stack.edges <- data.frame(from = c("AOT-AE",    "AOT-AE",   "C-IIAC",   "C-IIAC",   "CCS-2",    "EES-16",   "EES-16",   "EES-16",   "ISR-1",    "ISR-1",    "ISR-2",    "ISR-2",    "J-4",  "J-4",  "MPA-CINT", "MPA-CINT", "MPA-Q",    "MPA-Q",    "NEN-5",    "NEN-5",    "P-1",  "Q-5",  "T-1",  "T-1",  "T-2",  "T-2",  "T-2",  "T-2",  "XCP-4",    "XTD-PRI",  "XTD-PRI",  "XTD-PRI",  "XTD-PRI"),
                          to = c("CCS-3",   "J-6",  "AMPP-4",   "PT-DO",    "T-5",  "T-5",  "XCP-8",    "A-1",  "CCS-6",    "T-2",  "EES-17",   "EES-17",   "P-1",  "M-3",  "T-4",  "MPA-MAGLAB",   "CCS-6",    "C-IIAC",   "XCP-3",    "SIGMA-1",  "T-2",  "T-1",  "MST-8",    "MST-8",    "CCS-2",    "ISR-6",    "MPA-Q",    "MPA-Q",    "T-3",  "P-4",  "E-6",  "XTD-PRI",  "M-6"),
                          Cols = c("#66666660", "#66666660",    "#F0027F",  "#F0027F",  "#BEAED4",  "#7FC97F",  "#7FC97F",  "#7FC97F",  "#7FC97F",  "#7FC97F",  "#386CB0",  "#386CB0",  "#F0027F",  "#F0027F",  "#FDC086",  "#FDC086",  "#386CB0",  "#386CB0",  "#66666660",    "#66666660",    "#66666660",    "#FDC086",  "#7FC97F",  "#BEAED4",  "#66666660",    "#66666660",    "#66666660",    "#66666660",    "#BEAED4",  "#F0027F",  "#F0027F",  "#66666660",    "#66666660"))

Graphing the network
library(igraph)
stack_graph <- graph_from_data_frame(stack.edges, vertices = stack.vtx, directed = TRUE)

plot(stack_graph,
     layout = layout_in_circle(stack_graph),
     vertex.color = V(stack_graph)$Cols,
     vertex.shape = "sphere",
     vertex.label.color = "black",
     vertex.size = V(stack_graph)$Count * 4,
     vertex.label = "",
     edge.size = 0.3,
     edge.color = E(stack_graph)$Cols,
     edge.curved = 0.2,
     edge.arrow.size = 0) #Changing this flag with anything > 0 will cause transparency issues of the graph vertices

The resulting graph with directed = TRUE OR edge.arrow.size = 0.1 (or anything greater than zero, will produce a graph with the vertex colors that are transparent. Here is what it looks like:

However, the resulting graph when directed = FALSE OR edge.arrow.size = 0 will produce a graph with the vertex colors that are 'normal' or what is desired from this code. Here is what it looks like:

The closest solution I have found can be found in this post: Unexpected arrow color behavior with semi-transparent edges (igraph, R).
it is suggested to make the vertices smaller (ex. vertex.size. However, reducing the vertex.size still keeps the transparent vertices (when directed = TRUE. Additionally, passing the edge.arrow.mode suggested in the post, has no effect.
Any suggestions as to how I can obtain non-transparent vertices when graph_from_data_frame(directed = TRUE)?
Comment: Does it help to set the vertex colours directly from the vertex dataframe like: `plot(stack_graph, ..., vertex.color = stack.vtx$Cols)`?
Comment: I'm curious: what OS? What IDE are you using? What package versions do you have? If not Windows, what graphic device are you using? No matter what I do to this plot the nodes are opaque. I can't reproduce the error.
Comment: Thank you for your comment. I did try passing the `vertex.color = stack.vtx$Cols` and I still get the transparent vertices when `directed = TRUE`.

My `sessionInfo()` output = R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.6.2 (the latest one)

Attached packages: igraph_1.3.5. I am using RStudio version 2022.12.0+353 (the latest one). The graphics I am using = AMD Radeon Pro 5500M 8 GB.

To note - I can run other `igraph` plots with edge and vertex colors w/out issue.
Answer: Change #66666660 into for example #666666. That is drop the alpha channel RRGGBB60 in #RRGGBBAA.
This problem is also reproducible on Windows, Rgui.
Small example
library(igraph)
stack_graph <- make_ring(2, directed=TRUE)

plot(stack_graph,
     layout = layout_in_circle(stack_graph),
     vertex.shape = "sphere",
     edge.color = c("#66666660"),   # Transparency issues are caused by non valid RBG color code.
     edge.arrow.size = 2            # Changing this flag with anything > 0 will show transparency issues of the graph vertices.
     )

Edit: If edge.color = rgb(red=0x66/255, green=0x66/255, blue=0x66/255, alpha=0.3) in plot() is set as transparent, then the vertices will also be rendered transparent after saving in the .pdf file. Most likely an igraph bug.
Comment: Thank-you. This actually worked. To note, the #66666660 is #666666 but with transparency 60, so #66666660 - I've used this quite a bit in plotting graphs (and learned how to do this on this website a while back). However, it seems as if igraph makes everything transparent, even if one color is coded with a transparency. Again, thank-you for pointing this out.


How to hide loadingAnimation.gif in Thickbox...?

Question: I'm new to jQuery Thickbox. I've implemented the following code:
HTML:
map

JS:
function initMap(x,y,msg,flg){
        var map = new GMap2(document.getElementById("map_canvas"),{size: new GSize(400,380)});
        map.removeMapType(G_SATELLITE_MAP);
        map.removeMapType(G_HYBRID_MAP);
        var point = new GLatLng(x, y);
        var htmlMsg = msg;
        map.setCenter(point, 14);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        var baseIcon = new GIcon();
        baseIcon.shadow = "";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = "http://www.google.com/intl/en_ALL/mapfiles/marker.png";
        markerOptions = { icon:letteredIcon };
        var marker = new GMarker(point,markerOptions);
        GEvent.addListener(marker, "mouseover", function() {
            map.openInfoWindowHtml(point, htmlMsg);
        });
        map.addOverlay(marker);
        if (flg==1){
            map.openInfoWindowHtml(point, htmlMsg);
        }
        tb_show('Quick view','#TB_inline?height=400&width=400&inlineId=show_map',true);
    }

The issue is the 'loadingAnimation.gif' always visible on the thickbox, even after the google map gets completely loaded.
Answer: You can remove the loading message (so that it doesn't show at all) with the following CSS:
#TB_load img { display: none !important; }

optionally, you can remove it through script by calling
document.getElementById('TB_load').style.display = "none"

whenever you want.
Comment: I opted for your CSS method to hide it temporarily for now.
Comment: how do you call the javascript? does the getElementById yield a valid element?
Comment: Its weired... When i tried using getElementById, it is not working but when using CSS it is working..!! Anyways i am hiding the image for now.
Answer: Seems strange, but changing
map

to
map

makes it work for me.
Comment: Yes... that's the whole issue... It's also weird cause when i use # its getting redirected to home page.
Comment: Have you tried just removing the image after calling tb_show()?  Using: $("#TB_load").remove();
Answer: I'm pretty sure the Google Maps code has nothing to do with it. The best methods would be to not display the loader at all (remove it from the Thickbox code) or to hide the loading.gif after the layover has initialized.
I'm not that familiar with Thickbox. But it should have a callback. Use that hide the gif (or it's container).
Comment: @Damien: If i can remove it.. definitely i wouldn't be asking this question here. :)


How to handle pagination queries properly with mongodb and php?

Question: Am I doing this right? I went to look at some old PHP code w/ MySQL and I've managed to get it to work, however I'm wondering if there's a much "cleaner" and "faster" way of accomplishing this.
First I would need to get the total number of "documents"
<code>$total_documents = $collection->find(array("tags" => $tag,
        "seeking" => $this->session->userdata('gender'),
        "gender" => $this->session->userdata('seeking')))->count();

$skip = (int)($docs_per_page * ($page - 1));
$limit = $docs_per_page;
$total_pages = ceil($total_documents / $limit);
</code>
// Query to populate array so I can display with pagination
<code>$data['result'] = $collection->find(array("tags" => $tag,
        "seeking" => $this->session->userdata('gender'),
        "gender" => $this->session->userdata('seeking')))->limit($limit)->skip($skip)->sort(array("_id" => -1));
</code>
My question is, can I run the query in one shot? I'm basically running the same query twice, except the second time I'm passing the value to skip between records.
-- New code ...
Ok, unless someone knows of another way to do this (if it's possible), I'm going to say it's not doable. With that said, I changed the way I run my queries through mongodb, which yielded better looking code. ;-) I was trying to minimize the trips to the DB, but oh well hopefully this doesn't take a performance hit. My other attempt was to count the number of elements in the array, but quickly found out that wouldn't work since the $limit & $skip parameters would give ITS total number of docs.
<code>$skip = (int)($docs_per_page * ($page - 1));
$limit = $docs_per_page;

$query = array("loc" => array('$near' => array('lat' => $latitude, 'lon' => $longitute) ),
        "tags" => $tag, "seeking" => $this->session->userdata('gender'),
        "gender" => $this->session->userdata('seeking'));

$fields = array("username", "zipcode", "tags", "birth_date");
$total_documents = $collection->find($query, array("_id"))->count();
$data['result'] = $collection->find($query, $fields)->limit($limit)->skip($skip);
</code>
Answer: Since the result of find()->limit()->skip() is a Mongo_Cursor you don't have to execute the actual query twice.
The following should work as well :
<code>$skip = (int)($docs_per_page * ($page - 1));
$limit = $docs_per_page;

$query = array("loc" => array('$near' => array('lat' => $latitude, 'lon' => $longitute) ),
    "tags" => $tag, "seeking" => $this->session->userdata('gender'),
    "gender" => $this->session->userdata('seeking'));

$fields = array("username", "zipcode", "tags", "birth_date");
$cursor = $collection->find($query, $fields)->limit($limit)->skip($skip);
$total_documents = $cursor->count();
$data['result'] = $cursor;
</code>
btw I first misread your question, I thought you didn't know about limit & skip.
Comment: shouldn't skip come before limit?
Comment: How can we do this with aggregate ?? @vimvds
Answer: Yes you are doing right.
And you can run query in one shot.
Here is a paging example:
<code>function printStudents(pageNumber, nPerPage) {
   print("Page: " + pageNumber);
   db.students.find().skip((pageNumber-1)*nPerPage).limit(nPerPage).forEach( function(student) { print(student.name + "<p>"); } );
}
</code>
Reference: Advanced Queries - MongoDB: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-{{skip%28%29}}
Comment: I played with it some more and realized I didn't really need the first query. I just need something like this $total_documents = $collection->find($query, array("_id"))->count();


Why is tanh almost always better than sigmoid as an activation function?

Question: In Andrew Ng's Neural Networks and Deep Learning course on Coursera he says that using $tanh$ is almost always preferable to using $sigmoid$.
The reason he gives is that the outputs using $tanh$ centre around 0 rather than $sigmoid$'s 0.5, and this "makes learning for the next layer a little bit easier".

Why does centring the activation's output speed learning? I assume he's referring to the previous layer as learning happens during backprop?
Are there any other features that make $tanh$ preferable? Would the steeper gradient delay vanishing gradients?
Are there any situations where $sigmoid$ would be preferable?

Math-light, intuitive  answers preferred.
Comment: A sigmoid function is S-shaped (hence the name).  Presumably you are talking about the logistic function $\frac{e^x}{1+e^x}$.  Apart from scale and location, the two are essentially the same: $\text{logistic}(x)=\frac12 +\frac12\tanh(\frac{x}2)$.  So the real choice is whether you want outputs in the interval $(-1,1)$ or the interval $(0,1)$
Comment: double (and single) precision floats have less round-off error closer to 0.
Answer: Yan LeCun and others argue in Efficient BackProp that

Convergence is usually faster if the average of each input variable over the training set is close to zero. To see this, consider the extreme case where all the inputs are positive. Weights to a particular node in the first weight layer are updated by an amount proportional to $\delta x$ where $\delta$ is the (scalar) error at that node and $x$ is the input vector (see equations (5) and (10)). When all of the components of an input vector are positive, all of the updates of weights that feed into a node will have the same sign (i.e. sign($\delta$)). As a result, these weights can only all decrease or all increase together for a given input pattern. Thus, if a weight vector must change direction it can only do so by zigzagging which is inefficient and thus very slow.

This is why you should normalize your inputs so that the average is zero.
The same logic applies to middle layers:

This heuristic should be applied at all layers which means that we want the average of the outputs of a node to be close to zero because these outputs are the inputs to the next layer.

Postscript @craq makes the point that this quote doesn't make sense for ReLU(x)=max(0,x) which has become a widely popular activation function. While ReLU does avoid the first zigzag problem mentioned by LeCun, it doesn't solve this second point by LeCun who says it is important to push the average to zero. I would love to know what LeCun has to say about this. In any case, there is a paper called Batch Normalization, which builds on top of the work of LeCun and offers a way to address this issue:

It has been long known (LeCun et al., 1998b; Wiesler & Ney, 2011) that the network training converges faster if its inputs are whitened – i.e., linearly transformed to have zero means and unit variances, and decorrelated. As each layer observes the inputs produced by the layers below, it would be advantageous to achieve the same whitening of the inputs of each layer.

By the way, this video by Siraj explains a lot about activation functions in 10 fun minutes.

@elkout says "The real reason that tanh is preferred compared to sigmoid (...) is that the derivatives of the tanh are larger than the derivatives of the sigmoid."
I think this is a non-issue. I never seen this being a problem in the literature. If it bothers you that one derivative is smaller than another, you can just scale it.
The logistic function has the shape $\sigma(x)=\frac{1}{1+e^{-kx}}$. Usually, we use $k=1$, but nothing forbids you from using another value for $k$ to make your derivatives wider, if that was your problem.

Nitpick: tanh is also a sigmoid function. Any function with a S shape is a sigmoid. What you guys are calling sigmoid is the logistic function. The reason why the logistic function is more popular is historical reasons. It has been used for a longer time by statisticians. Besides, some feel that it is more biologically plausible.
Comment: You don't need a citation to show that $\max_x \sigma^\prime(x) < \max_x \tanh^\prime(x)$, just high-school calculus.
$$
\sigma^\prime(x) = \sigma(x) (1 - \sigma(x)) \le 0.25
$$
We know that this is true because $0 < \sigma(x) < 1$, so you just have to maximize a concave quadratic.
$$
\tanh^\prime(x) = \text{sech}^2(x) = \frac{2}{\exp(x) + \exp(-x))} \le 1.0
$$ which can be verified by inspection.
Comment: @craq, good point, I think that's a flaw in LeCun's argument indeed. I have added a link to the batch normalization paper where it discusses more about that issue and how it can be ameliorated. Unfortunately, that paper doesn't compare relu with tanh, it only compares relu with logistic (sigmoid).
Comment: How does ReLU solve the zigzagging problem?
Comment: Apart from that I said that **in most cases** the derivatives of tanh are larger than the derivatives of the sigmoid. This happens mostly when we are around 0. You are welcome to have a look at this [link](https://theclevermachine.wordpress.com/2014/09/08/derivation-derivatives-for-common-neural-network-activation-functions/) and at the clear answers provided [here](https://stats.stackexchange.com/questions/101560/tanh-activation-function-vs-sigmoid-activation-function) question which they also state that the derivates of $\tanh$ are usually larger than the derivates of the $\text{sigmoid}$.
Comment: hang on... that sounds plausible, but if middle layers should have an average output of zero, how come ReLU works so well? Isn't that a contradiction?
Comment: @ekoulier, the derivative of $\text{tanh}$ being larger than $\text{sigmoid}$ is a non-issue. You can just scale it if it bothers you.
Comment: 'When all of the components of an input vector are positive, all of the updates of weights that feed into a node will have the same sign' Isn't this true only if all the weights also have the same sign?
Comment: @ngram not necessarily. In backpropagation, finding the derivative of the loss w.r.t to some weight `wi` via chain rule will result in the same derivative for all the weights that feed into same node as `wi`; thus, the sign of their final gradient will depend on their inputs, not on the weights themselves.
Answer: It's not that it is necessarily better than $\text{sigmoid}$. In other words, it's not the center of an activation fuction that makes it better. And the idea behind both functions is the same, and they also share a similar "trend". Needless to say that the $\tanh$ function is called a shifted version of the $\text{sigmoid}$ function.
The real reason that $\text{tanh}$ is preferred compared to $\text{sigmoid}$, especially when it comes to big data when you are usually struggling to find quickly the local (or global) minimum, is that the derivatives of the $\text{tanh}$ are larger than the derivatives of the $\text{sigmoid}$. In other words, you minimize your cost function faster if you use $\text{tanh}$ as an activation fuction.
But why does the hyperbolic tangent have larger derivatives? Just to give you a very simple intuition you may observe the following graph:

The fact that the range is between -1 and 1 compared to 0 and 1, makes the function to be more convenient for neural networks. Apart from that, if I use some math, I can prove that:
$$\tanh{x} = 2σ(2x)-1$$
And in general, we may prove that in most cases  $\Big|\frac{\partial\tanh (x)}{\partial x}\Big| > \Big|\frac{\partial\text{σ} (x)}{\partial x}\Big|$.
Comment: It's not the fact that the average is around 0 that makes $\tanh$ faster. It's the fact that being around zero means that the range is also grater (compared to being around 0.5 in the case of $\text{sigmoid}$), which leads to larger derivatives, which almost always leads to faster convergence to the minimum.

I hope that it is clear now. Ng is right that we prefer the $\tanh$ function because it is centered around 0, but he just didn't provide the complete justification.
Comment: @Maxim Which "it" skews the distribution of activations, zero-centering or $2x$? If zero-centering is a Good Thing, I still don't feel that the "why" of that has been answered.
Comment: Zero-centering is more important than $2x$ ratio, because it skews the distribution of activations and *that* hurts the performance. If you take `sigmoid(x) - 0.5` and $2x$ smaller learning rate, it will learn on par with `tanh`.
Comment: So why would Prof. Ng say that it's an advantage to have the output of the function averaging around $0$?
Comment: From the 1st order derivative curves of sigmoid and tanh, we can see that only within range (-2, 2), tanh has larger derivative than sigmoid.
So by "most cases", we should prove that most of the time, the input values of  activation function should fall within this range, right? And how do we prove that?
Answer: It all essentially depends on the derivatives of the activation function, the main problem with the sigmoid function is that the max value of its derivative is 0.25, this means that the update of the values of W and b will be small.
The tanh function on the other hand, has a derivativ of up to 1.0, making the updates of W and b much larger.
This makes the tanh function almost always better as an activation function (for hidden layers) rather than the sigmoid function.
To prove this myself (at least in a simple case), I coded a simple neural network and used sigmoid, tanh and relu as activation functions, then I plotted how the error value evolved and this is what I got.

The full notebook I wrote is here https://www.kaggle.com/moriano/a-showcase-of-how-relus-can-speed-up-the-learning
If it helps, here are the charts of the derivatives of the tanh function and the sigmoid one (pay attention to the vertical axis!)
Comment: (-1) Although this is an interesting idea, it doesn't stand on it's own. In particular, most optimization methods used for DL/NN are first order gradient methods, which have a learning rate $\alpha$. If the max derivative with regards to one activation function is too small, one could easily just increase the learning rate.
Comment: Don't you run the risk of not having a stable learning curve with a higher learning rate?
Comment: Well, if the derivatives are more stable, then increasing the learning rate is less likely to destablize the estimation.
Comment: That's a fair point, do you have a link where I could learn more of this?
Comment: it might be useful if you tried a custom function with Tanh/logistic so they have same zero mean.
Answer: Answering the part of the question so far unaddressed:
Andrew Ng says that using the logistic function (commonly know as sigmoid) really only makes sense in the final layer of a binary classification network.
As the output of the network is expected to be between $0$ and $1$, the logistic is a perfect choice as it's range is exactly $(0, 1)$. No scaling and shifting of $tanh$ required.
Comment: For the output, the logistic function makes sense if you want to produce probabilities, we can all agree on that. What is being discussed is why tanh is preferred over the logistic function as an activation for the middle layers.
Comment: How do you know that's what the OP intended? It seems he was asking a general question.
Answer: And especially when you are solving an Ordinary Differential Equation with a stiff gradient.

tanh(x) converges earlier than sigmoids of all types.
Comment: Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer).
Answer: Zero mean assumption relates to the curvature of the error surface.
The whitening argument (https://stats.stackexchange.com/a/330885/27556) is a standard issue with gradient descent.
just consider linear regression with gradient descent - the curvature of the error surface is given by the covariance of the inputs (including bias term). [ for a nonlinear model such as a neural net, I would argue you have the same issues]
Zero mean ensures you have no covariance with the bias term ( and the other non zero mean inputs).
If you have covariance with a lot of inputs, then you create a narrow valley shape (in our case across the bias/constant input direction). This is hard for gradient descent to handle (because you would want a bigger learning rate along the valley and a shorter one across the valley), gradient descent has a single learning rate, so you are forced to have a very low learning rate (making slow progress along the valley) to avoid jumping from peak to peak of the valley.  Having different learning rates in each direction is essentially what second order methods do, but they are too memory intensive for large parameter models such as neural networks.
Answer: Generally, the non-zero centered activation function restricts the movement of parameters over the surface area in some specific directions. Which makes the training slower because it needs mush steps to move from the initial point to the minimum point with these restricted movements.
For more details watch only 7 min from this video starting from time 8:43.
Comment: This is very similar to the answer [here](https://stats.stackexchange.com/a/484008/7290). Please do not post duplicate answers.
If you believe a question is completely answered by an answer elsewhere, flag /
vote to close that question as a duplicate of the other. If it isn't
*completely* answered by the other answer, customize your answer here to be more
specific to the question.


Use Systems Manager to Run AWS CLI Commands on Windows EC2 Instance

Question: Currently I'm trying to use Systems Manager to execute AWS CLI commands on an EC2 instance.  I would like for Systems manager to run the below:
aws s3 cp <origin directory> s3://<destination directory>

But when I run it through the AWS-RunPowerShellScript document, here's the output I receive:
aws : The term 'aws' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\ProgramData\Amazon\SSM\InstanceData\i-
06a4de60a263996a1\document\orchestration\11f5777e-08c8-4385-9ab4-cdc85c846f
24\awsrunPowerShellScript\0.awsrunPowerShellScript\_script.ps1:1 char:1
+ aws s3 cp C:\private s3://private
+ ~~~
+ CategoryInfo          : ObjectNotFound: (aws:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

When I RDP into the Windows instance and execute that same command, the file is dropped into the S3 bucket in AWS without any issues.
Am I missing something here, or is there a better way to do this?  I'm trying to make this process as scriptable as possible without needing an SSH connection.  Appreciate all input, thanks!
Comment: Sounds like the Windows user being used by Systems Manager does not have the `aws` program in its Path.
Answer: I believe AmazonSSMAgent needs a restart to pick up changes to the Path variable.
Answer: As the above comment stated, the user being used by AWS Systems Manager did not have the AWS CLI added as a PATH variable.