update variable from 7.1 to 7.2 : got error message "The form has become outdated"

Question: Got D7 multilingual configuration with many modules, from which i18n...
Trying to update variable from 7.1 to 7.2 version, following http://drupal.org/node/1660798 #1 tutorial (I also tried other methods explained in other nodes like http://drupal.org/node/1670274 ).
After clearing cache (twice !), replacing old i18n and variable folders with the new ones, I try to run update.php. Nothing happens, I mean reloading same page... I try then to put website into maintenance mode.
After submitting maintenance mode form, got error message "The form has become outdated. Copy any unsaved work in the form below and then reload this page."
So I tried to put first into maintenance mode, before replacing folders. But then back to the same story : update.php reloads its page so I have to check again if I'm still into maintenance mode : BUT I'm not anymore...!
I guess what may happen is that changing variable folder generates the following mistake : Drupal doesn't read correctly its database variables, so doesn't see any more it is into maintenance mode ?
So who from the egg or hen comes first ? How could I manage that and stop eating aspirin ? Thanks a lot for sharing experience
Comment: All right, as it occurs sometimes, you search two nights, and one hour after posting a question you find the solution somewhere ( http://drupal.org/node/1666670 #7 ). Just had to update from i18n 1.4 to 1.5 and variable 1.1 to 1.2, then updating to variable 2.1 and i18n 1.7 versions becomes as simple as boiling an egg !!! Many people were searching the same last days, I hope it can help.
Answer: So to answer the question :
for people who don't manage to update simultaneously variable and i18n to get variable 2.1 & i18n 1.7 :
even if unusual, you have to update first from your current version (i.e i18n 1.4  and variable 1.1 ) to i18n 1.5 and variable 1.2, only after that you'll be able to update to variable 2.1 and i18n 1.7 versions.
Always following same steps :

clear cache
replace modules folders with the new version
run update.php


For statement print 3 times instead of 1. Flashscore scrape python

Question: I can't understand where is the error i'm getting. I have a file list of id matches then if i write a for to take each element then it output 3 times+ eeach element instead of 1.
The total outoput is 18 lines printed instead of 6.
<code>mamm = open('partite2.txt', 'r')
lines = mamm.readlines()
count = 0
for x in lines:
    count += 1
    url = driver.get("https://www.flashscore.com/match/" + x + "/#h2h/overall")
    time.sleep(3)
    print("Line{}: {}".format(count, x.strip()))

# --- show more results in every section --

    for i in range(3):
        # after every click search all elements again
        section_more_results = driver.find_elements_by_xpath("//div[@class='h2h__section section ']//div[@class='h2h__showMore showMore']")
        #print('len(section_more_results):', len(section_more_results))
        #print('click:', i)
        driver.execute_script('arguments[0].click()', section_more_results[i])
        # it needs to move to this element because it is hidden
        #section_more_results[i].click()

# --- get resulst in every section ---

        all_sections = driver.find_elements_by_xpath("//div[@class='h2h__section section ']")

        for section in all_sections:
            all_rows = section.find_elements_by_xpath(".//div[@class='h2h__row']")
            print('--- section ---')
            print('len(all_rows):', len(all_rows))
            count = 0
            c=0
            for row in all_rows:
                date   = row.find_element_by_xpath(".//span[@class='h2h__date']").text
                result = row.find_element_by_xpath(".//span[@class='h2h__regularTimeResult']").text
                team_home = row.find_element_by_xpath(".//span[contains(@class, 'h2h__homeParticipant')]").text
                team_away = row.find_element_by_xpath(".//span[contains(@class, 'h2h__awayParticipant')]").text
                if result != '0 : 0':
                    #print(f"{date} | {result} | {team_home:20} | {team_away}")
                    count += 1
                else:
                    c=c+1
                if count == 7:
                    break

            if c>=1:
                print("NOT OK")
                print("Found: " , c, " 0-0 finished matches")
            else:
                print("OK")
</code>
The expected output should be:
<code>--- section ---
len(all_rows): 10
OK
--- section ---
len(all_rows): 10
NOT OK
Found:  1  0-0 finished matches
--- section ---
len(all_rows): 10
OK
</code>
3 line prints for every id match. Now i'm getting 3x prints for each of them and I don't know why. And this is example of what I get if i check only 1 id match.
enter image description here
Comment: You have a for inside of another for and the print is on the inner for....I think that's the issue you are looking for.
Answer: You have to unindent the last if, that should do what you want.
<code>    mamm = open('partite2.txt', 'r')
    lines = mamm.readlines()
    count = 0
    for x in lines:
        count += 1
        url = driver.get("https://www.flashscore.com/match/" + x + "/#h2h/overall")
        time.sleep(3)
        print("Line{}: {}".format(count, x.strip()))

        # --- show more results in every section --

        for i in range(3):
            # after every click search all elements again
            section_more_results = driver.find_elements_by_xpath(
                "//div[@class='h2h__section section ']//div[@class='h2h__showMore showMore']")
            # print('len(section_more_results):', len(section_more_results))
            # print('click:', i)
            driver.execute_script('arguments[0].click()', section_more_results[i])
            # it needs to move to this element because it is hidden
            # section_more_results[i].click()

            # --- get resulst in every section ---

            all_sections = driver.find_elements_by_xpath("//div[@class='h2h__section section ']")

            for section in all_sections:
                all_rows = section.find_elements_by_xpath(".//div[@class='h2h__row']")
                print('--- section ---')
                print('len(all_rows):', len(all_rows))
                count = 0
                c = 0
                for row in all_rows:
                    date = row.find_element_by_xpath(".//span[@class='h2h__date']").text
                    result = row.find_element_by_xpath(".//span[@class='h2h__regularTimeResult']").text
                    team_home = row.find_element_by_xpath(".//span[contains(@class, 'h2h__homeParticipant')]").text
                    team_away = row.find_element_by_xpath(".//span[contains(@class, 'h2h__awayParticipant')]").text
                    if result != '0 : 0':
                        # print(f"{date} | {result} | {team_home:20} | {team_away}")
                        count += 1
                    else:
                        c = c + 1
                    if count == 7:
                        break

            if c >= 1:
               print("NOT OK")
               print("Found: ", c, " 0-0 finished matches")
            else:
               print("OK")
</code>
Comment: I tried your code but the issue is the same. I mean the code should print only 3 lines for every id match and not 3 section of print cause it iterate over it multiple times. How to fix it?
Comment: I edited the code before to help you understand.


blogdown not inserting archetype when selected

Question: I have a website setup using hugo and blogdown. I have an archetype setup called post.md, located at /themes/silhouette-hugo/archetypes/post.md, that looks like this:
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
categories:
  - Software Development
tags:
  - Untagged
featureImage: images/nelson_lakes_photo.jpg
---

""`<!--more-->`""

I went through the New Post addin in RStudio:

However the markdown file I got didn't include the archetype style
---
title: test
author: Conor Neilson
date: '2021-08-22'
slug: []
categories: []
tags: []
---

This used to work fine, it's only stopped working in the last few months.
Hugo version is 0.87.0, blogdown version 1.4.3.
Source code for my website is here
Comment: Sounds like a bug of blogdown. I'll see if I can fix it soon. BTW, I strongly recommend that you pin the Hugo version in your `.Rprofile`---`blogdown::check_site()` will tell you how: https://bookdown.org/yihui/blogdown/a-quick-example.html#when-in-doubt-run-blogdowncheck_site
Comment: Thanks for the suggestion, have now added this
Answer: I just fixed this bug in blogdown. You may try the dev version:
remotes::install_github('rstudio/blogdown')

(Remember to restart R)
Comment: Thanks Yihui. I've updated to blogdown v1.4.4, but still seeing the same issue. Only difference I'm seeing now is that the yaml includes `draft: yes`
Comment: Sorry I only tested the function `blogdown::new_post()` yesterday. The "New Post" addin seems to be still problematic. I can reproduce your problem with the addin, and will try to fix it soon. Thanks!
Comment: Fixed now. Please reinstall the dev version. Thanks!


Why isn't #i just called #i?

Question: This answer to How to write functional chord symbols with accidental roots, basically claims that (and I paraphrase) "#i is not a thing" in functional harmony. But why not just call a spade a spade, so to speak. If the chord is built on the raised tonic, why not call it #i, which is perfectly clear?
For example, in C major, why not call C#-E-G #io, which is completely clear, rather than adding the complexity of calling it viio/ii or some such?
Answer: Allowing for the possibility of a circumstance where #i makes sense, it would be the rare exception when applying an analysis in terms of functional harmony.
When #i/I does make sense
If the goal is just to name a chord, #i/I makes perfect sense. It's obviously a chord built on the raised tonic, and any added figures will express the type of chord and inversion. In the key of C major, say, #io obviously means #C-E-G.
In effect, using #i/I would simply be a variation on the Nashville Number System, which labels chords based on their interval relative to the tonic and does not concern itself with functional meaning. In such a system, #i/I or bii/II would be interchangeable, since they would point to exactly the same (enharmonically equivalent) chord.
Functional Harmony / Roman Numeral Analysis is a language
The purpose of RNA is not just to name chords, but to name them in such a way as to describe how they operate within their specific musical context. To that end, there are established ways to describe chords and established ways to interpret RNA notations. Within this system, unlike the pseudo-Nashville example above, #i and bii would designate different and unique chordal functions for the same set of absolute pitches.
i/I implies a tonic function
One of the fundamentals of RNA is that i/I chords — that is, any chord using Roman numeral i/I — carries a tonic function. #i/I has no tonic function within the "key of i/I". Such a chord would not feel at rest within the prevailing key.
bII has an established meaning as the Neapolitan chord
While #i/I has no established functional meaning, the enharmonically equivalent bII does: it functions as pulling toward the tonic. When one sees bII, there is a likelihood that the following chord will be i/I, barring an exceptional circumstance. bII is often labeled N just to avoid any possibility of ambiguity of function.
viio has an established meaning as a leading-tone chord
Similarly, seeing the chord symbol viio, one immediately understands the likelihood that the next chord will have a root a half-step higher. This is why diminished chords with other functions, such as a common-tone chord, receive other designations, such as CTo.
The chord progression i - #io - ii - V
In absolute terms — say, in "pseudo-Nashville" terms — this is perfectly clear. In C major we would have c - c#o - d - G. Perfectly common and easy to understand for anyone having seen the progression once or twice. In this way, the progression could also be given as i - biio - ii - V. Exactly the same progression, and just as clear.
However, such a notation is ambiguous as to chord function. For example, is the diminished chord leading upward or downward?
For this reason, the #i chord in this progression would be unambiguously labeled as viio/ii, because its function is to lead into the ii chord. It's function is defined as more strongly related to the ii chord rather than the i chord.
RNA is designed as an analytical language, and the complexity of the notation stems from the added functional meaning rather than the simpler literal designation of the chord.
Comment: Thanks, @ToddWilcox. That's genuinely the nicest comment you could have left.
Comment: Very nice question and answer. I learned something, which still happens all the time, but at my age I'm more and more surprised about it. :-)
Comment: Any explanation for #i°7 used as a common-tone diminished 7th chord? (Admittedly, maybe the names iii°7, v°7, #vi°7, or bvii°7 would make more sense, but man, do they all look weird...)
Comment: @Dekkadeci I've never encountered an analysis where those chords would appear, but I like the notation CTo7, because it makes the function clear and it allows for the possibility that the common tone with the preceding chord and the common tone with the succeeding chord might be different. However, in the simplest case, where the common-tone chord elaborates the same root, then it would be named according to that root. A hypothetical iii-CTo-iii would use iiio, etc.
Comment: @Aaron - Huh, they definitely taught and showed me common-tone diminished 7th chords by the Royal Conservatory of Music's Harmony 5 course (fairly certain the archetypal example is in Weber's *Oberon*).
Comment: @Dekkadeci I was responding to the specific chords you brought up. Common-tone diminished chords themselves are fairly common, but they most often occur on I or V.
Comment: @Dekkadeci I should soften the "most often occur" comment. Better to say, I have primarily encountered them on I and V chords. I don't believe I've ever encountered them on #i, iii, or bvii.
Comment: @Dekkadeci Just checking to see if your original comment has been sufficiently addressed.
Comment: @Aaron - I don't think so. It took me a while, but I found an example of what is supposedly v°7 in (a piano transcription of) the overture of Weber's *Oberon* in the 1st line of the labelled Page 57 of https://s9.imslp.org/files/imglnks/usimg/9/90/IMSLP21740-PMLP50024-Weber_Oberon_Overture_Blasser_2hands.pdf, **but it is spelled as D#-F#-A-C with A as its root** - i.e. if it really were spelled as v°7 instead, I expect to see A-C-Eb-Gb. This means that, yes, it is spelled as #i°4/3. I believe this to be the most common remaining case of #i or #i° that your answer does not yet address.
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/138659/discussion-between-aaron-and-dekkadeci).
Comment: @Aaron - As far as I can tell, that D#-F#-A-C with A as its root is still a common-tone diminished 7th chord, as it shares the A note with both its neighbours, but even then, I don't think spelling that common-tone diminished 7th chord as an inversion of #i°7 is an exception.

Why isn't #i just called #i?


How to connect to mongodb from docker?

Question: How to connect to mongodb from docker and to see all collections?
I have installed and launched this docker image
How to connect and to make insert, update?
Comment: Well, MongoDB has a default port, so you could try with that.
Comment: Outside docker or in docker? How to do that?
Comment: I guess outside.  The connection string should be something like `mongodb://127.0.0.1/dbname`.
Answer: If your container has port forwarded, like "27017:27017" then you can connect it with any mongo client sitting on your machine.
Example: mongo -u  -p  127.0.0.1/

How to connect to mongodb from docker?


Flex mobile: How to disable scale smoothing for sprite/bitmap

Question: In my Flex mobile project I need to draw a sprite with a couple of lines and rectangles and then rotate/scale it without edges smoothing. The problem is - when the sprite is drawn the first time, there is no smoothing, but once it get rotated/scaled, lines and rectangles edges become smoothed (or anti-aliased?). And they remain smoothed even when rotation is set back to 0 and scaleX scaleY are set back to 1.
I tried converting my sprite to bitmap like this:
<code>stage.quality=StageQuality.LOW; // for performance reasons
.......
var tempSprite:Sprite=new Sprite();
// draw stuff on tempSprite

var b:BitmapData = new BitmapData(w, h, true, 0x0);
b.draw(tempSprite,null,null,null,null,false); // smoothing=false
var bitmap:Bitmap = new Bitmap(b,"auto",false); // smoothing=false
bitmap.smoothing=false;
myUIComponent.addChild(bitmap);
</code>
But it didn't help - still the bitmap becomes smoothed when it is rotated/scaled.
So, my question: is it possible to disable smoothing completely or at least return sprite to an unsmoothed state when rotation and scale values are reset?
Comment: Take a look at the docs for [Stage.quality](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#quality) - it states that smoothing has no effect when you set the quality to `StageQuality.LOW`. So at least you know it's not the smoothing :) Do the artifacts go away if you raise the quality?
Comment: Some screenshots of before and after may help me understand the issue here.
Comment: http://doptrix.com/images/user/image.PNG
Here is a part of screenshot. You should probably zoom to see the difference. But it's much more noticeable on iPhone/iPod.
Comment: I'm no designer; but I have to say I cannot tell the difference between the two images.  Perhaps someone with a sharper eye will be able to help you.
Comment: I updated the screenshot - zoomed to 300%. The difference is visible, especially on old devices with poor screen resolution. Upper sprite appears blurry.
Comment: Looks like the upper one has a very small grey border around it; but otherwise I still can't tell the difference.  Sorry; this one is beyond me.  Best of luck!
Comment: It looks to me like the positioning is off by a few subpixels.  Try to use the `pixelSnapping` property and see if that helps: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Bitmap.html#pixelSnapping
Comment: No, I draw everything in integer pixels. I tried different values for pixelSnapping, pixelHinting and lots of other options without any luck. Initially my sprite is sharp until I rotate/scale it, then it immediately becomes blurry.
Comment: What kind of rotation is that? "rotation" or 3D?
Comment: I don't know what kind of "smoothing" this is. It only appears when I transform sprite, so my guess is that it's "scale smoothing".
I do rotation, rotationX, rotationY and scaleX, scaleY in my game. As far as I noticed, touching any of these parameters cause smoothing of the sprite.
Answer: If you're using AIR for mobile and enabling GPU mode then your bitmaps will always be smoothed.  All you can really do is hide the smoothing by not scaling/rotating your bitmaps and making sure that none of the containers in the draw list hierarchy are either.  I had a similar problem to yours and my only fix would've meant rewriting my display scaling code so I just lived with the blurriness until my next project.


How to multiply on a calculator which only allows add, subtract and reciprocal

Question: Assume we have a calculator with the following flaw. The only operations can be done by it are $+$ and $-$ and $\dfrac{1}{x}$ i.e. you only can add or subtract two numbers and also calculate the reciprocal but you can't multiply or divide. Using this calculator how can you multiply two numbers?
I twiddled with lots of formulas but I got nowhere. Sorry if  I can't add any further information or idea. I appreciate any solution on this....
Comment: Should we tag this [tag:puzzle]?
Comment: $xy=\underbrace {x+x+x+...+x+x+x}_{y \text{ times}}$
Comment: What if the numbers are non integer?
Comment: Can you do something like $5 \times 3 = 5+5+5 = 15$?
Comment: How do you calculate $\sqrt 2 \times \sqrt 3?$
Answer: I assume that you can also put additional numbers (except your two operands) into the calculator, i.e. that you can calculate $x+4$, for instance. Then,
$$\frac14\,x^2=\frac1{\dfrac1x-\dfrac1{x+4}}-x\tag1$$
and
$$xy=\frac14\,(x+y)^2-\frac14\,(x-y)^2\tag2.$$
Of course, (1) is valid only if $x\neq0,-4$, but there's no need to calculate $0^2$, and if $x=-4$, we use $x^2=(-x)^2$.
Comment: In case anyone wants to see the whole thing written out: $$xy=\cfrac1{\cfrac1{x+y}-\cfrac1{x+y+4}}-y-\cfrac1{\cfrac1{x-y}-\cfrac1{x-y+4}}-y.$$ (The $\pm x$'s cancel out.)
Answer: Just for curiosity because it is just a silly method to get $xy$, but Prof Vector answer made me think of that.
Let's define $H(x,y)=\dfrac 2{\frac 1x+\frac 1y}$ and $A(x,y)=\dfrac{x+y}2$ respectively harmonic and arithmetic means.
Multiplying or dividing by $2$ is not an issue since $2a=a+a$ and $\dfrac a2=\dfrac 1{\frac 1a+\frac 1a}$.
Then it is known that the arithmetic-harmonic mean $\begin{cases} x_0=x & x_{n+1}=A(x_n,y_n)\\y_0=y & y_{n+1}=H(x_n,y_n)\end{cases}$
converge quickly to the geometric mean $\sqrt{xy}$.
Finally we can use $a^2=\dfrac 1{\frac 1a-\frac 1{a+1}}-a$ to conclude.


How do I redirect a directory's default file with IIS7?

Question: I want the default document in a folder to redirect to another URL but I still want other files in the same directory to be served normally. How can I do this in IIS 7? For example:
<code>my-site.com/              => redirects to http://example.com
my-site.com/default.aspx  => redirects to http://example.com
my-site.com/foo.aspx      => served normally
</code>
Answer: If you create default.aspx and redirect it to another page, the other pages should still work.
In IIS Manager, right click the default.aspx page, select properties, select the radio button for A redirection to a URL.  Enter the redirection URL and click OK.
In IIS 7, drill down to the page in Content view, then when you click the page select Switch to Features View and select HTTP redirect.
Comment: Are these instructions for IIS 7? I don't see `Properties` when I go into Content View and right click the file.
Comment: edited my answer
Comment: Oh, ok. That makes sense. I tried that and IIS is redirecting for `/default.aspx` but not `/`. Is it supposed to redirect for both?
Comment: if you redirect / I believe it will not allow you to get inside the directory.  The default file should be sufficient.


If G has solvable word problem, then word metric $d_{S}(e,w)$ is computable.

Question: I was recently given feedback on an assignment in Geometric Group Theory that the following proposition is false:

If $G = \langle S \rangle$ is finitely generated and has solvable
  word problem, then for every word $w \in F(S)$, $d_{S}(e,w)$ is computable

(where $e$ is identity element of $G$ and $F(S)$ is the free group on $S$)
Here's an attempeted proof of the statement, where do I go wrong?

By definition of word metric $d_{S}$, $d_{S}(e,w) = |v|_{S} \leq |w|_{S}$ for some $v \in F(S)$ such that $v =_{G} w$. To find such a $v$, enumerate all words $v \in F(S)$ of length $|v|_{S} \leq |w|_{S}$. Since $S$ is a finite set and $|w|_{S}$ is finite, there are only finitely many such $v$. At each step in the enumeration, use $G$'s solvable word procedure to determine if $v =_{G} w$ (i.e $vw^{-1} =_{G} e$). Terminate the enumeration with the $v$ of shortest length. The length of $v$ is $d_{S}(e,w)$.
Comment: I agree: your proof is correct.But you need to assume that $S$ is finite - you did not actually say that! Also the relator set $R$ does not seem relevant, so it is not clear why you introduced it.
Comment: At first glance this looks entirely sensible to me. You should even be able to go further and say that $d_S(v,w)$ is computable for any words $v$ and $w$.
Comment: Fair point, I assumed $G$ was finitely-generated, but not necessarily that $S$ was such a finite generating set. Thanks for the affirmation, maybe there was a mistake!
Comment: It makes little sense to assume that "$\langle S|R\rangle$ has solvable word problem", since $R$ has nothing to do with this (the word problem does not refer to $R$ on both the input and the output).
Comment: Oh I see Derek already pointed this. You should have edited.
Answer: If $S$ is not assumed to be finite, one has to be careful in the formulation "$G=\langle S\rangle$ is finitely generated with solvable word problem". If one understands that $G$ has solvable word problem (in the usual sense, w.r.t some/any finite generating subset), then there are trivial counterexamples if it is not assumed that $S$ is finite.
For instance, let $G=\mathbf{Z}$. Let $I$ be a subset of $\mathbf{N}$; define $s(n)=2$ for $n\in I$ and $s(n)=3$ for $n\notin I$. Define $u(n)=n!$ for $n\ge 1$ odd and $u(n)=s(n/2)(n-1)!$ for $n$ even.
So $u(1)=1$, $u(2)\in\{2,3\}$, $u(3)=6$, $u(4)\in\{12,18\}$, $u(5)=120$, etc.
Then $u$ is injective; since $u_1=1$, the image of $u$ generates $\mathbf{Z}$. Let the free group on $\mathbf{N}_{>0}$ map to $\mathbf{Z}$ by the assignment $s_i\mapsto u(i)$. Then the word length of the image of $s_i^2$ is 1 if and only if $n\in I$. In particular, if $I$ is not a recursive subset, then the word length is not computable.

Edit: If one understands that $G$ has solvable word problem with respect to the family $S$, then it slightly more tricky when $S$ is infinite. (When $S$ is finite, the word length is computable, as you proved it.)
But a variant of the previous example works. Let $G$ be an arbitrary infinite f.g. group with solvable word problem (for instance $\mathbf{Z}$). Fix a finitely generated free group $F$ with a surjective homomorphism to $G$, and let $(s_n)_{n\in\mathbf{N}}$ be an arbitrary computable sequence in $F$, starting with the family of free generators. Then the corresponding word problem (determining whether an element of $F_\mathbf{N}$ maps to a trivial element of $G$) is solvable, by computation inside $F$.
On the other hand, we can assume that the image of the family is a recursively enumerable, but non-recursive subset of $G$. In this case, the word length is not computable, since one cannot decide whether the length of an element is 1.


Easiest software VPN solution in Windows Server 2003

Question: At a client's office, their SonicWALL device is out of commission temporarily, so I've set up a Linksys WRT54G router in its place until we can get a fix.
They need some kind of VPN solution, though, for access until the SonicWALL is back online.
What is the easiest way to get a VPN setup configured between Windows Server 2003 and Windows XP client machines?  I was thinking of something like Hamachi for the time being, but I'm not sure if doing it natively with Windows is easier...
Answer: RRAS's VPN is pretty easy to setup. Here is a guide.
Comment: I'm definitely trying that out later.  I'll post my results afterwards...
Answer: I'd go with Windows RRAS, its very easy to setup and manage, supports both PPTP and L2TP and RADIUS if you need it. The only significant downside is that if your running it, you can't run the windows firewall, but that's not always an issue.
Answer: You could flash the WRT54G with DD-WRT and run OpenVPN in static keying mode.
Comment: I've read some other threads mentioning that.  I definitely want to use that setup for their DMZ wi-fi access point once this problem blows over and the SonicWALL is back.
Comment: The WRT's aren't terribly pokey. So you'd probably want to disable compression and make sure that you don't saturate it. But it works well.


What would be a good objective function for a computer vision model that predicts rotation?

Question: Let's say I have to correct images of scenes which can be rotated in the range $[0, 2\pi)$. For my particular application I already know a CNN makes sense. I'd use this to detect the orientation, then apply the appropriate correction. I'm just trying to think of how to frame the problem and objective function. This blog post mentions the use of categorical cross-entropy loss with a multi-label classification problem where the labels are $\{0, 1, 2, 3, ... 359\}$. To me this doesn't make much sense because if the target is 45, then 44 is penalised just as hard as 87.
Regression feels a lot more sensible, apart from the fact that here the number line wraps back around onto itself. But, since max-pooling works with back-propagation I don't see why I can't use a modified MSE like $min((y-\hat{y})^2, (y-\hat{y}-2\pi)^2, (\hat{y}-y+2\pi)^2)$.
Another potential solution could be to go for the categorical approach, but apply a gaussian smoothing to the target probabilities, centered around the target label.
Would anyone care to comment or advise?
Comment: Just to confirm, you actually want to detect the degree to which an image was rotated vs. another? Or you want to do some other task, but want to make sure you get a model that is invariant to rotations? In the latter case, there's also the option of having an additional training loss (that the pooled embedding for two rotated versions of the image should be the same - i.e. at each step you don't just process a batch of images, but also of randomly rotated versions and besides the main objective, you also have this second objective).
Comment: @Björn that's quite a cool way of getting rotational invariance and I'll file that away, but yes I was going for the former question not the latter.
Answer: In circular statistics, your suggestion $\min((y−\hat{y})^2,(y−\hat{y}−2π)^2,(\hat{y}−y+2π)^2), $ which we could call arc distance loss, is actually one of the known loss functions that can be used. It works, and intuitively it is certainly more sensible than the categorical approach, for the reason you mention. The reason that it is still used, I suppose, is that it circumvents treating the predictions as circular entirely.
A possibly simpler and more common approach in the field of circular statistics is to use $1 - \cos(y - \hat{y}),$ which we can call the cosine distance loss, and which ranges between 0 (when $y = \hat{y}$) and 2 (when $y = \hat{y} + \pi).$
The arc distance loss is related to the Wrapped Normal distribution; the cosine distance loss is related to the von Mises distribution. Of course, other loss functions on the circle (and hypersphere, for that matter) are conceivable, and used, such as analogues of absolute error loss.
Comment: Ahh thanks! I use cosine distance often but as a similarity metric between two unit vectors in some feature embedding space. I didn't think to apply it here.
Comment: Yes, that's right. This loss is behaves just like cosine distance (often also called cosine similarity),  except that it simplifies because we only have unit vectors here.
Answer: A common approach is to have the neural network output $u, v$ representing the angle $\hat \theta$ such that $\cos(\hat \theta) = \frac{u}{u^2+v^2}$, $\sin(\hat \theta) = \frac{v}{u^2+v^2}$. This avoids "boundaries" in the output which might prove challenging for backprop to learn.
Each rotation $\theta$ has a corresponding rotation matrix, which, conveniently, can be constructed with the sin and cosine of the angle.
$$R = \left[
\begin{array}{cc}
\cos \theta & -\sin \theta\\
\sin \theta & \cos \theta\\
\end{array}
\right]$$
Let $\hat R$ be the rotation matrix corresponding to the predicted angle, and $R$ be the true rotation matrix. Then $\text{tr}(R^T\hat R) = 2\cos (\hat \theta - \theta)$ is useful for computing the cosine distance loss. Conveniently, this trace trick also works for 3D rotations, even if the rotation happens along more than one coordinate axis.
Comment: I ended up unaccepting the other answer and accepting this instead. @KeesMulder's answer didn't work out for me, and I suspect it's because of the multiple minima in the objective function but I haven't proven it rigorously. Using vanilla MSE worked decently even with the issue of a circular objective. Sure the loss sometimes spiked up during training, but overall the effect was averaged away. As such I was motivated to try this approach, but with a 2D MSE loss (therefore doing away with the circular objective). It worked like a charm!


Display hierarchical entity reference fields inside node

Question: I have the following content types in Drupal 8 :
User

field: image
other fields

Organization

field: logo
Entity reference to User [one to many]
other fields

Event

Field: Date
Field: Description
Entity reference to Organization [one to many]

The event nodes need to look like this (display the following):

Event Title
Event Date
Event Description
All the organization logos (from the organizations added as a reference entity to the event)
All the user logos that belong to the already referenced organizations

Is there any way to do this with already existing Drupal 8 modules or do I have to write some custom code ?
Answer: You could render this with four different display modes:
1.) The Event view mode to start with:

Event Title
Event Date
Event Description
Reference field for Organization, configured to render the view mode logos1
Reference field for Organization, configured to render the view mode logos2

2.) The Organization view mode logos1 contains one field for the Organization logos:

Image field logos

3.) The Organization view mode logos2 contains one field for the User logos

Reference field for the content type User, configured to render the view mode userlogos

4.) The content type User view mode userlogos

Image field for user logos

To implement two instances of the reference field you need the module Display Suite. Or combine logo1 and logos2 in one view mode with two fields, but then the logos will get mixed. Or use for the second reference field a View, with the help of EVA.
Comment: So clear and precise answer :)


Visualizing the level sets for this function

Question: Let $F: T^{2} \to \mathbb{R}$ be given by $(x_{1}, x_{2}, x_{3}) \mapsto x_{2}$. Recall that $$T^{2} = \{(x_{1}, x_{2}, x_{3}) : \left(\sqrt{x_{1}^{2} + x_{2}^{2}} - R \right)^{2} + x_{3}^{2} = r^{2}\}$$
I am being asked to identify the shapes of the level sets $F^{-1}(c)$ for $c$ a singular value, but I am a bit unclear as to what a singular value is in this context.
Answer: You're slicing $T^2$ by planes $x_2=\text{constant}=c$. Any time this plane is tangent to the surface, $c$ is a singular value. You should find $4$ such values.
Comment: Thanks for the reply. How would I go about solving for such singular values, and is there a way to visualize/describe the level sets corresponding to these singular values (and regular values, as well)?
Comment: Draw the torus and draw slices! The computation is: The normal vector of a level surface $f=C$ is given by $\nabla f$. When is this parallel to $(0,1,0)$?
Comment: Sorry if I'm being dense, but I'm very rusty on multivariable calculus. From what I understand, this approach entails viewing the 2-torus itself as a level set $G(x,y,z) = r^{2}$? So it would be the gradient of this function defining the torus (as shown in the question statement)?
Comment: Yes, precisely. Warm up on a simple sphere if you want to reassure yourself.
Comment: I'm not sure if I'm going in the right direction with this. If we define $G(x,y,z) = \left( \sqrt{x^{2} + y^{2}} - R \right)^{2} + z^{2}$ and view the 2-torus as a level set $G(x,y,z) = r^{2}$, then I calculated:

$$\nabla{G} = \left(x \left(2 - \frac{2R}{\sqrt{x^{2} + y^{2}}} \right), y \left(2 - \frac{2R}{\sqrt{x^{2} + y^{2}}} \right), 2z \right)$$

Then $\nabla{G}(0,1,0) = (0, 2-2R, 0)$.

Either I've done something wrong or I just don't know how to proceed.
Comment: No, no, you want to know when (i.e., for what $(x,y,z)$) $\nabla G(x,y,z)$ is *parallel* to $(0,1,0)$.
Comment: So after seeking some help with this multivariable calculus aspect of the problem, I have determined that the set of vectors parallel to $(0,1,0)$ is the set $(0, k, 0)$ for any $k \in \mathbb{R}$.

At this point, after determining $x = 0 = z$, we're left with solving $(y - R)^{2} = r^{2}$ and solving for $y$... which gives two possibilities: $y = R - r$ and $y = R + r$.

Am I completely off-base?
Comment: Plus or minus. Those give you the $4$ critical points I told you to expect. What are the cross-sections $x_2=c$ Of the torus for these four?
Comment: Thanks, that was quite the learning experience.

Now, I suppose you're asking about the cross-sections to motivate how the level sets look? To clarify first, cross-section refers to the intersection of the torus with the plane $x_{2} = c$?
Comment: Yuppers. Get a doughnut and slice ;)
Comment: Thank you so much for the help. It really cleared up a lot of things for me. I'm still struggling a little bit with the visualization of the level sets, but since it might be kind of hard to explain what they look like, I suppose I'll have to work it out myself.

Other than that, I would just like to clarify that the regular values for this function will be all points on the torus were $x \neq 0, y \neq 0, z \neq 0$?

I'm very thankful for your help, so if this is asking too much, I will just close the question as solved and keep at it.
Comment: The regular *values* are all $c$ other than $\pm(R\pm r)$. And what you should see is that when you slice with such planes $x_2=c$, you get either the empty set or a smooth curve. (Values live in the range of the function, not the domain.)


How to prove that $\frac{\cos\alpha}{\cos\beta}=a/b$

Question: If $\alpha \not= \beta$, and $$ a\tan \alpha+b\tan\beta=(a+b)\tan\frac{\alpha+\beta}{2}$$ then can we prove that $\frac{\cos\alpha}{\cos\beta}=\frac{a}{b}$?
Seems like I am stuck on this one.
Comment: This is not true. $\alpha = 0$, $\beta = 2\pi$.

Are there any restrictions? Like maybe they are acute?
Comment: I wonder if it might help to recall that $\tan((\alpha+\beta)/2)=(\sin\alpha+\sin\beta)/(\cos\alpha+\cos\beta)$?
Answer: This is true if we assume $\displaystyle \sin\left(\frac{\alpha -\beta}{2}\right) \neq 0$ and $\displaystyle b \neq 0$.
Let $\displaystyle \frac{a}{b} = c$
Rewrite your equation as
$$c\tan \alpha + \tan \beta = (c+1)\tan(\frac{\alpha+\beta}{2})$$
$$ c\left(\tan \alpha - \tan\left(\frac{\alpha+\beta}{2}\right)\right) = \tan\left(\frac{\alpha+\beta}{2}\right) - \tan \beta$$
Using $\displaystyle \tan x + \tan y = \frac{\sin(x+y)}{\cos x \cos y}$ we get
$$ c \cdot \frac{\sin\left(\frac{\alpha-\beta}{2}\right)}{\cos \alpha \cos \left(\frac{\alpha+\beta}{2}\right)} =\frac{\sin\left(\frac{\alpha-\beta}{2}\right)}{\cos \beta \cos \left(\frac{\alpha+\beta}{2}\right)}$$
and so
$$ \frac{\cos \alpha}{\cos \beta} = c = \frac{a}{b}$$
Comment: $\displaystyle\tan(x)+\tan(y)=\frac{\sin(x+y)}{\cos(x)\cos(y)}$ makes this a lot simpler. (+1)
Answer:
Sorry for bad drawing. The drawing is equal to the question.
$m(BAC)=\alpha$
$m(DBG)=\beta$
$x=a\tan \alpha$
$y=b\tan \beta$
Focus on EAD triangle.
$\tan (m(EAD))=\frac{x+y}{a+b}=\frac{a\tan \alpha+b\tan \beta}{a+b}=\frac{(a+b)\tan \frac{(\alpha+\beta)}{2}}{a+b}=\tan \frac{(\alpha+\beta)}{2}$
Thus
$m(EAD)=m(HAC)=\frac{(\alpha+\beta)}{2}$
Let's calculate the angles: $m(BAD)$ and $m(ABD)$ and $m(ADB)$
$m(BAD)=m(BAC)-m(HAC)=\alpha-\frac{(\alpha+\beta)}{2}=\frac{(\alpha-\beta)}{2}$
$m(ABD)=90-\alpha+90+\beta=180-\alpha+\beta$
$m(ADB)=180-m(ABD)-m(BAD)=\frac{(\alpha-\beta)}{2}$
$m(ADB)=m(BAD)$
if so ABD triangle is an isosceles triangle
Thus $AB=BD$
$AB=\frac{a}{\cos \alpha}$
$BD=\frac{b}{\cos \beta}$
$\frac{a}{\cos \alpha}=\frac{b}{\cos \beta}$
$\frac{\cos \alpha}{\cos \beta}=\frac{a}{b}$
Comment: I'd been looking for the picture proof, and you beat me to it! :)  But ... Isn't the similarity stuff irrelevant? The key is that $\triangle ABD$ is isosceles (say, with leg $r$), so that $a = r\cos\alpha$ and $b = r\cos\beta$ ... and we're done, right?
Comment: (Missed the comment-editing time window.) That $\angle HAC = (\alpha+\beta)/2$ simply restates the assumed relation, since $(a+b)\tan(\angle HAC) = x+y = a\tan\alpha+b\tan\beta$. There's no need to derive this angle's measure; we're *given* it.
Comment: If We do not have the relation $a\tan \alpha+b\tan \beta=(a+b)\tan \frac{(\alpha+\beta)}{2}$ in question, We cannot say $m(HAC)=\frac{(\alpha+\beta)}{2}$
Comment: Right, and you used the relation in your computation of $k$. I'm just saying that the relation is immediately observable in your clever diagram: In $\triangle DAE$, *clearly* $\tan\angle DAE = (x+y)/(a+b)$. We're told that $(\alpha+\beta)/2$ matches this tangent value, so there's nothing elaborate to prove here. Thales is certainly overkill.
Comment: I saw what you mean. you are correct.Blue triangle is enough to proof. Thanks in advice.
Answer: Noting that
$$
\begin{align}
\tan\left(\frac{\alpha+\beta}{2}\right)
&=\frac{\sin(\alpha+\beta)}{1+\cos(\alpha+\beta)}\\
&=\frac{\sin(\alpha)\cos(\beta)+\cos(\alpha)\sin(\beta)}{1+\cos(\alpha)\cos(\beta)-\sin(\alpha)\sin(\beta)}\\
&=\frac{\tan(\alpha)+\tan(\beta)}{1+\sec(\alpha)\sec(\beta)-\tan(\alpha)\tan(\beta)}\tag{1}
\end{align}
$$
yields
$$
\begin{align}
\tan\left(\frac{\alpha+\beta}{2}\right)-\tan(\beta)
&=\frac{\tan(\alpha)+\tan(\beta)}{1+\sec(\alpha)\sec(\beta)-\tan(\alpha)\tan(\beta)}-\tan(\beta)\\
&=\frac{\tan(\alpha)-\sec(\alpha)\sec(\beta)\tan(\beta)+\tan(\alpha)\tan^2(\beta)}{1+\sec(\alpha)\sec(\beta)-\tan(\alpha)\tan(\beta)}\\
&=\frac{\tan(\alpha)\sec^2(\beta)-\sec(\alpha)\sec^2(\beta)\sin(\beta)}{1+\sec(\alpha)\sec(\beta)-\tan(\alpha)\tan(\beta)}\\
&=\frac{\tan(\alpha)-\sec(\alpha)\sin(\beta)}{\cos^2(\beta)+\sec(\alpha)\cos(\beta)-\tan(\alpha)\cos(\beta)\sin(\beta)}\\
&=\frac{\sec(\alpha)(\sin(\alpha)-\sin(\beta))}{\cos(\beta)\sec(\alpha)(\cos(\alpha)\cos(\beta)+1-\sin(\alpha)\sin(\beta))}\\
&=\frac{\sin(\alpha)-\sin(\beta)}{\cos(\beta)(\cos(\alpha+\beta)+1)}\tag{2}
\end{align}
$$
Symmetry and negation yields
$$
\tan(\alpha)-\tan\left(\frac{\alpha+\beta}{2}\right)=\frac{\sin(\alpha)-\sin(\beta)}{\cos(\alpha)(\cos(\alpha+\beta)+1)}\tag{3}
$$
Dividing $(2)$ by $(3)$ yields
$$
\frac{\tan\left(\frac{\alpha+\beta}{2}\right)-\tan(\beta)}{\tan(\alpha)-\tan\left(\frac{\alpha+\beta}{2}\right)}=\frac{\cos(\alpha)}{\cos(\beta)}\tag{4}
$$
Back to the original equation:
$$
a\tan(\alpha)+b\tan(\beta)=(a+b)\tan\left(\frac{\alpha+\beta}{2}\right)
$$
Dividing both sides by $b$:
$$
\frac{a}{b}\tan(\alpha)+\tan(\beta)=\left(\frac{a}{b}+1\right)\tan\left(\frac{\alpha+\beta}{2}\right)
$$
Collecting $\frac{a}{b}$ on the left:
$$
\frac{a}{b}\left(\tan(\alpha)-\tan\left(\frac{\alpha+\beta}{2}\right)\right)=\tan\left(\frac{\alpha+\beta}{2}\right)-\tan(\beta)
$$
Thus, by $(4)$,
$$
\frac{a}{b}=\frac{\tan\left(\frac{\alpha+\beta}{2}\right)-\tan(\beta)}{\tan(\alpha)-\tan\left(\frac{\alpha+\beta}{2}\right)}=\frac{\cos(\alpha)}{\cos(\beta)}
$$
as requested.
Answer: Did you try applying some identities?
$\tan\theta=\frac{\sin\theta}{\cos\theta}$ if $\cos\theta\neq0$.
$\tan(\alpha+\beta)=\frac{\tan\alpha+\tan\beta}{1-\tan\alpha\tan\beta}$.


Classifying text using neural networks

Question: I would like to classify a text using neural networks. The text would consist of "address" and "non-address" texts.
My question is, how do I represent the text as a numeric values to input to my neural networks?
Should I do character level representation or word level?
Thank you
Answer: You might use one-hot encoding, so one neuron represents one letter. That being said, it seems to me that neural networks and NLP in general are bit of overkill for your problem. You might get what you want with just some clever feature engineering
Answer: Based on the question, it sounds like some grounding in the fundamentals of text analysis would be helpful. There are no shortage of resources worth exploring. Here are some suggestions:
NLP R tutorial
http://www.r-bloggers.com/natural-language-processing-tutorial/
Toronto's Deep Learning Lab - focus on neural nets
http://deeplearning.net/demos/
Stanford's NLP Lab
http://nlp.stanford.edu/software/index.shtml
Cambridge's Textual Analysis Lab
http://www.cl.cam.ac.uk/~fh295/simlex.html
National Center for Text Mining
http://nactem.ac.uk/
Corpus manuals to English language dictionaries
http://clu.uni.no/icame/manuals/

Classifying text using neural networks


Registro de boletos Caixa WebService SOAP WSDL PHP

Question: Amigos, nunca trabalhei com WS, mas agora, em decorrência das novas mudanças, estou precisando registrar boletos que gero em um site. Recebi um documento da CEF mas até agora não consegui fazer funcionar.
Seguem as instruções:
URL:
https://barramento.caixa.gov.br/sibar/ManutencaoCobrancaBancaria/Boleto/Externo
Toda chamada ao Web Service deverá ser realizada por meio de comunicação segura (SSL/HTTPS) e em método POST.
Recomendamos a utilização das mensagens XSD e WSDL fornecidos pela CAIXA para construção da solução, prezando o correto ordenamento dos campos.
Abaixo, a própria Caixa fornece uma ideia de como deve ser o resultado do xml:
O exemplo abaixo demonstra o envio de dados gerados pelo cliente à CAIXA, referente à entrada de uma solicitação de registro de título.
<ns3:SERVICO_ENTRADA  xmlns:ns2="http://caixa.gov.br/sibar" xmlns:ns4="http://caixa.gov.br/sibar/manutencao_cobranca_bancaria/boleto/externo"> <ns2:HEADER> <VERSAO>1.0</VERSAO> <AUTENTICACAO>wIXyzrhD7HjlrpCP1xNnq8vFE7ggAGuz/srw1BOtxDY=</AUTENTICACAO> <OPERACAO>INCLUI_BOLETO</OPERACAO> <SISTEMA_ORIGEM>SIGCB</SISTEMA_ORIGEM> <DATA_HORA>20170221110643</DATA_HORA> </ns2:HEADER> <DADOS> <INCLUI_BOLETO> <CODIGO_BENEFICIARIO>537588</CODIGO_BENEFICIARIO> <TITULO> <NOSSO_NUMERO>14000000091946802</NOSSO_NUMERO> <NUMERO_DOCUMENTO>NF123456</NUMERO_DOCUMENTO> <DATA_VENCIMENTO>2017-02-21</DATA_VENCIMENTO> <VALOR>1.00</VALOR> <TIPO_ESPECIE>99</TIPO_ESPECIE> <FLAG_ACEITE>S</FLAG_ACEITE> <DATA_EMISSAO>2017-02-21</DATA_EMISSAO> <JUROS_MORA> <TIPO>ISENTO</TIPO> <DATA>2017-02-21</DATA> <VALOR>0</VALOR> </JUROS_MORA> <VALOR_ABATIMENTO>0</VALOR_ABATIMENTO> <POS_VENCIMENTO> <ACAO>DEVOLVER</ACAO> <NUMERO_DIAS>0</NUMERO_DIAS> </POS_VENCIMENTO> <CODIGO_MOEDA>9</CODIGO_MOEDA> <PAGADOR> <CPF>00000000191</CPF> <NOME>EXEMPLO DE NOME DO PAGADOR</NOME> <ENDERECO> <LOGRADOURO>SAUS QUADRA 3</LOGRADOURO> <BAIRRO>BRASILIA</BAIRRO> <CIDADE>BRASILIA</CIDADE> <UF>DF</UF> <CEP>70070030</CEP> </ENDERECO> </PAGADOR> 38.239 v002 micro     26
LEIAUTE DE ARQUIVO ELETRÔNICO - WEBSERVICE XML COBRANÇA BANCÁRIA CAIXA
<SACADOR_AVALISTA> <CNPJ>00360305000104</CNPJ> <RAZAO_SOCIAL>EXEMPLO DE NOME DO SACADOR AVALISTA</RAZAO_SOCIAL> </SACADOR_AVALISTA> <PAGAMENTO> <QUANTIDADE_PERMITIDA>1</QUANTIDADE_PERMITIDA> <TIPO>NAO_ACEITA_VALOR_DIVERGENTE</TIPO> <VALOR_MINIMO>0.00</VALOR_MINIMO> <VALOR_MAXIMO>0.00</VALOR_MAXIMO> </PAGAMENTO> </TITULO> </INCLUI_BOLETO> </DADOS> </ns3:SERVICO_ENTRADA>

Alguém poderia me dar uma ajuda para montar os parâmetros de conexão para envio e tratamento do retorno?
Answer: Escrevi um protótipo de biblioteca seguindo o exemplo abaixo. Está disponível aqui.

Segue um exemplo para a inclusão de boletos que usa NuSOAP e XmlDomConstruct. Você vai ter que trocar as constantes no meio desse código para as variáveis aplicáveis ao seu ambiente e ao convênio da empresa.
include(LIB_DIR . '/nusoap/lib/nusoap.php');
include(LIB_DIR . '/XmlDomConstruct/XmlDomConstruct.php');

// Hash de autenticação
$raw = preg_replace('/[^A-Za-z0-9]/', '',
    '0' . CODIGO_BENEFICIARIO .
    NOSSO_NUMERO .
    strftime('%d%m%Y', strtotime(DATA_VENCIMENTO))) .
    sprintf('%015d', preg_replace('/[^0-9]/', '', VALOR)) .
    sprintf('%014d', CNPJ));
$autenticacao = base64_encode(hash('sha256', $raw, true));

    $xml_array = array(
        'sibar_base:HEADER' => array(
            'VERSAO' => '1.0',
            'AUTENTICACAO' => $autenticacao,
            'USUARIO_SERVICO' => 'SGCBS02P',
            'OPERACAO' => 'INCLUI_BOLETO',
            'SISTEMA_ORIGEM' => 'SIGCB',
            'UNIDADE' => UNIDADE,
            'IDENTIFICADOR_ORIGEM' => OUT_IP,
            'DATA_HORA' => date('YmdHis'),
            'ID_PROCESSO' => ID_PROCESSO,
        ),
        'DADOS' => array(
            'INCLUI_BOLETO' => array(
                'CODIGO_BENEFICIARIO' => CODIGO_BENEFICIARIO,
                'TITULO' => array(
                    'NOSSO_NUMERO' => NOSSO_NUMERO,
                    'NUMERO_DOCUMENTO' => NUMERO_DOCUMENTO,
                    'DATA_VENCIMENTO' => DATA_VENCIMENTO,
                    'VALOR' => VALOR,
                    'TIPO_ESPECIE' => '99',
                    'FLAG_ACEITE' => 'S',
                    'DATA_EMISSAO' => DATA_EMISSAO,
                    'JUROS_MORA' => array(
                        'TIPO' => 'ISENTO',
                        'VALOR' => '0',
                    ),
                    'VALOR_ABATIMENTO' => '0',
                    'POS_VENCIMENTO' => array(
                        'ACAO' => 'DEVOLVER',
                        'NUMERO_DIAS' => NUMERO_DIAS,
                    ),
                    'CODIGO_MOEDA' => '09',
                    'PAGADOR' => array(
                        'CPF' => sprintf('%014d', CPF),
                        'NOME' => NOME,
                        'ENDERECO' => array(
                            'LOGRADOURO' => LOGRADOURO,
                            'BAIRRO' => BAIRRO,
                            'CIDADE' => CIDADE,
                            'UF' => UF,
                            'CEP' => CEP
                        )
                    )
                )
            )
        );

    // Geração do XML
    $xml_root = 'manutencaocobrancabancaria:SERVICO_ENTRADA';
    $xml = new XmlDomConstruct('1.0', 'iso-8859-1');
    $xml->preserveWhiteSpace = false;
    $xml->formatOutput = true;
    $xml->fromMixed(array($xml_root => $args));
    $xml_root_item = $xml->getElementsByTagName($xml_root)->item(0);
    $xml_root_item->setAttribute('xmlns:manutencaocobrancabancaria',
        'http://caixa.gov.br/sibar/manutencao_cobranca_bancaria/boleto/externo');
    $xml_root_item->setAttribute('xmlns:sibar_base',
        'http://caixa.gov.br/sibar');
    $xml_string = preg_replace('/^<\?.*\?>/', '', $xml->saveXML());

    // Consulta ao webservice
    $client = new nusoap_client(ENDERECO_WSDL, $wsdl = true, $timeout = TIMEOUT);
    $response = $client->call('INCLUI_BOLETO', $xml_string);
    $err = $client->getError();
    if (!$client->fault && !$err)
        echo $err; // inclusão falhou

    print_r($response) // inclusão feita com sucesso


How to use priors on the parameter number with an information criterion (AIC, BIC, …)?

Question: Example
The example is made up because I hope that it's more accessible than my actual problem.
I want to determine the number of planets of a star.
I have:

data for some astronomical observable of that star, e.g., an intensity time series,
a model that describes the observable with the sizes of the star's planets as parameters,
goodness of fits / Bayes factors for the planet sizes (parameter values) that best explain the data for a given number of planets,
prior data on how likely a given number of planets is,
no prior data on planet sizes.

The model is such that for an infinite number of planets, it can explain any data. Also, a planet with size zero is equivalent to no planet.
Question
If I have priors on the number of parameters itself, how do I properly penalise additional parameters to avoid overfitting?
In the example, how do I penalise an extra planet?
I am primarily skeptical as to whether my prior information on the number of planets already covers any extra penalty imposed by an information criterion such as the Akaike or Bayesian information criterion.
Intuitively I would say that it isn't covered and thus I would use an information criterion with the product of Bayes factor and the known priors for the number of planets as likelihood.
My prime rationale for this is that in absence of prior information on the number of planets, i.e., equal priors, I clearly need to have an additional penalty lest I arrive at a model with an infinite number of planets.
On the other hand, I was thinking: What if the ratio between the prior probabilities of one and no planet was higher than the parameter penalty imposed by the information criterion?
For example, in case of the AIC, what if one planet were more probable than no planet by a factor $e$ (from prior insights)?
In that case, my approach would always favour one planet over none – unless I implement a penalty, lower prior, or similar for very small planets.
Comment: I think the vagueness of the example makes it harder to discern the question.  I would have thought that more planets would mean more parameters to describe them.  As you already have a prior on the number of parameters, if it penalises large models, it is already helping to avoid overfitting,  Note also you may want to avoid overfitting by penalising the values of parameters (e.g. regularisation).  The idea that complexity is only measured by the number of parameters is a bit past its sell by date.
Comment: @DikranMarsupial: *I would have thought that more planets would mean more parameters to describe them.* – They do. Each planet comes with one extra parameter, namely its size. (I hoped this was clear from the question.) — *As you already have a prior on the number of parameters, if it penalises large models, it is already helping to avoid overfitting* – Well, if you so wish, my question is whether this is sufficient or some information criterion needs to be included on top. The example when my prior says that one planet is more likely than none suggests this.
Comment: there is no way of telling whether a prior on the number of parameters is sufficient to avoid overfitting without trying other forms of overfitting avoidance and seeing what happens.  The more (correct) prior knowledge you put into a model, the more likely it is to perform well, but if you have enough data your priors become irrelevant - so there is a compromise that also involves data quality and quantity.  If you want to avoid ambiguity, then set out the model formally with equations.
Answer: You are mixing two different concepts: model selection and regularization.

AIC and BIC are used for model selection. Model selection is not that different between frequentist and Bayesian settings, though they may use different tools and methods. You can easily search for Bayesian model selection to learn more.
Another thing is regularization. In a frequentist setting, you could use regularization to penalize the number of parameters (e.g. ridge or lasso regression), the same is done in a Bayesian setup with choosing sparsity introducing priors that drag the parameters towards zero. You either need to choose the priors based on what you know about the problem or choose a hyperprior that would be used to learn the priors.
Comment: Thank you for your answer. I haven't delved into the links and concepts you refer to, but I definitely will later. Still, I expect that it would be useful if you could briefly elaborate what approach is appropriate for the problem in question, where the choice of model somewhat aligns with the scientific question (i.e., how many planets there are).
Comment: @Wrzlprmft there's no one appropriate solutions, but many possibilities. What you are describing sounds like a research project that can take several weeks or months, than something that has one, simple solution.
Comment: (Finally getting back to this) *You are mixing […] model selection and regularization.* – Okay, but what is the difference, in particular when it comes to what problem they solve? Most of the literature I found so far either focuses on one of these, but the illustrative problems seems to fall in the same class to me. In some literature, e.g., [van Erp et al. (2019)](https://doi.org/10.1016/j.jmp.2018.12.004) shrinkage priors and BIC are compared when attacking the same problem.
Comment: *What you are describing sounds like a research project that can take several weeks or months* – Finding out how to attack the general class of problems I describe or how to do it for a specific example? In the latter case, is there any answer to the former?


Why is $E(\hat{\beta}_1) = \beta_1 + 1$ also an unbiased estimator of true $\beta_1$

Question: let regression model be $$Y_i = \beta_0 + \beta_1X_i +u_i$$
If the ordinary least square estimator of $\beta_1$, $\hat{\beta}1 = \frac{\sum^n_{i=1}(x_i -\bar{X})(y_i-\bar{Y})}{\sum^n_{i=1}(x_i -\bar{X})^2}$ is an unbiased estimator for true $\beta_1$. Is $\hat{\beta_1}$ also an unbiased estimator of the true $\beta_1 + 1$? In other words, is $E(\hat{\beta}_1) = \beta_1 + 1?$
Question: Why would $E(\hat{\beta_1}) = \beta_1 + 1$ still be an unbiased estimator?
Comment: How can $\hat{\beta_1}$ be unbiased for $\beta_1+1$ when it is already unbiased for $\beta_1$?
Answer: No
$$\mathbb E[\hat\beta_1]=\beta_1\ne \beta_1+1$$
But $\hat\beta_1+1$ will be unbiased for $\beta_1+1$.
$$
\mathbb E[\hat\beta_1 + 1] = \mathbb E[\hat\beta_1] + \mathbb E[1] = \beta_1 + 1
$$


How can I map multiple fields to one 'parent' field?

Question: What I want to do is best illustrated with an example.
Apartments have different price ranges for different units according to how many bedrooms/bathrooms those units have.
How could I set up a content type that will allow me to do something like this:
$500 -- 5br/4ba
$400 -- 4br/3ba
$350 -- 4br/2ba
In the above example each price range has a corresponding 'bedroom' and 'bathroom' value.
In order for this to work I need to map these price ranges to these values. Each apartment entity can have up to 10 of these mapping pairs.
I hope this makes sense. I can't figure out how to set up the content type.
Thanks for your patience and help.
Answer: See this example post on Select lists for content types:
How to have CCK "Select list" field on my content type?
you would just make the values for the list option as (value | display text):

500 | $500 - 5br/4ba
400 | $400 - 4br/3ba
350 | $350 - 4br/2ba
... 6 more options of whatever conditions ...
 25 | $25 - you sleep in the closet

This price field could allow up to 10 values if that's your limit.
Since the keys (values) of the prices are integers. You could SUM or Aggregate the values using Views if needed.
Answer: Have you looked at Conditional Fields?

Conditional Fields allows you to manage sets of dependencies between fields. When a field is "dependent", it will only be available for editing and displayed if the state of the "dependee" field matches the right condition.
  When editing a node (or any other entity type that supports fields, like users and categories), the dependent fields are dynamically modified with the States API.

With this module, I think that you could create a scheme whereby the field containing the bed/bath values would be dependent on the field containing the price values. Selecting $500 (for example) would automatically update the bed/bath value for the node with '5br/4ba'. Not entirely sure if that's what you're looking for.
Comment: Not quite what I need. I installed it and played around with it. But it doesn't map my values. I need something that takes a price, asks the user how many BR, BA are associated with that price, and then associates that price with those values.
Comment: I don't follow. You should probably clarify your issue in the OP.


Add Outlook Category to mail via MS Graph

Question: I am trying to add a Category to an email via Graph.
I found this:
https://github.com/microsoftgraph/microsoft-graph-docs/blob/main/api-reference/v1.0/resources/outlookcategory.md
But it only explains how to create a new category. I already got my categories and I'd like to add one to a mail I've also got in memory.
var categories = await graphService.Users[metadata.UserOid].Outlook.MasterCategories.Request().GetAsync();
var category = categories.SingleOrDefault(c => c.DisplayName == name);
if (category == null)
{
  if (KNOWN_OUTLOOK_CATEGORIES.ContainsKey(name))
  {
    await graphService.Users[metadata.UserOid].Outlook.MasterCategories.Request().AddAsync(KNOWN_OUTLOOK_CATEGORIES[name]);
    if (retry > 3)
    throw new InvalidOperationException($"Cannot create category in mailbox: {name}.");
    await SetCategoryMailByName(name, graphService, metadata, retry + 1);
  }
}
// Add my category to the mail
await graphService.Users[metadata.UserOid].Messages[m.MailId].Categories.Add(category);

Problem here is that there is no category in the mail. How could I add one?
Answer: Try adding a new Message object with the new category instead of trying to add it to the message categories list:
var message = new Message
{
    Categories = ["new category"]
};

await graphClient.Me.Messages["{message-id}"]
    .Request()
    .UpdateAsync(message);


Limiting distribution of first order statistics ${X}^{n}$

Question: Question from "Introduction to probability and mathematical statistics" by Bain and Engelhardt
Salutations! I am attempting the problem above.Specifically I am having a problem with part c). I do not understand how the answer at the back of the book was arrived at. Could you please help me figure it out. the answer given is $$0 \, \text{for} \, y \le 1 \\  1-\frac{1}{y} \, \text{for} \, y\gt 1 \\$$
Thank you!
Answer: I found the answer. So just in case anyone struggles with the same problem her is the solution.
$$F_{X_{1:n}}(x) = P[X_{1:n}\le x]\\ = 1-P[X_{1:n} \gt x]\\= 1- P[Min(X_{1},X_{2},...X_{n}) \gt x] \\ =1- P[X_{1}\gt x, X_{2} \gt x,...,X_{n} \gt x] \\
=\prod_{i=1}^nP[X_{i:n} \gt x]  = 1-[1-F(x)]^n = 1 - (1-(1- \frac{1}{x}))^n \\
= 1 - \frac{1}{x^n} $$
Thus the limit of this goes to 1 as n goes to $\infty.$ Similarly for part c) we have that:
$$F_{X_{1:n}^n}(x) = P[X_{1:n}^n\le x]\\ = P[X_{1:n} \le x^{1/n}]\\
= 1 - (x^{1/n})^{-n} = 1- \frac{1}{x} $$
Hence the limit of this expression stays the same when n approaches $\infty$.


Idiomatic way of returning nil in Ruby

Question: Is it idiomatic in Ruby to not explicitly return nil? Let's say something like this:
def find_something data
    if data.has_what_we_need?
        a = data.something
        if a.has_what_we_want?
            a.the_stuff[42]
        end
    end
end

As opposed to something (arguably noisier) like this:
def find_something data
    if data.has_what_we_need?
        a = data.something
        if a.has_what_we_want?
            a.the_stuff[42]
        else
            nil
        end
    else
        nil
    end
end

Answer: I try to create methods that bail out early if conditions aren't met, flattening the code in terms of indentation:
def find_something data
  return unless data.has_what_we_need?

  a = data.something

  return unless a.has_what_we_want?

  a.the_stuff[42]
end

This has two well defined exit points, as return should show up as a fairly obvious thing when reading it. A return with no argument will return nil to the caller.
If you don't care about nil vs. false you can condense this even more:
def find_something data
  return unless data.has_what_we_need?

  a = data.something

  a.has_what_we_want? and a.the_stuff[42]
end

Comment: I like the postfix conditional for flow-control - as well as reducing indentation level, if it used mainly for flow control (as opposed for logic like `e = 3 if a.b?`), then it is quick to spot flow points when reading code.
Answer: I'm unaware of a specific idiom pertaining to this, but this might be preferable:
def find_something data
    if data.has_what_we_need?
        a = data.something
        if a.has_what_we_want?
            return a.the_stuff[42]
        end
    end

    nil
end

Comment: @detunized: Implied results that pop out from 3 lines back in the code  don't seem much better though. Sometimes it's better to make one thing explicit and readable over and above another. Code philosophies taken as absolutes can lead to some pretty awful code (some of the worst code I've seen recently has been due to promoting DRY above all other considerations).
Comment: Sounds like @Neil agrees with Obi-Wan: http://www.youtube.com/watch?v=wgpytjlW5wU
Comment: Thanks. I try to avoid using `return` (and all the other flow breaking statements) in Ruby. Every time I see a `return` I think it could've been done better.
Comment: The funny thing is I agree. I would almost never do this in Java, but it seems fairly idiomatic in Ruby. I've adjusted accordingly in my own code, but I get what you're saying.
Answer: If by idiomatic you mean does ruby by default return nil from a method then no.
Ruby methods by default return the value of the last statement executed. If your last statement resolves to nil then your method will return a nil.
I'm assuming from your example that you want a method to return a value if found but nil if not found. Something like this might work:
def find_something (i)
    a = ['boris', 'natasha', 'moose' ,'squirrel']
    a[i]
end

irb(main):020:0> find_something 4
=> nil
irb(main):021:0> find_something 2
=> "moose"`

The result from the index into array a is the return value.
Hope this helps.
Comment: By default methods in Ruby are empty, and an empty method returns, by default, nil.  You need to actually write something in the method to change this.  I should add that methods return the last line evaluated.

Idiomatic way of returning nil in Ruby


update variable depending on form values

Question: it's a pretty detailed form with many values to enter & the result is a price, the function to get the price depending on the main 2 select box is working fine it gives me a variable "price" that is updated on checkbox changes & select box changes, the problem is some check boxes affect the final price like add 40% of the total price the changes work perfectly fine with only 1 checkbox affecting the price but upon adding other inputs affecting the price it doesn't count in the previous inputs in.
I tried putting many conditions like if first check box is checked & first radio is selected do this : .. else if first check box is checked & second radio is selected do that : ..
but it sounds complicated, I hope there is an easier way of doing it using j query or JavaScript

function getPrice() {
//let's assume the price is 80
price = 80;
//order duo is supposed to add 20% of total price
if ($('#order_duo').is(':checked')) {
 document.getElementById("price").textContent=price + price*0.2;
 document.getElementById("price_input").value = price +  price*0.2;
}
else {
    document.getElementById("price").textContent=price;
    document.getElementById("price_input").value = price;
}
//if prefered lane is support add 50% of total price
$('#prefered_lane').on('change', function() {
    prefered_lane = this.value;
    if (prefered_lane === "Support") {
      getPrice();
      price =   parseInt(document.getElementById("price_input").value);
    document.getElementById("price").textContent=price + price*0.5;
    document.getElementById("price_input").value = price +  price*0.5;
  }else{
    getPrice();
    price = document.getElementById("price_input").value;
     document.getElementById("price").textContent=price;
    document.getElementById("price_input").value = price;
  }
});
}
$('#prefered_lane').on('change', function() {
  getPrice();
  });
$('#order_duo').click(
    function () {
     if ($('#order_duo').is(':checked')) {
   getPrice();
}
else {
    getPrice();
}
});

<form>
        <input type="text" name="price_input" id="price_input" value="" hidden="">
<input type="checkbox" value="true" name="order_duo" class="form-check-input" id="order_duo"  style="width: 20px;height: 20px;">
<select id="prefered_lane" name="prefered_lane" class="form-control" style="text-align-last: center;"  autocomplete="off">
  <option value="Top Lane">Top Lane</option>
  <option value="Jungle">Jungle</option>
  <option value="Mid Lane">Mid Lane</option>
  <option value="AD Carry">AD Carry</option>
  <option value="Support">Support (+50%)</option>
</select>

Buy Now

For €

</form>

Once you uncheck duo while selecting support price resets to 80 :/
Answer: You are resetting your price in each calculation in the getPrice function, but you are not taking into account both lane and duo conditions, only duo.
The $('#prefered_lane').on() block inside the function doesn't execute during your price calculation. What it does is add a new event handler to the select element, so the result is that if you uncheck duo, price goes back to the original 80.
Having this in mind, you can clean up your code:

// Do calculations whenever any element changes
$('#order_duo, #prefered_lane').change(function() {
  //let's assume the price is 80
  let price = 80;

  //order duo is supposed to add 20% of total price
  if ($('#order_duo').is(':checked')) {
    price = price * 1.2;
  }
  //if prefered lane is support add 50% of total price
  if ($('#prefered_lane').val() === "Support") {
    price = price * 1.5;
  }

  $('#price').text(price);
  $('#price_input').val(price);
});

<form>
  <input type="text" name="price_input" id="price_input" value="" hidden="">
  <input type="checkbox" value="true" name="order_duo" class="form-check-input" id="order_duo" style="width: 20px;height: 20px;">
  <select id="prefered_lane" name="prefered_lane" class="form-control" style="text-align-last: center;" autocomplete="off">
    <option value="Top Lane">Top Lane</option>
    <option value="Jungle">Jungle</option>
    <option value="Mid Lane">Mid Lane</option>
    <option value="AD Carry">AD Carry</option>
    <option value="Support">Support (+50%)</option>
  </select>

Buy Now

For €

</form>


Score for orchestra tuning

Question: I was talking this monring about Cage's 4:33 and I mentioned that I'd love to see a score for an orchestra tuning. It's one of the most recognisable sounds in concerts and has many distinctive musical aspects in various combinations:

the initial oboe,
the tuning of each string,
apparent exchanges and "battles", as players alternate,
ocassional scales,
often a comically late brass honk,
the slow subsiding as tuning ends,
often the tap of the baton,
the following moment's silence.

It seemed to me that at some point in the joyous 1960s, some wag must have prepared a full orchestral score of this general cacophone, that some poor band be put through the effort of reproducing it precisely. We had a vague idea that Hoffnung may have done it. I'd love to see the score. I'm sure it will be amusing just to read it, to see the notation, and to hear, like the "Pianists" in Carnival of the Animals.
Does anyone have a link to such a thing, or have a proper bibliographic reference. Has it ever been scheduled along with 4:33? :-)
Comment: That might be fun. I've long thought the very first notes of Beethoven's 9th reminds me of tuning, but it doesn't have all the elements you describe. Seems like an opportunity for you to make a fun composition!
Comment: You've got it a bit backwards: the battles and scales better have happened during warmups, or someone's going to get beaten about the head with a (spare :-) ) violin bow.   Anyway,  I once played a concert band medley of Sgt.Pepper tunes which started out with a pseudo-warmup sound.
Comment: In P.D.Q. Bach's ["Howdy" Symphony](http://www.presser.com/shop/rental/howdy-symphony-s-67-8.html) ("discovered" in 1976 by that joyous '60s wag Peter Schickele), the orchestra tunes about halfway through the first movement.  But in the score it's just notated as an unmeasured bar with the instructions "tune & noodle".
Answer: John Corigliano's oboe concerto starts with the oboe tuning, as part of the first movement.  I have no idea how it's notated for the ensemble, though knowing the composer, I doubt it's very precise.
Comment: That's a beautiful effect, like someone emerging from among a picturesque chorus to sing their aria.
Answer: You may want to check out Tuning Up by Edgard Varèse. It's not exactly what you're looking for, but it's an earlier example than the Corigliano mentioned earlier.
I also wanted to mention a story I once heard. It may be apocryphal, but I don't believe it was. I've forgotten the source, so I wanted to share it in case anyone else knows any more details:
A famous non-Western musician joined a famous Western composer to the former's first orchestra concert. The lights dimmed, and the oboe played its tuning pitch. When at last the orchestra became silent after about 30 seconds, the non-Westerner turned to his colleague, thrilled by what he had just heard, before he was informed that it was "just" the tuning note. As I remember it, these microtonal fluctuations were something that his musical culture really valued, and so he was so excited to hear something so close to "his" music in this foreign country. Meanwhile, the Westerner, whose culture was not geared towards microtonality, never once thought to actually listen and experience the tuning practice.
Comment: Which is almost the reverse of the Ravi Shankar story; where, after the first 'collection of noises' made on stage the [western] audience applauded appreciatively. He took the mic & said, "Thank you. I hope you appreciate our music as much as you did our tuning up."
Comment: I'm fairly sure it's apocryphal - I've heard it many times, with different nationalities and occupations.
Comment: @Tetsujin - that could well be true. The first sitar recital I went to, I felt the performer was taking an absolute age with his tuning checking, etc. After about 7 or 8 minutes, he stopped, and everyone applauded - it was his first number.
Comment: As opposed to "Tuning Up" by Ken Aldin, which is more about pyramids.
Comment: There's  a little of that at the start of Stravinsky's The Rake's Progress.
Comment: Or the Miles Davis/Eddie VIncent "Tune Up."  song

Score for orchestra tuning


what is the fastest way of a typical 2d array path finding?

Question: an expecting result is like this one.
0 0 0 1 0 * * * 0 0 0 0
0 0 0 0 1 * 1 * * 0 0 0
0 0 1 1 1 * 1 0 * 0 0 0
0 * * 0 1 * 1 0 * 0 0 0
0 * 1 1 1 * 1 0 * * 0 0
0 * 0 1 1 * 1 0 0 * 0 0
0 * 0 0 1 * 1 0 0 * 0 0
0 * * * * * 1 0 0 * * 0
0 0 0 0 0 0 1 0 0 0 * 0
0 0 0 0 0 0 1 0 0 0 * 0
0 0 0 0 0 0 1 0 0 0 * 0
0 0 0 0 0 0 1 0 0 0 * *

you can only walk at 4 directions,no 45 degree direction, im using A* , i changed part of the original algorithm for more suited in my case.
here's my python code:
i run it 1000 times.
the cost is 1.4s~1.5s
def astar(m,startp,endp):
    w,h = 12,12
    sx,sy = startp
    ex,ey = endp
    #[parent node, x, y,g,f]
    node = [None,sx,sy,0,abs(ex-sx)+abs(ey-sy)]
    closeList = [node]
    createdList = {}
    createdList[sy*w+sx] = node
    k=0
    while(closeList):
        node = closeList.pop(0)
        x = node[1]
        y = node[2]
        l = node[3]+1
        k+=1
        #find neighbours
        #make the path not too strange
        if k&1:
            neighbours = ((x,y+1),(x,y-1),(x+1,y),(x-1,y))
        else:
            neighbours = ((x+1,y),(x-1,y),(x,y+1),(x,y-1))
        for nx,ny in neighbours:
            if nx==ex and ny==ey:
                path = [(ex,ey)]
                while node:
                    path.append((node[1],node[2]))
                    node = node[0]
                return list(reversed(path))
            if 0<=nx<w and 0<=ny<h and m[ny][nx]==0:
                if ny*w+nx not in createdList:
                    nn = (node,nx,ny,l,l+abs(nx-ex)+abs(ny-ey))
                    createdList[ny*w+nx] = nn
                    #adding to closelist ,using binary heap
                    nni = len(closeList)
                    closeList.append(nn)
                    while nni:
                        i = (nni-1)>>1
                        if closeList[i][4]>nn[4]:
                            closeList[i],closeList[nni] = nn,closeList[i]
                            nni = i
                        else:
                            break

    return 'not found'

m = ((0,0,0,1,0,0,0,0,0,0,0,0),
     (0,0,0,0,1,0,1,0,0,0,0,0),
     (0,0,1,1,1,0,1,0,0,0,0,0),
     (0,0,0,0,1,0,1,0,0,0,0,0),
     (0,0,1,1,1,0,1,0,0,0,0,0),
     (0,0,0,1,1,0,1,0,0,0,0,0),
     (0,0,0,0,1,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0),
     (0,0,0,0,0,0,1,0,0,0,0,0)
     )

t1 = time.time()
for i in range(1000):
    result = astar(m,(2,3),(11,11))
print(time.time()-t1)
cm = [list(x[:]) for x in m]

if isinstance(result, list):
    for y in range(len(m)):
        my = m[y]
        for x in range(len(my)):
            for px,py in result:
                if px==x and py ==y:
                    cm[y][x] = '*'

for my in cm:
    print(' '.join([str(x) for x in my]))

exit(0)

tell me if you know faster or fastest way by now.
Comment: Not an answer, but something you may want to compare your performance with: http://networkx.lanl.gov/ implementation of shortest paths.
Answer: A* algorithm is pretty fast one for a known graph (all edges are known and you can estimate distance to the target using some admissible heuristic).
There are some improvements to A* algorithm which makes it faster at the cost of being less optimal. The most common is A*-Epsilon (AKA bounded A*). The idea is to allow the algorithm to develop nodes that are (1+epsilon)*MIN (where regular A* develops only MIN). The result (depending on the epsilon value of course) is usually a faster solution, but the path found is at most (1+epsilon) * OPTIMAL.

Another possible optimization is doing A* from one end - and from the other (the "exit") do a BFS simultaneously. This technique is called bi-directional search - and is usually a great way to improve performance in unweighted graphs when the problem has a single final state. I tried to explain the principles of bi-directional search once in this thread


correct way to load JS library for custom module

Question: I am trying to use d3.js in my module, but I am facing some issues.
I have the D3 module installed in sites/all/modules/d3/ and d3.min.js installed in sites/all/libraries/d3/, and I can see the d3/examples fine.
Now I have a js script in my custom module that is supposed to use D3, but I fail to load it. Here's my attempt to load the library:
drupal_add_library('d3','d3');
# retrieve some data as JSON
$setting = mymodule_load_data($param);
# create the render array
$content = array(
  '#attached' => array(
    # attach JS
    'js' => array(
      array(
        'type' => 'setting',
        'data' => array('mymodule' => array('json_data' => $setting)),
      ),
      array(
        'type' => 'file',
        'data' => drupal_get_path('module', 'mymodule') .'/js/myscript.js',
      ),
    ),
    'css' => array(
      drupal_get_path('module', 'mymodule') . '/css/mystyle.css',
    ),
  )
);
# return result
return $content;

but I don't see d3 included in the source code of my page and myscript.js of course doesn't work.
I have checked the return value of drupal_add_library as suggested in the comments and it's FALSE.
Any hints?
Comment: You could implement [`hook_library_alter`](https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_library_alter/7.x) in your module. The second argument `$module` can be checked if it matches `d3`, if so, dump the `$libraries` (first argument) to see what libraries this module declares (`dpm()` if you have devel or just `var_dump()`). That might give you a clue on where you're going wrong.
Comment: `drupal_add_library` returns a value. The library array, or `FALSE` if the library wasn't found. Check the return value and post an update.
Comment: such value is FALSE
Answer: drupal_add_library() works only with libraries defined from hook_library() implementations. Since the d3.js module implements hooks used from the Libraries API module, you need to use the functions this module make available.
See Using Libraries API 2.x (as a module-developer) or Using Libraries API 1.x (as a module-developer), basing on which version of the Libraries API module you are using.
Normally, with the 2.x branch of the Libraries API module, you just call libraries_load('d3') which will call drupal_add_js(), or drupal_add_css(), or both, depending on what the library contains. Otherwise, if you need to check when a library has not been loaded, you use code similar to the following one.
if (($library = libraries_load($name)) && !empty($library['loaded'])) {
  // The library has been loaded.
}

Even better, using a render array, you just need to use code similar to the following one, which is for the case the library needs to be attached to a form element.
$form['myelement']['#attached']['libraries_load'][] = array('d3');

For an hook_page_build() implementation, adding the library at the page bottom requires code similar to the following one.
function mymodule_page_build(&$page) {
    $page['page_bottom']['d3_library']['#attached']['libraries_load'][] = array('d3');
  }
}


Seq2Seq with Keras understanding

Question: For some self-studying, I'm trying to implement simple a sequence-to-sequence model using Keras. While I get the basic idea and there are several tutorials available online, I still struggle with some basic concepts when looking these tutorials:

Keras Tutorial: I've tried to adopt this tutorial. Unfortunately, it is for character sequences, but I'm aiming for word sequences. There's is a block to explain the required for word sequences, but this is currently throwing "wrong dimension" errors -- but that's OK, probably some data preparation errors from my side. But more importantly, in this tutorial, I can clearly see the 2 types of input and 1 type of output: encoder_input_data, decoder_input_data, decoder_target_data
MachineLearningMastery Tutorial: Here the network model looks very different, completely sequential with 1 input and 1 output. From what I can tell, here the decoder gets just the output of the encoder.

Is it correct to say that these are indeed two different approaches towards Seq2Seq? Which one is maybe better and why? Or do I read the 2nd tutorial wrongly? I already got an understanding in sequence classification and sequences labeling, but with sequence-to-sequence it hasn't properly clicked yet.
Answer: Yes, those two are different approaches and there are other variations as well. MachineLearningMastery simplifies things a bit to make it accessible. I believe Keras method might perform better and is what you will need if you want to advance to seq2seq with attention which is almost always the case.
MachineLearningMastery has a hacky workaround that allows it to work without handing in decoder inputs. It simply repeats the last hidden state and passes that as the input at each timestep. This is not a flexible solution.
    model.add(RepeatVector(tar_timesteps))

On the other hand Keras tutorial has several other concepts like teacher forcing (using targets as inputs to the decoder), embeddings(lack of) and a lengthier inference process but it should set you up for attention.
I would also recommend pytorch tutorial which I feel is the most appropriate method.
Edit:
I dont know your task but what you would want for word embedding is
x = Embedding(num_encoder_tokens, latent_dim)(encoder_inputs)

Before that, you need to map every word in the vocabulary into an integer, turn every sentence into a sequence of integers and pass that sequence of integers to the model (embedding layer of latent_dim maybe 120). So each of your word is now represented by a vector of size 120. Also your input sentences must be all of the same size. So find an appropriate max sentence length and turn every sentence into that length and pad with zero if sentences are shorter than max len where 0 represents a null word perhaps.
Comment: Littleone, thanks! That helps! I didn't look into the PyTorch tutorial. That made me actually just realize that using the (shifted) targets as input for the decoder is the "teacher forcing". While I also try with PyTorch, for this project/tutorial I need to stick with Keras. I just need to figure out how to adopt the Keras tutorial to word sequences -- the presented extensions does use Embeddings, but I still struggle, probably because I'm not sure how the input data is supposed to be shaped.
Comment: Thanks for the edits. Yeah, I think (well, hope) I did the encoding and padding of the sequences correctly since I used embeddings in previous tutorials, e.g., sequence classification and sequence labeling. I've tried a couple of hours more with no prevail. [https://stackoverflow.com/questions/48728099/word-level-seq2seq-with-keras](I therefore submitted another question, but much more specific with my problem.) I show there how my input/target data and the models look like. I really feel I'm missing something here.
Comment: In 'Keras Tutorial', there is a 'teacher forcing' using 'decoder_input_data', which is same as 'target_data' offset by one timestep. This means use 'target' as input feature as well. In the "MachineLearningMastery Tutorial", It does not seem to have this 'teacher forcing' element. Can you explain it a bit more? Thank you very much!

Seq2Seq with Keras understanding


Increasing ulimit open files not applied in RHEL 8.1

Question: I have a fully-updated Red Hat Enterprise Linux (RHEL) 8.1 x86_64 system where, as a local user in a local terminal (i.e. not remotely via SSH), when I run ulimit -Sn it says 1024 and ulimit -Hn yields 4096. I would like to increase those numbers because often when I compile large projects like gcc, the make step would fail with a too many open files error. I've even tried moving the compilation into a podman container, but that didn't help because the container just inherited my local user's ulimits.
Here's what I've tried on the host:

In /etc/security/limits.conf I've added:

*          soft nofile 32767
*          hard nofile 65535
[username] soft nofile 32767
[username] hard nofile 65535

I checked ls -a /etc/security/limits.d/ and there are no files there.
In /etc/sysctl.conf there is a line stating fs.file-max = 65535. I also ran sysctl -p.
I added session required pam_limits.so to the end of the file /etc/pam.d/login.
ls -a /etc/sysctl.d/ showed two files ./  ../  50-libreswan.conf  99-sysctl.conf@, neither contained anything related to the number of open files.
cat /proc/sys/fs/file-max says 65535.
/etc/systemd/user.conf is empty (all lines commented out), and I tried to explicitly set DefaultLimitNOFILE=65535.

After trying the above, when I run cat /proc/self/limits or prlimit, the relevant output line still says:
NOFILE     max number of open files                1024      4096 files

The only clue I have right now is that when I use Gnome's System Activity app to look at my processes, when I have a terminal window open there are two bash processes, both under my local user. Strangely, when I run cat /proc/[process id]/limits on the two processes, one shows the large 32767 limit but the other has the original 1024 limit. I thought of checking my ~/.bashrc and ~/.bash_profile but they don't change my ulimit.
I'm stumped!
What else can I check and try in order to increase my ulimit -n? Could systemd or SELinux have something to do with it? Thank you for your help.
EDIT: Per @Gerrit's suggestion, I ran systemd-cgls which says that practically all things running under my local user are under:
-.slice
├─user.slice
│ ├─user-1005.slice
│ │ ├─session-2.scope
│ │ │ └─[many entries here]
│ │ └─email@example.com
│ │   └─[many entries here]

Answer: In a systemd setup before version 240 all user processes run under default user resource limits suggested by the kernel, which on RHEL 8.1 appears to be 4096 for the hard limit and 1024 for the soft limit. In systemd 240 this changed to set the default hard limit to 512K, the default soft limit remains the same, to avoid problems with programs reserving memory for every potential file descriptor or other compatibility issues.
You can change the default HARD limit of NOFILE in /etc/systemd/system.conf in the [Manager] section with the DefaultLimitNOFILE= setting. A reboot is required to make this effective.
After that you can indeed raise the default Soft limit of NOFILE by setting it in /etc/security/limits.conf with * soft nofile nnnn. Adding pam_limits to /etc/pam.d/login is not required, at least not in Centos 7, as that is already included through /etc/pam.d/system-auth.
Comment: I can only test on Centos 7 for the time being. I find that I can raise the HARD limit by setting DefaultLimitNOFILE= in [Manager] section of /etc/systemd/system.conf AND doing a reboot, no luck with just daemon-reload. user.conf will only apply to processes started with systemctl --user
Comment: If you say user.conf was empty, do you mean it was completely empty? I believe that all directives should  be under a section header [manager]. But it could also be that you should modify system.conf to apply to the system console. And then probably reboot also.
Comment: Perhaps it would be best to first use `systemd-cgls` to see where your shell is located in the hierarchy.
Comment: thanks for your suggestions. I checked `/etc/systemd/user.conf` and it is empty (all lines commented out), and I tried to explicitly set `DefaultLimitNOFILE=65535`, but that didn't help. Anything else I can check/try?
Comment: Running `systemd-cgls` shows me that almost everything related to my local user (e.g. Gnome, Wayland, shell, etc.) are under: `-.slice
├─user.slice
│ ├─user-1005.slice
│ │ ├─session-2.scope`

And under `user-1005.slice` there is also another tree called `│ │ └─firstname.lastname@example.com
` **(this is kind of hard to read so I've edited my original post to make it clear)**
Comment: as for `user.conf`, `system.conf` and the other `*.conf`s I saw, they were empty in that there is indeed a `[manager]` header, but everything was commented out. I *un-commented* `DefaultLimitNOFILE=` in `user.conf` and changed it to `DefaultLimitNOFILE=65535` to no avail.
Comment: Finally got it to work. I additionally found [this question](https://unix.stackexchange.com/q/366352/1375) and [this question](https://superuser.com/q/1200539/13679) that matches your suggestion, and indeed the limits are generally set by `user.conf` and `system.conf` in `/etc/systemd`. Thank you for pointing me in the right direction! Perhaps your comments can be put into you answer and I'll mark it as solved!
Comment: Ooops I missed your updated answer, thank you so much @Gerrit, marking as solved! :)


Using form elements for various forms in Drupal 7

Question: I am building a site with several forms that partly contain identical building blocks (for example a fieldset 'payment details'). Ideally, I would like to design my form elements separately and then inject them into each form. My idea was to create functions with &$form as parameter, as in the following code:
function payment_details(&$form) {
  $form['payment_details']['amount'] = array(
    '#title' => t('Payment Amount'),
    '#type' => 'textfield',
  );
}

I then call the function in my form definition.
transaction_form() {
  // ...
  payment_details(&$form);
  // ...
  return $form;
}

While the approach leads to the desired result, Drupal gives me the following warning:

Deprecated function: Call-time pass-by-reference has been deprecated in drupal_load() (line 1100 of C:\Wamp\www\paradise\includes\bootstrap.inc).

Maybe anyone can give me a better way to reach my goal? At least, can you guys advise on how to fix this problem? I understand from various forum posts that this is a PHP related issue, and that it can be more or less ignored for development. Do you agree?
Answer: Your function already accepts the $form variable by reference (function payment_details(&$form)) so you don't have to pass the $form element by reference when calling that function. Just use payment_details($form); in your form builder and you should be fine. Passing by reference is deprecated in recent PHP versions, so that's why you get a warning.


Chord Theory: Verse, Bridge and Chorus

Question: I am a beginner/intermediate level guitarist. From time to time I come up with a nice sounding chord progression. I have little knowledge of music theory, and I am struggling to make this chord sequence evolve into a bridge or a chorus. I see small pieces of answers to other questions but usually applied to a specific example.
Is there any way to, given a chord progression, methodically deduce a bridge and a chorus (probably multiple ways)? Are these methods depend on the scale we are in? The key? The starting chord progression?
I understand in music there is never a magical formula, but does theory provide an easy-to-use framework to start with?
Answer: There are a couple of well used ideas that feature. A verse will end on the key chord, often use the dominant seventh version of that chord, to lead into the chorus starting on IV. The end of that chorus will usually finish on V (or V7) which in turn leads back to I for the start of another verse.
Another idea can work in two ways. Either going from minor in the verse to relative major for the chorus, or the opposite way, if the verse is in Am, for example, the chorus goes to C major.
Those are only a couple of examples - the best way to answer the question is to do a lot of listening to already existing songs, and work out what happens in them. The key a song is in has no impact as to what chord pattern works - although usually those chords involved are diatonic - as in 'from that key'. Sometimes with the addition of V of V, to signal a slight modulation.
One other idea is using chords from the parallel key.
So, in C, for instance, there could be a pool of - C Dm Em F G Am Bo, D (V of V) Cm Do Eb Fm Gm Ab Bb, and E (V of Am). That's got to be enough to make several songs!

Chord Theory: Verse, Bridge and Chorus


activating function only when tkinter button clicked on Python

Question: I have made a function called get_data['name'].
This function gets a string('name' for example) and the output is an array of number.
I have made few button with tkinter that looks like this:
# A Button
A_Button = Button(GUI, image=A_Im, command=get_data('a_name'))
A_Button.grid(column=0, row=1)

# C Button
C_Button = Button(GUI,image=C_Im,command=get_data('c_name'))
C_Button.grid(column=0, row=2)

# D_Button
D_Button = Button(GUI, image=D_Im, command=get_data('d_name'))
D_Button.grid(column=0, row=3)

However, when i run the code, It executes the function get_data for every button, while i wanted it to work only when i click the specific button.
How can i make it work only at click?
Thank you.
Comment: I'm not familiar with tkinter, but from the looks of it you're adding the command get_data to every button. Is that intended?
Comment: Yes, each button should call the function but with different input.
Answer: The problem is that the way you're assigning a value to the Button's command argument, will first immediately call the get_data function. This is because you're writing it as a regular function call. In the end you end up assigning the return value of the actual function call to the command argument of the Button.
Instead what you need to to is to make sure you are assigning an actual function (or in general, any callable object) to the command argument. This makes sure it will be lazily evaluated and only invoked at the time you're actually clicking on the button.
A simple way to do this is by wrapping it in a small lambda function like this:
C_Button = Button(GUI,image=C_Im,command = lambda: get_data('c_name'))

Comment: It worked, thank you very much.