Why does my RNN perform well on long sequences, but not on the short, easy ones?

Question: I have a problem that has left me perplexed and baffled. I am training an RNN (with LSTM cells) to generate a sequence from the output of a CNN for the purpose of annotating images. E.g.,
input: picture of an orange
output -> "fruit"
input: picture containing: apple fish banana broccoli
output -> "fruit meat fruit veggie"
The sequences vary in length between 0-20 words. My model seems to be performing very well when there are ~7+ inputs (60%+ accuracy), but almost completely incorrect for shorter sequences (<7). How can this happen? I would think that if it is good at recognizing "orange" in a long sequence, a single sequence with only the word "orange" should be a piece of cake, since it can reuse that same, learned pattern regardless of whether the input is long or short.
One note: There are very many more long examples in the training set (naturally, since there are exponentially more possible combinations). But rather than augmenting the training set, I'm curious as to why the easy, short sequences don't turn out to be easy at all. Any ideas?
Comment: What is the error on the training set? On the hold out set? If the training error is large, it might indicate overfitting, you might need a more complicated model. May be some parameter tuning as well.
Comment: True, I'm not over fitting yet, so I could make the model larger, but the question remains: why does the easy input require more resources than the longer sequences and not the other way around?
Answer: My hunch is that this is because you also have a  language model (implicitly) trained which might be helping in your case.
I guess you are trying to perform image captioning. Taking a concrete example,  an image of a diaper will always typically occur with the image of a baby but not vice versa.
Assume that images of only diapers are quite rare but those of only babies are numerous in your dataset and do can be learnt easily. Then captioning "Baby wearing diaper" might be easier than captioning just "Diaper" . Assumption here is that your network is able to recognize a baby well.
@jstaker7 If you can post some images on which the network works well and some on which it doesn't it'll be easier to dig deeper
Answer: Your input does not seem to be sequential (I.e in the output the $t^{th}$ word seems to be independent of $t-1^{th}$). Using LSTMs might be an overkill for this problem.
To your question on why performance deteriorates-can you indicate what's the overall structure of your network. Are you using an encoder-decoder kind of framework? Knowing this will help provide a better answer.
Comment: My apologies, I tried to give a simple example but that seems to cause more confusion. It's actually a CNN+RNN to annotate images and the outputs are not independent.
Comment: @jstaker7  to make my earlier question clear- do you start generating output at step 0 in the RNN or do you feed in the entire sequence first and then start decoding?
Comment: The entire input is read in first (encoded to a state vector) which the output is then decoded from.


Split a txt file into N lines each?

Question: I would like to split a very large .txt file in to equal parts files each part containing N lines. and save it to a folder
with open('eg.txt', 'r') as T:
    while True:
        next_n_lines = islice(T, 300)
        f = open("split" + str(x.pop()) + ".txt", "w")
        f.write(str(next_n_lines))
        f.close()

But this creates a files with data
" < itertools.islice object at 0x7f8fa94a4940 >"

in the txt files.
I would like to preserve the same structure and style maintained in the original txt file.
And this code does not terminate automatically when it reaches end of file as well. If possible I would the code to stop writing to files and quit if
there is no data left to write.
Answer: You can use iter with islice, taking n lines at a time using enumerate to give your files unique names.   f.writelines will write each list of lines to a new file:
with open('eg.txt') as T:
    for i, sli in enumerate(iter(lambda:list(islice(T, 300)), []), 1):
        with open("split_{}.txt".format(i), "w") as f:
            f.writelines(sli)

Your code loops forever as you don't include any break condition, using iter with an empty list will mean the loop ends when the iterator has been exhausted.
Also if you wanted to pass an islice object to be written you would just call writelines on it  i.e f.writelines(next_n_lines), str(next_n_lines).
Comment: For anyone finding this years down, you need to put this above the code. https://docs.python.org/3/library/itertools.html#itertools.islice
Answer: The problem is tat itertools.islice returns an iterator and you are writing it's str in your file which is the representation of functions in python (showing the identity of object):
< itertools.islice object at 0x7f8fa94a4940 >

As a more pythinic way for slicing an iterator to equal parts, you can use following grouper function, which has been suggested by python wiki as itertools recipes:
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

You can pass your file object as an iterator to function and then loop over the result and writ them to your file:
with open('eg.txt', 'r') as T:
    for partition in grouper(T,300):
        # do anything with `partition` like join the lines
        # or any modification you like. Then write it in output.

Split a txt file into N lines each?


two css files are conflict

Question: My CSS3 Menu Bar is conflicting with the bootstrap.min.css
If I remove the bootstrap css then menu bar looks like below

Once bootstrap menu is added then it become like below

Instead of alphabet letters such as Menu1, Menu2, I m using some images on the menu. Because of CSS conflict, menu images comes below the menu border line. I got the image like below (but I want the image should be in center)

<link rel="stylesheet" type="text/css" href="Style/css3menu1/style.css" />
<link href="Style/vTab/bootstrap.min.css" rel="stylesheet" />

I have linked the both css like above and copied those codes in jsfiddle. I could not find the conflicting part in the CSS. Can anyone please help me.
jsfiddle sample is here...
    https://jsfiddle.net/w3jz075t/
Comment: If you want to find the correct css element then use the Developer Tools inside any browser (preferably Chrome). Here is the video: https://developers.google.com/web/tools/chrome-devtools/iterate/inspect-styles/animations/edit-element-name.mp4 . You hit CTRL+SHIT+C and click on the icon where it's not placed correctly and then the rest would be easy to fix. You can make local changes inside that window until you find the correct solution and then you can apply it in your css file.
Comment: Is there any reason why you're not using icon fonts like fontawesome?
Comment: i didnt know about that icon fonts...let me try that...thank u
Comment: there are many others you can use like glyphicons
Comment: Why don't you use the Bootstrap Navbar? https://getbootstrap.com/examples/navbar/
Comment: I would suggest you to take a look at this page. https://getbootstrap.com . I think you're not fully aware of possibilities inside Bootstrap.
Comment: its not an issue with the bootstrap. its just conflict of two css, i would like to know the line that conflict each other
Comment: @Aruna: I think you can simply solve this issue if you reverse the order of your css file. So add the bootstrap css file above and your style.css file below bootstrap css file and you should not have any issues....let me know.
Comment: @navnit style.css is in master page and the other one in content page
Comment: @Aruna: if you are not able to place the bootstrap file above your style.css file then I guess you need to check that manually like CodeMaster suggested..
Answer: The order in which you include your CSS files is important, they will load in that order.
If there's 2 lines that impact the same html element, the browser will take the one included last (except if you use the attribute !important).
Comment: thank you! but style.css is in master page and the other one in content page so nothing to change here
Answer: Have you tried linking the css files like this:
<link href="Style/vTab/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="Style/css3menu1/style.css" />

(in reversed order)
so your css styles will apply after the bootstrap ones.
Keep an eye on !important after some css properties when troubleshooting with the dev tools!
Good Luck!
Comment: thank you! but style.css is in master page and the other one in content page so nothing to change here
Comment: @Aruna OK (: Glad you've fixed your problem anyways! Have a nice evening!
Answer: I could not reverse the order due to Master page & content page. Finally I have concluded with the issue and identified the conflict code in bootstrap css, its below
/*
@media print
{
*
{
text-shadow:none!important;
color:#000!important;
background:transparent!important;
box-shadow:none!important
}

a,a:visited
{
text-decoration:underline
}
a[href]:after
{
content:" (" attr(href) ")"
}
*/

really I dont know what they did and how they are root cause for the issue but if I comment those lines then my menu bar places correctly. Thank you all for your support.
Comment: These lines normally only apply to print media, which means they have no effect on your screen render. You should also be carefull what you comment, it seems like you don't include the closure ('}') of the '@media print', it could result in more issues ;)

two css files are conflict


Holomorphic action of $C_{2}\times C_{2}$ on $\mathbb{P}^1$

Question: I am looking for a holomorphic action of $C_{2}\times C_{2}$ on $\mathbb{P}^1$. Is it true that there is a unique effective action given by
$$
a:z\rightarrow -z, \ \ \ \ b:z\mapsto 1/z
$$
up to change of variable? Here $a$ is the generators of the first (second) factor of $C_{2}\times C_{2}$.
What about $C_{3}\times C_{3}$? I could not find any effective action.
Comment: Isn't $z=-z$ in $\Bbb P^n$? In which case $a$'s action is trivial. Or am I missing somehting?
Comment: He uses $z$ as an inhomogeneous coordinate $[z,1]\in \mathbb{P}^1$.
Answer: It is true. Any finite group of automorphisms can be conjugated to that it becomes a subgroup of the group of rotations of the Riemann sphere, and there is a classification of finite subgroups (up to conjugation) of $SO(3)$, containing $C_2\times C_2$ just once (as the group of rotations by $\pi$ around the coordinate axes).


How to restrict completion of only one field within a field group

Question: Is there a simple way to have several fields within a field group; such a way that only one field in the group can be populated.
Example use Case
Field group is called 'Insert Media'.
The group contains
- an image field
- an audio field
- a video field
The user may complete one field, but can not complete multiple fields within the group.
Answer: I have not expressly done the exact use case you've requested. I recommend you look at the following blog post describing the Drupal States API, including newer additions made to that API for ORing and XORing conditions:
http://www.metaltoad.com/blog/drupal-7-form-api-using-states-multiple-conditionals-and-or-and-xor
For simple multiple-condition clauses the States API follows this convention:
'[field-selector]' => array('[evaluate]'),
'[field-selector2]' => array('[evaluate]'),

For more complex conditional logic you can write the following array structure:
array('[field-selector]' => array('[evaluate]')),
'[OR/XOR]',
array('[field-selector2]' => array('[evaluate]')),

Be aware that the version of jQuery being used on your site could be important for these conditionals to work (again see the blog post).
If the above does not meet your needs I recommend you look at the Field Conditional States module:

The working states that are supported by FCS are:

visible / invisible - Hides/Shows a form element
enabled / disabled - Grays out a form element
required / optional - Makes a form element required to be able to submit the form
checked / unchecked - Checks/Unchecks a checkbox (of course only applicable to checkboxes)

Fields to which these states will be applied, are called target fields
  or controled fields.
To trigger these states there is a different set of trigger states
  (not to be confused with the states described above) that will trigger
  a state when applied to a control field:

empty - Triggers when the control field is empty
filled - Triggers when the control field is not empty
checked - Triggers when the control field is checked
unchecked - Triggers when the control field is not checked
value - Triggers when the control field's value is equal to a given string
!value - Triggers when the control field's value is NOT equal to a given string

All of them are supported as long as they make any sense for the
  widget of the used control field. For example it doesn't make any
  sense to wait for a text field to be checked or for a checkbox to have
  the value "Hello World". So most (if not all) fields will only use a
  subset of these trigger states.

Note: that the states api and the field condition module are clientside modules -- they don't enforce the validation on the server. You'll likely also want to use a custom form validation function to assure that only 1 field is provided upon form submission.
Comment: Very interesting and certainly a valid solution, but not so good for working with horizontal tabs. Maybe I need a rethink. Thank you.
Comment: no problem, in further questions you may want to provide further details such as "i'm trying to not show certain tabs in my form(s)" ... although some meta programming should be able to hide a tab group if all the conditions in the above examples are met. It just sounds like sufficient javascript-fu is needed. I mean an html form is an html form and its just logic. "tabs" are simply a visual tweak usually achieved with JS and CSS.
Comment: I have gone with your suggestion and flagged my question as answered. I really appreciate your help.


Jetpack compose with RxJava2 and Realm

Question: I am working my through a new android application using Jetpack Compose (1.0.0-alpha08) and RxJava2 to manage the flow of data from my model (Realm 10 in this case. For a given screen, I have a view model that defines the data that will be subscribed to by the top level Compostable view. So, for example:
ViewModel...
class ListItemViewModel: ViewModel() {
   val items: Flowable<Item>
       get() {
          val data1 = userRealm.where<Item1>()
             .also(query).findAllAsync().asFlowable()
             .onBackpressureLatest().doOnNext{System.out.println("Realm on Next")}
            .observeOn(
                Schedulers.single()
            ).filter{it.isLoaded}.map{ result ->
                System.out.println("Maping Realm")
                result
            }.doOnSubscribe {System.out.println("Subscribe")}
          val data2 == //same as above but with a different item

          return Flowable.combineLatest(data1, data2, combineFunction)
             .onBackpressureLatest()
             .doOnNext{System.out.println("Hello")}
             .doOnComplete {System.out.println("Complete")}
             .subscribeOn(AndroidSchedulers.mainThread())
       }
}

View
@Compostable
fun List(List<Item> items) {
   val viewModel: ListItemViewModel = viewModel()
   val list by viewModel.items.subscribeAsState(initial = listOf())
   ItemList(list = list)
}

@Compostable
fun ItemList(List<Item> items {
   LazyColumnFor(...) {
     .......
   }
}

Everything works as I would expect and the list renders on the screen as I want. However, what I assume would happen here is that the subscribe would only happen once and the Flowable would only push out new data as new data was emitted. As a result, I would only expect the various onNext methods to be triggered when new data was present in the stream, e.g. something changed in the realm db. As I am not adding/deleting any data to/from the Realm, once I have the first set of results, I would expect the flowable to go "silent".
However, when I run the above, the subscribe message related to the realm subscription is logged over and over. The same for the "Hello" and the other logging statements in the onNext methods. Also, if I add any logging in my combine function, I see those log statements in the same fashion as I see the "Hello" log. From this it seems like each time the List composable is being rendered, it resubscribes to the Flowable from my viewmodel and triggers the full process. As I said, I was expecting that this subscription would only happen once.
This is perhaps correct behaviour, but mentally, it feels like I am burning CPU cycles for no reason as my methods are being called over and over when no data has change. Am I setting things up correctly, or is there something flawed in how I have configured things?
Answer: I ultimately worked around the problem and took a hybrid approach where I used Realm/RXJava to handle the data flow and when things have changed, update a LiveData object.
View Model
private val internalItemList = MutableLiveData(listOf<Item>())
val itemList: LiveData<List<Item>> = internalItemList

//capture the subscription so you can dispose in onCleared()
val subscription = items.observeOn(AndroidSchedulers.mainThread()).subscribe {
    this.internalItemList.value = it
}

View
val list by viewModel.itemList.observeAsState(listOf())

This is must less chatty and works as I want it to. Not sure if it is the correct way to do this, but it seems to be working

Jetpack compose with RxJava2 and Realm


Bobby McFerrin Psalm 23

Question: Bobby McFerrin's Psalm 23 in Measures 7 & 15 does the E flat in the bass line make the tenor an E flat also?
Answer: We could argue whether the bass Eb on beat 1 affects the tenor note on beat 4, particularly as the notation doesn't use seperate 'stems up' and 'stems down' voicing.   But I think it was sensible of the composer or editor to make it clear!    Does your version not have the Enat written?

Bobby McFerrin Psalm 23


Find inverse operator

Question: Let $D=\dfrac{d}{dx}.$  Consider the operator
$$
D_{h,x}=\frac{e^{hD}-1}{h}.
$$
Question.  What is explicit form of the operator $D^{-1}_{h,x}?$
I think that
$$
D^{-1}_{h,x}=\frac{h}{e^{hD}-1},
$$
is wrong answer.
Comment: You might want to look at how these ops are related to the exponential generating function for the  Bernoulli numbers and to the Euler-Maclaurin formula.
Answer: If $\mathcal{I} = D^{-1}$ then wouldn't
$$
D^{-1}_{h,x}[f] = \mathcal{I} \left[ \frac{\ln(fh+1)}{h} \right],
$$
because then
$$
D^{-1}_{h,x}\left[D_{h,x}[f]\right] = f?
$$
Answer: I think it is a correct answer: $$e^{hD}f = \sum \frac{h^n}{n!}D^n f = f(x+h)$$ so $$D_{h,x}f = \frac{f(x+h)-f(x)}{h}$$
Given $$g(x) = \frac{f(x+h)-f(x)}{h}$$ we may reconstruct $f(x)$ as $$f(x) = h\sum_{n=1} g(x - nh)$$
Now $g(x - nh) = e^{-nhD}g$, that is $$f = h\sum e^{-nhD}g = h\sum (e^{-hD})^n g= h(\frac{1}{1 - e^{-hD}} - 1)g = \frac{h}{e^{hD} - 1}g$$
All convergence considerations aside, of course.
PS: I don't know what I was thinking before. Thank @Leox for pointing out.
Comment: but in yours notation I got  that $$g(x)=\frac{f(x+h)-h f(x)}{h}$$
Comment: @Leox I don't see how you got that. Can you demonstrate?
Comment: Suppose  that $$f(x) = \sum_{n=1} h^n g(x - nh),$$ or $$f(x)=hg(x-h)+h^2g(x-2h)+h^3g(x-3h)+\cdots.$$ Then $$f(x+h)=hg(x)+h^2g(x-h)+h^3g(x-2h)+\cdots.$$ Thus $$\frac{f(x+h)}{h}=g(x)+hg(x-h)+h^2g(x-2h)+h^3g(x-3h)+\cdots=g(x)+f(x).$$ It follows now that $$g(x)=\frac{f(x+h)-hf(x)}{h}$$  but  not $$g(x)=\frac{f(x+h)-f(x)}{h}$$ as was promised.
Comment: @Leox Than you. I don't know what I was thinking. Surprisingly the result holds.
Answer: The op $D^{-1}D_{h,x}$ and its inverse $DD_{h,x}^{-1}$ are classic operators associated with the Bernoulli numbers (and polynomials) and the Euler-Maclaurin formula.
Comment: See, for example, https://tcjpn.wordpress.com/2014/12/10/appells-for-the-bernoullis/#more-698 . This has more detail than you would probably like or need but indicates the importance of these operators. Or, for conciseness, see Henry Cohn's answer to http://mathoverflow.net/questions/61252/why-do-bernoulli-numbers-arise-everywhere .
Comment: Be careful not to use confusing terminology. Inverse operator is not equal to inverse function. The inverse op is generally a multiplicative inverse and the inverse function, either a multiplicative or compositional inverse.
Comment: what kind of the association? The e.g.f. for the Bernoulli numbers is $$\frac{h}{e^h-1}.$$ How it  can helpto get the inverse function?
Comment: Also, Cohn is a little lax in his notation.
Answer: $D^{-1}_{h,x}(f) = e^{-h\frac{d}{dx}}(hf+1)$

Find inverse operator


Flex: LinkButton select=true has no visible effect

Question: I would like to have a LinkButton programmatically selected.
So I have a code like this:
<mx:LinkButton label="hello42" selected="{isHello42}"/>
isHello42 is true but the LinkButtons doesn't look like it's selected.
Do I have to change the complete skin to achieve that the LinkButton look like it's selected?
Here it look like setting selected=true should work: http://blog.flexexamples.com/2008/09/06/creating-a-toggleable-linkbutton-control-in-flex/
Answer: Read your mentioned article on blog.flexamples.com accurately! It's answering your question by itself. The desired behavior is achieved by extending the mx.skins.halo.LinkButtonSkin.
Comment: I should've mentioned that I'm using the spark theme, when using the `mx.skins.halo.LinkButtonSkin` there is no "background" on the LinkButton in default state. Neither changes the background at "MouseOver"...
Comment: I missed the wood for the trees, just extending this skin. I'm sorry :)


How do I add data from other models to a view in Laravel?

Question: Scenario:
A question is generated in a previous view page by the user. For example: "Question 1". This inputted question needs to appear on a future create page so the respondent can see it. The answers follow it.
I am unsure how to link this up. How do I get data from the "question" table to appear on the "respondent" create page?
I have aimed to set up relationships between the Respondents and Questions classes as well as edit the controllers and view/create pages.
Respondent Model:
public function questions()
{
    return this->hasMany('App\questions');
}

Question Model:
public function responses()
{
    return this->hasMany('App\respondents');
}

QuestionsController:
public function index()
{
    $question = questions::all();
    $questionnaire = questionnaires::all();
    return view('question.index', compact('question', $question, 'questionnaire', $questionnaire));
}

RespondentsController:
public function index()
{
    $question = questions::all();
    $respondent= respondents::all();
    return view('respondent.index', compact('question', $question, 'respondent', $respondent));
}

Respondent Create Page:

    @foreach($question as $insert)
        {{$insert->question1}}
        <input type="radio" name="response1" value="1">Agree
        <input type="radio" name="response1" value="2">Disagree
    @endforeach

Currently, I am getting the error: "Undefined variable: question".
In hindsight, the question should appear above the two radio buttons.
Thank you in advance for any help!
Comment: Can you add the code for the RespondentsController `create` action to your question
Answer: It should be like this.. because compact behind the scenes passes the variable which it takes as string
public function index()
    {
      $question = questions::all();
      $respondent= respondents::all();
      return view('respondent.index',compact('question', 'respondent'));
    }

Or you can do like this.
public function index()
    {
      $question = questions::all();
      $respondent= respondents::all();
      return view('respondent.index')->with(['question' => $question, 'respondent'=> $respondent]);
    }

Comment: Thank you, but the error is still there: "undefined variable: question"
Answer: The issue is here:
return view('respondent.index',compact('question', $question, 'respondent', $respondent));

the second parameter of view() is an array.
Either use:
return view('respondent.index',compact('question', 'respondent'));

compact create an array from passed variables
or
return view('respondent.index',['question' => $question, 'respondent' => $respondent]);

Comment: Thank you, but the error is still there: "undefined variable: question"


Laravel with postgresql db cant insert value to table and get last inserted id with eloquent

Question: Laravel Framework 5.4.36 | psql (PostgreSQL) 11.2
Hi, now I want to migrate my existing Laravel project DB from MySQL into PostgreSQL, I have some trouble here
$insert = [
   "a" => "name",
   "b" => age
   .
   .
];

return DB::table('table')->insert($insert);

when I try to insert DB with array data it returns like this,
Undefined column: 7 ERROR:  column "0" of relation "table" does not exist ..
LINE 1: insert into "table" ("0", "1", "2", ..

it not return "a","b","c".. column but array position. with MySQL DB it is working

and
i try to get last inserted data id like this,
$data = [
   'name'=>$name,
   'address'=>$address,
   'id_class'=>$id_class,
   'lat'=>$lat,
   'long'=>$long,
   'is_changed'=>true
];
return Model::create($data)->id;

this will return undefine for id value

i update my code, 1st problem already resolved with this:
$insert[] = [
   "a" => "name",
   "b" => age
   .
   .
];

foreach(array_chunk($insert,5000) as $i){
  return DB::table('table')->insert($insert);
}

Comment: try to do `$model = Model::create($data); return $model->id`
Comment: @Saromase still undefine
Comment: i think you need to define a primary key in the model protected $primaryKey = 'id';
Comment: @AhmedAboud not working too
Comment: So you want to create db table `table` without having defined Model for that table?
Comment: no, i dont create db table, but try to calling model and create data with 'create' eloquent @Alex
Comment: Who should create the table `table` for you in this case? Does it exist in DB?
Comment: @Alex yes when i create model from cmd i use --migration, then  run migrate db
Comment: @iskandarsuhaimi in the model     protected $table = 'your_table';
Answer: I once got the same error.
The issue was
Accidentally i put
DB::beginTransaction();

In my auth middleware, forgot to close the beginTransaction by
DB::commit();

that is why, every time i try to insert new value, i get the ID but entry doesn't appear in database.


calculate the road distance from source latitude and longitude to destination

Question: I have used the following code but this gives me the air distance from source to destination, but i require road distance, please help me guys with this.
This work but this air distance but i require street/road distance.
$center_lat =$_POST['lat'];

//19.159519;//
$center_lng =$_POST['lng'];//72.995782;//
$radius = '3963.0';
$range=$_POST['range'];
//echo $center_lng;
// Set the active mySQL database
$db_selected = mysql_select_db("mapdb", $connection);

// Search the rows in the markers table
$query = sprintf("SELECT name,lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM hospitals HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($center_lng),
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($radius));
$result = mysql_query($query);

if (!$result) {
  die("Invalid query: " . mysql_error());
}
 else
 {
  echo "Done";
 }
 // header('Content-type: application/json');
//$output = array();
$i=0;

 while($row = mysql_fetch_assoc($result)) {
     if($range > 0 )
      {
        $theta = $center_lng - $row['lng'];
        $distance = (sin(deg2rad($center_lat)) * sin(deg2rad($row['lat']))) + (cos(deg2rad($center_lat)) * cos(deg2rad($row['lat'])) * cos(deg2rad($theta)));
        $distance = acos($distance);
        $distance = rad2deg($distance);
        $distance = $distance * 60 * 1.1515;
        $distance = $distance *1.609344;
        if($range > round($distance,2))
        {
            $info[$i] = "\"".$row['lat']."&".$row['lng']."@".$row['name'];
            $i=$i+1;
        }
        $myTwitterResult1 = array($info);
    }
    else
    {
        $info[$i] = "\"".$row['lat']."&".$row['lng']."@".$row['name'];
        $i=$i+1;
        $myTwitterResult1 = array($info);
    }
}
    $myJSONTweets = json_encode($myTwitterResult1);
    echo $myJSONTweets;

Comment: there is no magical formula for road distance. first you need a map of roads
Comment: Without doing that i cannot find the distance?
Comment: you can tap into an external API like google maps
Comment: Finding the road distance is a complex task. First you need a map of existing routes with distances (somebody has do measure all this routes). Then you need to find the best route (this is a challenge of its own, the shortes is not always the quickest, the quickest can include uncomfortable ways...). A formula alone won't be able to solve your problem.
Comment: – martinstoeckli so no other way according to you can solve my problem?
Answer: this function may help you
function distance($lat1, $lon1, $lat2, $lon2, $unit)
 {
  $angle = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) *        cos(deg2rad($lat2)) * cos(deg2rad($angle));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
   return ($miles * 1.609344);
} else if ($unit == "N") {
  return ($miles * 0.8684);
} else {
    return $miles;
  }
 }
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers
";

more info here
Comment: -Rajeev Ranjan I have used the same function in my code, you can see it, i dont want this . This returns me the air distance.
Answer: If you are displaying a Google Map (read the Terms of Service)
Use the Directions Web Service or the Distance Matrix Web Service
If you are using the Google Maps API v3 to display a map, both have equivalent services in the API.
Comment: -geocodezip i dont want to display map, i just want the values


Acoustic guitar chords' positions vs those of a Bass guitar

Question: I will appreciate who can acknowledge me about the differences of chords on an acoustic guitar's fret board with that of a 4-string Bass guitar.
How could we perform one's chord on another's and how is it possible to harmonize them?
Comment: The most common approach is to not play chords at all on the bass guitar, and simply play a monophonic bassline.
Comment: Standard tuning for a 4-string bass is the same as the bottom four strings of a guitar in standard tuning, so any chord shape that you can move to the bottom four strings on a guitar can be played directly on the bass; but you might end up with impossible stretches or muddy chords this way. What do you mean with "how is it possible to harmonize them?"
Comment: I think the bass clarinet is relevant here. In *The Technique of Orchestration*, Kent Kennan says something along the lines of that there isn't much point playing the higher notes of the bass clarinet since the "regular" clarinet can play those much better.
Comment: OK, thanks! I got it! so what can we do to solve "ending up with some muddy chords"? I mean, is there any way to come up with better-voiced chords?
By the "possibility to harmonize them", I wanted to ask; how to chose the best chords on both instruments to get the most beautiful action as a united instrument?
Comment: Note that it isn't uncommon for bassists to play double stops instead of full chords.
Answer: The standard tuning of the bass strings are the same as the bottom four of standard guitar tuning - E, A, D, G - but one octave lower. The tones of chords are the same between the instruments except they sound one octave lower on bass. So A2 E3 A3 C#4 on guitar will sound A1 E2 A2 C#3 on bass guitar, and the fretting can be the same.
In terms of how to perform and "harmonize" them, a typical way that guitar an bass work together is for the guitar to strum the full chord while the bass plays single notes. If the guitar strums an A chord, the bass may play single tones A and E - the root and fifth of the chord - or it could arpeggiate the chord in single tones like A C# E C#.
One reason the bass plays single notes is because in the lower register playing simultaneous tones sounds "muddy". If you play higher - somewhere around the seven fret on the top three strings - the combined tones become clearer. A lot depends on exactly what is being played.
Rhythmically the bass and guitar parts can differ. If the bass plays straight quarter notes the guitar might strum with some upbeat rhythms. But a whole range of two part playing can be used from playing the exact same part in octaves to completely independent parts.
Answer: Bassically, the bass guitar wasn't designed or expected to have chords played on it. The notes are low, and even playing a simple triad often doesn't sound good. Muddy describes it well.
So, using a guitar and a bass, it's best to stick to one note on the bass, and the chord, or the rest of it, on the guitar. Thus, the bass could play one of the triad notes - usually 1, 3 or 5, with 1 being the best as it's the root note.
If you really want to play chords on a bass guitar, then it's possible. For instance, playing the root on bottom string, appropriate fret, and 3rd on the top string, one fret higher (for a major chord), or 3rd on same fret (for a minor chord). Only a two noter, but it sounds fine.If you wanted to put the 5th in, it could go on the A string, two frets higher than the root note's position.
As David states, the bottom 4 strings on guitar are tuned as the bass is, so any notes fingered as on those 4 strings on a guitar can be fingered the same on bass, with the understanding that the lower frets on bass tend to be bigger, and the sound will not usually be good.
Comment: you may find using lighter strings more helpful on bass to get the notes to ring out more clearly. I've never done more than double stop chords, and those _sparingly_. One approach that might work, especially in rock, is for the guitar to play the root and fifth parts and for the bass to weave around the third only (so no mud with the fifth or root). Works in ACDC
Answer: As stated above, playing riffs of single notes on the bass is the primary use of the instrument (either finger-style or picked). Certainly that has been my experience having played the bass guitar in gigging bands for about 35 years. "Chords" are possible high on the neck. I use the term loosely on 2-note chords above the octave or riffs of notes that are plucks and left to sustain into a "chord" for a few beats. I'm thinking of the bass intro to Steely Dan's "Peg" for the high neck 2-note "chords" or the intro to Aerosmith's "Sweet Emotion" for the high-neck riff of notes that is left to sustain a bit.
But adding to the comments above with regard to the harmonizing question, I would also mention the style of music being played would also contribute to harmonizing the bass with guitar. If we look at a guitar chord like an A-major which is A-C#-E, I think of that as a 1-3-5 chord (the whole major octave being 1-2-3-4-5-6-7-8). A being the 1, C# being the 3 and so on. So for a country tune, I might bounce back and forth in time between the 1 and 5. For a 12-bar blues, a "walking bass" might be appropriate that traverses chromatically up and down a 1-3-5-6-7b (possibly like "Pride and Joy" by Stevie Ray Vaughan). For a dance or disco type tune, an octave bounce between the 1 and 8 is common, even doubling up over the primary guitar or keyboard riff like "Disco Inferno" by the Trammps. Ballads typically call for single notes that are designed to directly harmonize and enhance the treble instrument. I'm thinking of "Color My World" by Chicago. An excellent application of harmonization as an enhancement between the bass and the piano leaving the pianist free of having to even touch the bottom half of the keyboard. Hope this helps.


How do I have an Apache site in a subdirectory, when the root is ProxyPassed to something else?

Question: I have shellinabox running on a subdomain through HTTPS and using HTTP Basic Access Authentication using the vhosts file below:
<VirtualHost xx.xx.xx.xx:80>
        ServerName              host.bananas.net
        Redirect                301 / https://host.bananas.net
        CustomLog               ${APACHE_LOG_DIR}/access.log combined
        ErrorLog                ${APACHE_LOG_DIR}/error.log
</VirtualHost.net>

<VirtualHost xx.xx.xx.xx:443>
        ServerName              host.bananas.net
        SSLEngine               On
        SSLCertificateFile      /etc/apache2/ssl/bananas.net.crt
        SSLCertificateKeyFile   /etc/apache2/ssl/bananas.net.key
        SSLCertificateChainFile /etc/apache2/ssl/sub.class1.server.ca.pem
        SSLCACertificateFile    /etc/apache2/ssl/ca.pem
        SetEnvIf                User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
        ProxyRequests           Off
        ProxyPass               / http://localhost:4200/
        ProxyPassReverse        / http://localhost:4200/
        <Location />
                AuthUserFile    /etc/apache2/.htpasswd-secure
                AuthName        "Go Away!"
                AuthType        Basic
                Require         valid-user
                Order           Deny,Allow
                Allow           from all
        </Location>
        SetEnvIf                Request_URI "^/u" dontlog
        HostnameLookups         Double
        CustomLog               ${APACHE_LOG_DIR}/ssl_request.log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
        ErrorLog                ${APACHE_LOG_DIR}/error.log
</VirtualHost>

I currently have another site on another subdomain, using this vhosts file:
<VirtualHost xx.xx.xx.xx:80>
        ServerName              files.bananas.net
        DocumentRoot            /var/www/files.bananas.net/

        CustomLog               ${APACHE_LOG_DIR}/access.log combined
        ErrorLog                ${APACHE_LOG_DIR}/error.log
</VirtualHost>

I would prefer to have the second site available at https://host.bananas.net/files instead of http://files.bananas.net.
The main reason for this is so that I can use the same SSL certificate, but there are other reasons as well.
How do I move the second site to be a subdirectory of the first site?
Answer: Exempt /files from the proxying:
ProxyPass /files !

Put it above the root's ProxyPass.  Looks like you'll also need to adjust your DocumentRoots or file structure as needed to serve the content from the SSL vhost.
An example configuration:
<Proxy *>
        Order deny,allow
        Allow from all
</Proxy>

ProxyRequests           Off
ProxyPreserveHost       On
<Location "/">
        ProxyPass               http://localhost:8080/
</Location>
<Location "/files">
        ProxyPass               !
</Location>

Comment: What should I make the `DocumentRoot`, now that the site within `/files/` is not at the root?  What needs to run is `https://bananas.net/files/index.php`.
Comment: If you want to keep the `files.bananas.net` host in place, then just point at that location in the directory structure; `Alias /files /var/www/files.bananas.net`.  Your authentication config from `` will still be applying, too; if that's not desired then undo that config in a `` block.
Comment: I have already moved the site's files to `/var/www/host.bananas.net/files/`, which I prefer.  How should I set a `DocumentRoot` for `https://bananas.net/files/index.php`?  Thanks.
Comment: `DocumentRoot /var/www/host.bananas.net` should do the trick.


How to get value of generated Calendar ID and then push it to a variable based on its row index?

Question: OBJECTIVE

Create booking system using Google form, sheets and calendar
When there is a new submission, calendar event is automatically created.
Respondent can also edit their submission (Edit form URL).
When edited, old event will be deleted, and then new event will be created.

CURRENT PROBLEM

When a row is edited, the updateCalendar() function will also loop through the whole data range; deleting event from the first row.

How to make sure that the function just work on a row that is being edited?
This is what the first row of the sheet looks like.

Timestamp
Email Address
name
Check-in date
Check-out date
Room
No. of people
total day
total
edit URL
Event Conflict
Event ID

TRIAL & ERROR
Currently, I am struggling with this piece of code that will:

Push generated edit URL (from other function) to a variable urls
Push generated event ID to a variable ids
Matching the index of urls and ids, assign it to a variable resultIds and delete.

This is part of the code that will do the above functions
var urls = [], ids = [], resultIds = [];
urls.push(values[i][9]);
ids.push(newEventId);
resultIds.push([values[i][9]?ids[urls.indexOf(values[i][9])]:'']);

This is the full function that will update the calendar: (any help is greatly appreciated, thanks in advance)
    function updateCalendar(request) {
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var range = sheet.getRange(2,1,lastRow,13);
var values = range.getDisplayValues();
var calendar = CalendarApp.getCalendarById('email@example.com');
var urls = [], ids = [], editIds = [];
getConflicts(request);
for (var i = 0; i < values.length; i++) {
  urls.push(values[i][9]);
  if (request.eventConflict == "conflict" && values[i][12] != "sent") {
    sheet.getRange(lastRow,11).setValue("conflict");
    break;
  } else if (request.eventConflict == "approve" && values[i][12] != "sent") {
      var newEvent = calendar.createEvent("booked", request.date, request.endTime);
      var newEventId = newEvent.getId().split('@')[0];
      sheet.getRange(lastRow,11).setValue("approve");
      sheet.getRange(lastRow,12).setValue(newEventId);
      ids.push(newEventId);
      break;
    } else if (request.eventConflict == "conflict" && values[i][10] == "approve"  && values[i][12] == "sent") {
      editIds.push([values[i][0]?ids[urls.indexOf(values[i][0])]:'']);
      var eventEditId = calendar.getEventSeriesById(values[i][11]);
      eventEditId.deleteEventSeries();
      sheet.getRange(i+2,11).setValue("");
      sheet.getRange(i+2,13).setValue("");
      getConflicts(request);
      if (request.eventConflict == "approve") {
        var newEvent = calendar.createEvent("booked", request.date, request.endTime);
        var newEventId = newEvent.getId().split('@')[0];
        sheet.getRange(i+2,11).setValue("approve");
        sheet.getRange(i+2,12).setValue(newEventId);
        break;
        } else {
        sheet.getRange(i+2,11).setValue("conflict");
        break;
   }
  }
}
};

Comment: Can you explain what you mean by `How can I make sure that the sheet will delete event from the right row`?
Comment: Hi Rafa, thank you for the reply.  What I mean by that statement is that, in current version, the next submission will also delete event from the previous submission (maybe it has something to do with the for() loop.

So I am trying to figure out how to get the row index in case of respondent editing their own submission (through edit form)
Comment: keep in mind, we do not see the full picture here, perhaps it was enough for Rafa, but to me it's a little unclear what the "previous submission" is (I was in the process of writing the same comment, but stopped when I saw that Rafa posted it). Is it an unrelated event that gets deleted? The last entry in the spreadsheet? Can you provide a sample data set and what gets deleted that should not.
Comment: Hi Vytautas, thank you for the reply, I just added screenshot of:
1) 4 consecutive new submissions
2) and what happened when I edit submission from the 3rd row.
Comment: Sorry for the late reply, didn't have a chance to sit down and look through the code. The for loop seems a little over-engineered as there is a lot going on there that is not really possible to properly understand from someone else looking at the code. I have no way of knowing which if statements would be met when due to the ambigious `request.eventConflict` value that is not defined anywhere in the code. Also, I see that you try to change `getRange(i + 2, 11)` twice, once to a blank value and then again to `approve` or `conflict`. Is that intentional or did you misplace a closing bracket?
Comment: I managed to solve this problem  by using a second sheet in same spreadsheet, and then use e.range method to get edited row index. But thank you for the reply earlier. cheers m8
Answer: I used two sheets in the same spreadsheet, and then use e.range method to retrieve index of the edited row.  This is the code
function updateCalendarTwo(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var responsename = "Main sheet"
  var copyname = "Copy Sheet";
  var responsesheet = ss.getSheetByName(responsename);
  var copysheet = ss.getSheetByName(copyname);
  var calendar = CalendarApp.getCalendarById('Your Calendar ID');
  // columns on copysheet
  var checkInCol = 4;
  var checkOutCol = 5;
  var roomNumCol = 6;
  var appCol = 11
  var eventIDCol = 12;
  var revCol = 14;

  var response = e.range;
  var rRow = response.getRow()

  var rLC = responsesheet.getLastColumn();
  var cLC = copysheet.getLastColumn();
  var rLR = responsesheet.getLastRow();
  var cLR = copysheet.getLastRow();

  if (rLR > cLR){
    var resprange = responsesheet.getRange(rLR,1,1,rLC);
    var respdata = resprange.getValues();
    copysheet.appendRow(respdata[0]);
    var eventTitle = copysheet.getRange(rRow,roomNumCol).getValue();
    var startDate = copysheet.getRange(rRow,checkInCol).getValue();
    var endDate = copysheet.getRange(rRow,checkOutCol).getValue().getTime()+ 24 * 60 * 60 * 1000;
    var conflicts = calendar.getEvents(new Date(startDate), new Date(endDate));
    if (conflicts.length < 1) {
    var event = calendar.createAllDayEvent(eventTitle, new Date(startDate), new Date(endDate));
    var eventID = event.getId().split('@')[0];
    copysheet.getRange(rRow,appCol).setValue("approve");
    copysheet.getRange(rRow,eventIDCol).setValue(eventID);
    } else {
    copysheet.getRange(rRow,appCol).setValue("conflict");
    }
  } else {
    var resprange = responsesheet.getRange(rRow,1,1,9);
    var respdata = resprange.getValues();
    var copyrespRange = copysheet.getRange(rRow,1,1,9);
    copyrespRange.setValues(respdata);

    var respAppRange = copysheet.getRange(rRow,appCol);
    var respApp = respAppRange.getValue();

    if (respApp == 'conflict') {
    var eventTitle = copysheet.getRange(rRow,roomNumCol).getValue();
    var startDate = copysheet.getRange(rRow,checkInCol).getValue();
    var endDate = copysheet.getRange(rRow,checkOutCol).getValue().getTime()+ 24 * 60 * 60 * 1000;
    var conflicts = calendar.getEvents(new Date(startDate), new Date(endDate));
    if (conflicts.length < 1) {
      var editedEvent = calendar.createAllDayEvent(eventTitle, new Date(startDate), new Date(endDate));
      var editedEventID = editedEvent.getId().split('@')[0];;
      copysheet.getRange(rRow,appCol).setValue("edited");
      copysheet.getRange(rRow,eventIDCol).setValue(editedEventID);
      } else {
      copysheet.getRange(rRow,appCol).setValue("conflict");
      };
    } else {
      var eventEditId = copysheet.getRange(rRow,eventIDCol).getDisplayValue();
      var editedEvent = calendar.getEventSeriesById(eventEditId);
      editedEvent.deleteEventSeries();
      var eventTitle = copysheet.getRange(rRow,roomNumCol).getValue();
      var startDate = copysheet.getRange(rRow,checkInCol).getValue();
      var endDate = copysheet.getRange(rRow,checkOutCol).getValue().getTime()+ 24 * 60 * 60 * 1000;
      var conflicts = calendar.getEvents(new Date(startDate), new Date(endDate));
      if (conflicts.length < 1) {
        var editedEvent = calendar.createAllDayEvent(eventTitle, new Date(startDate), new Date(endDate));
        var editedEventID = editedEvent.getId().split('@')[0];;
        copysheet.getRange(rRow,appCol).setValue("edited");
        copysheet.getRange(rRow,eventIDCol).setValue(editedEventID);
        } else {
        copysheet.getRange(rRow,appCol).setValue("conflict");
        };
      };

  }
}


How to get updated GitHub project after merge?

Question: I have a quick question about GitHub (workflow). I just started using it so this question might seem naive.
1) Say I have a local project which I created a branch and committed my changes.
2) I push up my changes to my GitHub Repository.
3) I then create a Pull request to a member who is going to review my work.
4) He accepts the work and merges my branch with his master copy.
My question is, how do I get those changes to my project? I am confused on how to update my project by using his since he merged it to his master branch?
Answer: Okay. So let's say you worked on a feature and your branch is called "1420"
after your colleague accepted your work and merged with master
He should push master branch to server, and then you have to branch out from master to begin with a new feature.
Steps:

Checkout to master branch.
git checkout master

Make sure your local master branch have the latest changes from
server by pulling all the changes.
git pull

(by now you have all your changes)

Additional steps to start a new feature:

Create a new branch from master for the new feature which will consist of your old branch 1420 commits and master commits (make sure you are in master branch) and then.
git checkout -b 1421

By now you should have all your changes and you are good to go
Comment: Thanks for the reply but I am still a bit confused. When you say " He should push master branch to server" is he pushing it to His gitHub account or somehow to mine?  All I want is when he merges my branch to his master, I would like to update my master branch to be up to date
Comment: I updated the answer please let me know you got the point now :)
Comment: Clear this up. Do I and my collaborator have to share the same GitHub Account for this to work this way? You keep mentioning pushing to server but is he pushing to HIS GitHub account or to a account we are Sharing?
Comment: No. Once the guy pushes the changes to server by "git push -u origin master" his changes will be on server. Now, all you have to do is step 1 and 2 and you should have the changes.
Comment: ooo so if he pushes the Changes to his GitHub account to Master and I do a Git Pull somehow Those changes he accepted which are part of his master will update my master copy? Thanks for the help I am learning this stuff all today!
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122587/discussion-between-johncarp-and-abzoozy).


AWS - Serving static files with performance in mind

Question: I'm working on a project of mine and I'm expecting about ~500-750k unique pings per month.
I'm curious to hear if there's any better way (in terms of latency) to host a static website than my idea below.
Idea:

EC2 instance running NGINX with gzip enabled
CloudFront for CDN

What I'm hosting:

HTML (3 pages in total)
CSS
SVG/PNG images

Also, if my idea above is OK, which EC2 instance would you go for? I'm thinking a micro instance would do just fine, I don't see why I would need the extra RAM/CPU.
Edit: Or how about something simpler like S3 w/ CloudFront?
Comment: All the "performance" of a static website is going to be provided by the CDN. That's the service that the user's browser will be interacting with. So there's no reason to overthink the origin of those files that the CDN will pull from, just use S3. CloudFront is obviously the CDN of choice on AWS, but really you could use any CDN.
Answer: Cost effective , high-available, fully managed , secure and fault-tolerant solution for your case is AWS S3 :

Create S3 Bucket (mybucket) and enable website static properties on it.
Create IAM user with a permission of read/write on that bucket.
<code>    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "s3:ListAllMyBuckets",
        "Effect": "Allow",
        "Resource": "arn:aws:s3:::*"
      },
      {
        "Sid": "Stmt1487841624000",
        "Effect": "Allow",
        "Action": ["s3:*"],
        "Resource": ["arn:aws:s3:::mybucket/*", "arn:aws:s3:::mybucket"]
      }
    ]
    }
</code>
(Keep secret key and access key and the region where you create the bucket)
Configure your secret key and access key in your laptop
<code>  aws configure;
</code>
Then upload your static website :
<code>  aws s3 sync /path/to/local/dir s3://mybucket;
</code>

Congrats! your website is hosted : http://[BUCKETNAME].s3-website.[REGIONMAME].amazonaws.com
If you want to :

map the website to another domain
or/and use SSL
or/and integrate with WAF.
or/and so on...

Use also AWS CloudFront.
Comment: When you hear those two words : AWS + Static Website ,Intuitively, your brain must think about AWS S3 + AWS CloudFront .  Believe me , don't cause pain for your efforts and your money.
Comment: Even SPA applications (like React + ReactRouter) , S3 is still valid with a suitable  configuration of index & error pages when enabling static website property.
Comment: I agree. S3 + CloudFront is certainly the easiest option, without a doubt.
Comment: Yeah, I'm well aware of this.. I guess my question was relating more to the cons/pros on the performance side. Thanks for your answer though, I appreciate it!
Comment: Just thought of this. The major benefit to use a custom web server + CloudFront would be "clean URLs" (/about/ instead of /about.html) without having to hack S3 behavior.
Answer: Can I be late to the party and also suggest S3.  Here are some metrics of actual performance

This is a graph showing S3 latency with a reasonably high load
As you can see the average time to first byte over each hour ranges from 30 to 60ms
We also found that the total amount of traffic did not affect the latency, in fact there seemed to be an inverse relationship between averaged latency and total request count.  I assume this was due to autoscaling "under the hood" at the AWS end
According to AWS documentation the S3 part of the static website setup is good for 800 GET requests per second, see http://docs.aws.amazon.com/AmazonS3/latest/dev/request-rate-perf-considerations.html
Sorry I have no ngnix statistics to compare this with.  We don't use ngnix for this type of workload


A function that is continuous at the origin but not differentiable there

Question: Show that $f(x,y)=\dfrac{xy^2}{x^2+y^2}$ (with $(x,y)\not=(0,0)$ and $f(0,0)=0$) is continuous but not differentiable at $(0,0)$.
I tried to show continuity with an $\epsilon -\delta$ argument but I don't know how to factorize the expression so that I can have something useful.
For differentiability I think I should show that the partials are not continuous at $(0,0)$. But finding the partials is also painful.
Answer: For continuity, a common trick is to express $f(x,y)=g(x,y)h(x,y)$ where $g$ has  limit $0$ at $(0,0)$ and $h$ is bounded in a punctured neighborhood of $(0,0)$. This is easy here:
$$
g(x,y)=x,\qquad h(x,y)=\frac{y^2}{x^2+y^2}
$$
because it's obvious that $0\le h(x,y)\le 1$ for all $(x,y)\ne(0,0)$.
Differentiability doesn't imply continuity of the partial derivatives; in some sense it's the other way round.
This function has partial derivatives at $(0,0)$ and both are zero:
$$
\lim_{h\to0}\frac{f(0+h,0)-f(0,0)}{h}=
\lim_{h\to0}\frac{1}{h}\frac{h\cdot0^2}{h^2+0^2}
=0
$$
and
$$
\lim_{h\to0}\frac{f(0,0+h)-f(0,0)}{h}=
\lim_{h\to0}\frac{1}{h}\frac{0\cdot h^2}{h^2+0^2}
=0
$$
So, if the function is differentiable at $(0,0)$, its differential must be zero. Can you go on?
Comment: No :( Could you give me the whole answer, what happens if its differential must be zero?
Comment: @Bedi Apply the definition: the limit at $(0,0)$ of $|f(x,y)|/\sqrt{x^2+y^2}$ should be zero. Is it?
Answer: By the inequality
$$|f(x,y)|=\dfrac{|x|y^2}{x^2+y^2}\leq \dfrac{|x|(x^2+y^2)}{x^2+y^2}=|x|$$
we see that $f$ is continuous at $(0,0)$.
For the differentiability, your thinking is correct so do not hesitate and do not be afraid of calculus
Comment: This inequality has no name unless you want call it Sami's inequality:)
Comment: Out of curiosity, what is this inequality called?
Comment: Great! But I have doubts about my thinking of the differentiability part: we know that continuous partials imply differentiability but that doesn't necessarily mean that if the partials are discontinous the function is not differentiable, here is an example: http://mathinsight.org/differentiable_function_discontinuous_partial_derivatives
Comment: @Bedi Yes but  to prove that $f$ isn't differentiable at $(0,0)$ it suffices to prove that its partial derivatives aren't continuous at $(0,0)$.
Comment: @SamiBenRomdhane "it suffices to prove that its partial derivatives aren't continuous at (0,0)"... No, no, and no.
Comment: @Did Why? I extracted this paragraph; "Cependant une fonction possédant des dérivées partielles continues sur $E$ est différentiable sur $E$ et est dite de classe $C^1$" from [this][1]

  [1]: http://fr.wikipedia.org/wiki/Fonction_de_plusieurs_variables
Comment: Continuous partial derivatives imply differentiability. Not the other way round.
Comment: To add something interesting, there's another inequality that I like to use all the time in these problems: x*y/(x^2+y^2) <= 1/2
Comment: I don't know if it has a name, but it comes from (x+y)^2 >= 0. (And sorry about the typewriting, I'm still learning LaTex).
Answer: We guess the limit is $0$ and so should verify that:
$$\forall\epsilon>0~\exists\delta>0,~\forall(x,y)\left(0<||(x,y)-(0,0)||<\delta\Longrightarrow\big|\frac{xy^2}{x^2+y^2}-0\big|<\epsilon\right)$$
When $0<||(x,y)-(0,0)||<\delta$ so $\sqrt{x^2+y^2}<\delta$ and from this we get $$|x|<\delta,~~|y|<\delta$$ Now if we set $z=\text{max}(|x|,|y|)$ then $z<\delta$ and also $$|\frac{xy^2}{x^2+y^2}-0|=\frac{|x||y|^2}{|x|^2+|y|^2}<\frac{z^3}{z^2+0}=z<\delta$$ So, it is enough to choose $\epsilon=\delta$. This shows that the function is continuous at the origin.
Comment: p.s. Glad to see you $> 30\mbox{K}$ again, where you belong!
Comment: @amWhy: Thanks so much. I hope I deserve to live in this level. :-)
Answer: For proving continuity you may put $x = r\cos\theta$ and $y= r \sin\theta$. Change the function to polar coordinates. As $(x,y) \rightarrow (0,0)$, $r \rightarrow 0$. This may be a method for showing continuity.
For your second question related to differentiability see the following.
For any $v \in \mathbb{R}^2$, and $t \in \mathbb{R}$ $f(tv) = t f(v)$.
From $\epsilon , \delta$ definition you may easily show $D_{(1,0)} f(0,0) = D_{(0,1)} f(0,0) = 0$ where $D_{(1,0)}$ and $D_{(0,1)}$ are partial derivative along $x$ and $y$ axis, respectively.
But $D_{(1,1)}f(0,0) = \frac{1}{2}$.
So the function is not differentiable at $(0,0)$.
Comment: I don't think your last two lines are clear enough...


Setting up ec2 instance of ubuntu 10.04

Question: I need to set up an ubuntu 10.04 instance on ec2, but there are some instance ready available I tried with few but I am not really happy with that since those are meant for test puporse.
I have to deploy a java/j2ee based web app with mysql.
1)need to install tomcat
2)java runtime 6
3) apache web server
What would be the best image to use for my requirement and I initially planning to launch with the micro instance if my web app demands I may further move to small and larger instances. So it should be eligible for free usuage tier.
Answer: This site has the AMI codes for the official Canonical Ubuntu AMI's.
Where I work, we're using one of those on a micro instance, in the free usage tier.
Comment: But the official images doesnt have small instance, if I want to install Java and Jdk on that then I guess I need to spend lot of money for that.
Answer: One thing to note with the Micro instances, I found they can be prone to crashing when installing the Sun JDK 6 due to resource limitations. The workaround for that is to setup the image on a larger instance size and when finished make a snapshot and run up that snapshot on the micro.
Note that only the 64 bit Ubuntu images run on the micro instances, so make sure you pick a 64-bit Ubuntu image if a micro instance is your target size.
Comment: Ya david I am aware of that I was fighting with 32 bit instance. Now I have to start everything now

Setting up ec2 instance of ubuntu 10.04


Is it possible to check whether these two means are statistically different?

Question: I am building a model on loan borrowers data. It's a binary classification problem, and I want to know whether income a person has, has any effect on whether the person would default on loan. I am in a very initial phase of the development and I still don't know whether 0 or 1 in the target represents that person would default.
My initial intuition is that more the income a person has, less likely he is to default. So I divided the the data based on income into two categories: rich and poor and took the mean of the count of these values for both the categories. All the 1s add up and represent the fraction of population who let's say would default. I want to test the hypothesis that poor people are more likely to default and so their mean would be higher than that of rich. I am getting mean of 16.7% for poor and 15.1% for rich. I want to know if these are statistically different or they are just the same.
I have total of 9578 entries with poor making up 5092 of them and 4486 of them being rich. The probability of being 1 on the entire dataset is 0.160 or 16%. How can I proceed from here? What if I divide the data into 4 income categories?
Comment: Sure, you can do a proportion test. My guess is that your sample size will cause that difference to be significant at any of the usual levels (e.g. $0.05$). You have a bigger issue, though, that you are using your hypothesis that rich people default less to test if rich people default less.
Comment: @Dave Sorry I couldn't understand. Could you pls explain what do you mean "using the hypothesis that rich people default less to test if rich people default less"?
Comment: You're trying to figure out if $0$ means default or of $1$ means default. In order to do that, you are trying to figure out which group has more $0$, the rich people or the poor people. If the poor group has more $0$s, then $0$ must mean default. If the rich group has more $0$s, then $1$ must mean default. Is this correct?
Comment: Yes that is correct. I am assuming 1s mean default and then testing that with the hypothesis that poor people are more likely to default, so they should have more number of 1s. A reaffirmation. I get only a slightly larger number and then I want to know if it's significant.
Comment: And then you use that definition of $0$ and $1$ in your analysis of whether or not wealth and defaults are related...but it all comes from your assumption that wealth and defaults are realted.
Comment: You mean to say that I can only infer whether poor default more or rich once I know the actual definitions of 0 and 1 because my initial hypothesis may not be correct in real world. I can only assume one out of definition of 0,1 or the initial hypothesis but not both.
Answer: You can perform an asymptotic Wald test on the difference or ratio of proportions.  Let $\hat{p}_\text{p}$ be the estimated proportion among the poor and let $\hat{p}_\text{r}$ be the estimated proportion of defaults among the rich.  Then
$$ 1-\Phi\Bigg(\frac{\hat{p}_\text{p}-\hat{p}_\text{r}-0}{\hat{\text{se}}}\Bigg) $$
is the one-sided upper-tailed p-value testing the hypothesis $H_0: \hat{p}_\text{p}-\hat{p}_\text{r} \le 0$ where $\hat{\text{se}}=\sqrt{\hat{p}_\text{p}(1-\hat{p}_\text{p})/n_{\text{p}} + \hat{p}_\text{r}(1-\hat{p}_\text{r})/n_{\text{r}}}$, $n_{\text{p}}$ is the number of poor subjects, and $n_{\text{r}}$ is the number of rich subjects.  You can test other hypotheses by changing the null value $0$ to a different value.  You can compare the p-value to a pre-specified $\alpha$ level to reject or not reject the null hypothesis, or you can simply report the p-value as the weight of the evidence (smaller p-value being stronger evidence against the null).  The corresponding $100(1-\alpha)\%$ confidence intervals for the unknown fixed true ${p}_\text{p}$, ${p}_\text{r}$, and ${p}_\text{p}-{p}_\text{r}$ are
$$\hat{p}_\text{p}\pm z_{1-\alpha/2}\cdot\sqrt{\hat{p}_\text{p}(1-\hat{p}_\text{p})/n_{\text{p}}} $$
$$\hat{p}_\text{r}\pm z_{1-\alpha/2}\cdot\sqrt{\hat{p}_\text{r}(1-\hat{p}_\text{r})/n_{\text{r}}} $$
$$\hat{p}_\text{p}-\hat{p}_\text{r}\pm z_{1-\alpha/2}\cdot\sqrt{\hat{p}_\text{p}(1-\hat{p}_\text{p})/n_{\text{p}} + \hat{p}_\text{r}(1-\hat{p}_\text{r})/n_{\text{r}}}. $$
Typically, 95% confidence intervals are used so $z_{1-\alpha/2}=1.96$.
For a ratio of proportions,
$$ 1-\Phi\Bigg(\frac{\text{log}\{\hat{p}_\text{p}/\hat{p}_\text{r}\}-\text{log}\{ 1\}}{\hat{\text{se}}}\Bigg) $$
is the one-sided upper-tailed p-value testing the hypothesis $H_0: \hat{p}_\text{p}/\hat{p}_\text{r} \le 1$ where $\hat{\text{se}}=\sqrt{(1-\hat{p}_\text{p})/\hat{p}_\text{p}n_{\text{p}} + (1-\hat{p}_\text{r})/\hat{p}_\text{r}n_{\text{r}}}$ is the estimated standard error of $\text{log}\{\hat{p}_\text{p}/\hat{p}_\text{r}\}$ based on the delta method (notice the subtle difference compared to before).  The corresponding $100(1-\alpha)\%$ confidence interval for the unknown fixed true ${p}_\text{p}/{p}_\text{r}$ is
$$\text{exp}\Bigg[\text{log}\{\hat{p}_\text{p}/\hat{p}_\text{r}\}\pm z_{1-\alpha/2}\cdot\sqrt{(1-\hat{p}_\text{p})/\hat{p}_\text{p}n_{\text{p}} + (1-\hat{p}_\text{r})/\hat{p}_\text{r}n_{\text{r}}}\Bigg]$$
Based on the point estimates you have provided, even if they are statistically significantly different, it may not be meaningfully different in terms of the effect size (16.7% vs 15.1%).
I'm guessing the data you have collected is not from a randomized trial.  The inference above is useful for determining association between wealth and loan default.  In order to make a causal connection we must assume no unmeasured confounders, meaning we must assume there are no other customer characteristics that drive loan defaults that are also associated with wealth.  As an example, one could posit that being rich or poor has nothing  to do with loan defaults and that it is actually a different factor that is responsible.  This other factor just happens to be more prevalent among poor people than among rich people, and so it only looks like wealth (or lack there of) is responsible for defaulting.  If you wanted to adjust for potential confounders you can construct inverse-probability-weights, but this is a much more complex topic.


How to play Chopin Etude Op.10 No.4 at the right speed

Question: No matter how much I practised this etude, i just can't play it in the right speed. I play the piano for 12 years approx. and it just didn't work for me with this etude.
Does someone have an idea for me?
Comment: Is this an issue with playing it at a consistent tempo, or a difficulty playing it too slow/too fast?
Comment: It seems that you should remember first all the broken arppegios and then you should just play it all slowly from beginning to end and find the hand position which gives the best tone and agility. Good way is to watch other musicians playing and run scores on Sibelius and Finale software in various tempo.
Answer: The first step is obviously to memorize the piece. Play it absurdly slow (semiquaver = 20; no, I'm not joking). Practice by writing down the piece, check where your memory is weak and train more those parts. When you are able to write it down completely, without looking at the score, you've done half the job.
Second step is being able to master all the technical difficulties in the piece (it's an etude, after all). What I mean is something like this: practice the first four semiquavers in the first bar with your right hand, at least 100 times. When you are able to play those four notes at right speed, then do the same with the other four, and so on. You may want to hold a book with your free hand and read it, like Liszt did, since this is the most time-consuming step. Like mjibson said, don't be scared if you have to put an absurd amount of time for a single bar. It's normal. The only thing you have to keep an eye on is your arm: if it starts getting tired, stop immediately: it means you're doing something wrong. Change position, try using less muscles to do the work.
Third step is putting all together.

First, make sure that you are able to play at the right speed every bar: check the first bar, stop a moment, then check the second bar, etc. If you meet difficulties, go back to step two and practice those elements in the bar that cause you troubles.
Second, practice like this: first and second bars, second and third bars, third and fourth bars, and so on. Again, if there are problems, identify them and go back at the appropriate step. Since this etude is pretty fast, you may also want to chain three bars at time (1-2-3, 2-3-4, 3-4-5, ...).
Only when you're playing it at the right speed you can start adding pedaling, dynamics, a little rubato here and there, etc.

The biggest challenge is the psychological one. Watching videos on youtube of 11 years old children playing this etude like a Bach's minuet may demoralize you. Seeing that after 10 hours of practice you only learnt that little bar may demoralize you. But the truth is that no one has learnt a Chopin's etude without putting in it days and herculean effort.
Answer: Frances Clark (a famous piano teacher) was asked a question about easy Chopin etudes. She replied (as documented in Questions and Answers) that his etudes only come in "hard, harder, and hardest". A graduate student in my piano program played a Chopin etude. He reported practicing 100 hours for a one minute performance. The Chopin etudes are highly difficult. The solution may be to: practice it for a few hundred hours in a disciplined manner (with metronome, slowly, high number of repetitions of short sections until perfect, etc.), or try it again in a few years once you are more skilled.
Comment: Anecdotally I also had lots of tempo problems with Chopin when I took lessons.  To many weird combinations of triplets.
Answer: After doing all that is written above, go to the Barton Tutorial for Op. 10 #4 and watch carefully. I guarantee you a 100% improvement after a few hours of practice. Start with the hardest section first, as short a section as possible without breaking up the passage too much, hands alone is important, with metronome is important; 10 times at one speed with one hand, some hand 10 times next setting, same hand 10 times at next setting (30 times done). Change hands and repeat the process. Tip the metronome over and play it hands together and move on to the next hardest (short if possible) passage.
Answer: Well, I am currently practicing the "easier" etudes 10-3 and 24-12. Memorizing them is a first step. Then the most important part is musicality. These are pieces of music, not mountain climbing. Even with half the tempo they should sound great without hicks and wrong notes. Correct subtle pedalling and a mindful and careful Rubato is very important. Only absolutely relaxed arms and wrists allow rotatation of the hand, invisible with tempo, which gives speed. It needs months of practice. A good personal teacher should be considered.


Procurar na coluna P por um valor > ou < e calcular esse valor na célula U?

Question: Desde já quero agradecer ao @Leandro Felipe Moreira pela ajuda com o seguinte código:
Sub teste()

rng = Columns(16).End(xlDown).Row

MsgBox rng

For i = 1 To rng

    If Cells(i, 16).Value > "0" And Cells(i, 16).Value <= "100" Then

        Range("U2").Select
        ActiveCell.FormulaR1C1 = "=RC[-5]*1.69*(1+40%)"
        lastrow = Range("A" & Rows.Count).End(xlUp).Row
        Range("U2").AutoFill Destination:=Range("U2:U" & lastrow)
        Selection.NumberFormat = "0"

    ElseIf Cells(i, 16).Value > "100" And Cells(i, 16).Value <= "150" Then

        Range("U2").Select
        ActiveCell.FormulaR1C1 = "=RC[-5]*1.69*(1+35%)"
        lastrow = Range("A" & Rows.Count).End(xlUp).Row
        Range("U2").AutoFill Destination:=Range("U2:U" & lastrow)
        Selection.NumberFormat = "0"

    End If

Next i

End Sub

O problema está no preenchimento até à última célula com:
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("U2").AutoFill Destination:=Range("U2:U" & Lastrow)

Pretendo que o cálculo seja feito na célula correta. Ou seja, procure na coluna P1 e apresente o cálculo na coluna U1 e sucessivamente.
Comment: Podes-me ajudar com este @Evert?
Comment: Olá amigo!  Ainda que marque com @NOME tanto em comentário ou numa pergunta/resposta o usuário não recebe essa notificação, salvo se já tiver participado de sua pergunta/resposta com um comentário.
Comment: Somente hj vi sua pergunta e que bom já ter encontrado sua resposta.
Answer: RESPOSTA: Cells(i, 26).Select
Procurar na coluna P por um valor superior  ou inferior e calcular esse valor na célula Z?
Sub teste()
Application.ScreenUpdating = False

rng = Columns(16).End(xlDown).Row

For i = 1 To rng

    If Cells(i, 16).Value = 0 Then

        Cells(i, 26).Select
        On Error Resume Next
        ActiveCell.FormulaR1C1 = "0"
        Selection.NumberFormat = "0"

    ElseIf Cells(i, 16).Value > 0 And Cells(i, 16).Value <= 50 Then

        Cells(i, 26).Select
        On Error Resume Next
        ActiveCell.FormulaR1C1 = "=RC[-10]*1.7*(1+25%)"
        Selection.NumberFormat = "0"

    ElseIf Cells(i, 16).Value > 50 And Cells(i, 16).Value <= 100 Then

        Cells(i, 26).Select
        On Error Resume Next
        ActiveCell.FormulaR1C1 = "=RC[-10]*1.7*(1+20%)"
        Selection.NumberFormat = "0"

    ElseIf Cells(i, 16).Value > 100 And Cells(i, 16).Value <= 200 Then

        Cells(i, 26).Select
        On Error Resume Next
        ActiveCell.FormulaR1C1 = "=RC[-10]*1.7*(1+15%)"
        Selection.NumberFormat = "0"

    End If

Next i

End Sub


Rails - redirect to current url without one of the GET params

Question: I have a very simple problem in a Rails app that I can't find an elegant solution to.
I have a URL like:
http://www.example.com/some-path/?foo=123&baz=456
And I want to process one of the params and do a redirect to:
http://www.example.com/some-path/?baz=456
I want this to work generally for any URL with param "foo", so I don't want to specify "baz" in the rewrite code.
What I want is something like:
redirect_to request.path, :params => request.query_parameters.except(:foo)

Except that doesn't quite work.  I know I can regex or parse the query string manually but I assume there's a better way.
Comment: In what way does it not work? Do you get an error? Do you get a URL you didn't expect?
Comment: For reference, this code does exactly what I want (apologize for formatting):

`query_string = request.query_parameters.except(:foo).map{|k,v| "#{k}=#{v}"}.join('&')`

`redirect_to query_string ? "#{request.path}?#{query_string}" : request.path`
Comment: The code above doesn't actually keep baz=456 in the redirect
Comment: The code in your comment has multiple problems. You're not escaping those querystring params when you reconstruct the url. If any of them contain = or & or other special characters that should be encoded, you'll end up with a very broken redirected url. Also, this code will not properly deal with the automatic hash and array-ification of query parameters like `x[a]=1&x[b]=2` that request.query_parameters gives you.  These are two reasons why it is generally a bad idea to write your own querystring manipulation code, especially in a complex framework like rails.
Answer: The best way which I have found so far is to use:
url_for(params.except(:obsolete_param_name))

UPDATE
As grosser mentioned this opens up a security hole in your application as an attacker can pass a host as a url param and redirect the user to a different website. In order to prevent this you can use rap1ds's suggestion and specify that only the path has to be used.
You can use a helper function like this one for this purpose:
def secure_path_for(params, *excluded_params)
  url_for(params.except(*excluded_params).merge(only_path: true))
end

Comment: This is a security hole, it will people redirect to a given host ?host=attacker.com -> attacker.com
Comment: `url_for` has an option `only_path`, so if you don't need absolute URL, you can do `url_for(params.except(:obsolete_param_name).merge(only_path: true)` This should not expose any security issues, or does it? At least I can not come up with any.
Comment: You are right! So maybe another solution would be to wrap it in another method, which will make sure that keys like 'host' and etc. won't be modified (something similar to what strong parameters does to prevent mass assignments).
Comment: I had to go to the current route with one parameter modified, I used: `url_for(params.merge({my_param: new_value}))`
Comment: what about `url_for(params.except(:obsolete_param_name, :host))` ?
Answer: What about just deleting params[:foo] before you pass it along.  Maybe something like:
params.delete[:foo]
redirect_to request.path, :params => params

It's not really elegant, but it only adds one line. Params by the way is a helper method that get's the url's current parameters
Comment: This is basically equivalent to what I did above (doesn't work), except params also pulls in params from the route, so you would possibly get extra stuff in the URL you didn't want.
Comment: Assuming the existing controller for /some-path/ can handle both forms  of expected URL (e.g. `if params[:foo]...`) then this is a perfectly appropriate solution.  You could create a named route that matches the  foo-less path, but that would make things less obvious, IMHO.
Comment: yes, that's true. This assumes that the only goal is to remove the parameter :foo going into the redirect.
Answer: In rails 5 you will get non-sanitized request parameters error if you attempt to do this url_for(params.except(:param))
So I found this way to do this:
request_url = request.url # eg. http://qwe.com/path?param1=1&param2=2

query_hash = Rack::Utils.parse_query(URI.parse(request_url).query).except('param1')
url = query_hash.empty? ? request.path : "#{request.path}?#{query_hash.to_query}"

puts url #=> /path?my_param2=2

redirect_to url

Answer: uri = URI.parse(request.url)
new_query = uri.query.split("&").reject{|pair|
  k,v = pair.split("=")
  k =~ /^foo\[?/ # reject params named "foo" or "foo[anything"
}.join("&")
# if the new querystring is empty, assign nil instead of "" so we don't
# end up with a solitary question mark at the end of the url.
uri.query = (new_query.empty? ? nil : new_query)
redirect_to uri.to_s

I came to the above solution after running into some non-obvious problems with trying to do things the "Rails way," if there is such a way, to solve this issue. For instance:

Rails parameter parsing will automagically instantiate arrays and hashes for query params that use the special square bracket syntax: ?x[a]=1&y[]=2 becomes {"x"=>{"a"=>"1"}, "y"=>["2"]} and so on, so code that uses the provided querystring and param routines are tricky to deal with in this regard. And filtering params by raw string value can be difficult as a result. Even the non-rails/rack CGI module will give you arrays instead of strings for the param values.
The rails params hash includes both GET, POST, and routing parameters, such as :action and :controller so you couldn't just re-use that to reconstruct a querystring anyway.
As such, the URL helpers like url_for are not much help.

Given these issues I found I was not able to use provided framework functionality to get a pure and unmolested querystring, reproduced as-is from the current request, that simply and only removes one parameter. The code above seems to work well, however.
Answer: In case someone comes to this later, this is what worked for me
link_to request.params.except(:param)


Call a function in Python and pass only the arguments it expects

Question: How do I call a function and only pass it the arguments that it expects. For example say I have the following functions:
func1 = lambda a: True
func2 = lambda a, b: True
func3 = lambda c: True

I want some Python code that is able to successfully call these functions without raising a TypeError by passing unexpected arguments. i.e.
kwargs = dict(a=1, b=2, c=3)
for func in (func1, func2, func3):
    func(**kwargs)  # some magic here

I'm not interested in just adding **kwargs to the functions when I define them.
Comment: @BlaXpirit... and bruteforce all combinations of arguments?
Answer: You can use inspect.getargspec():
from inspect import getargspec
kwargs = dict(a=1, b=2, c=3)
for func in (func1, func2, func3):
    func(**dict((name, kwargs[name]) for name in getargspec(func)[0]))

Comment: Note it is deprecated since 3.0 Use getfullargspec() instead, which provides information about keyword-only arguments and annotations. but getfullargspec() is not available in 2.7 and lower
Answer: Well, depending on exactly what you will want to do with variable arguments and keyword arguments, the result will be a little different, but you probably want to start with inspect.getargspec(func).


A proof on smooth function that I don't know what to proof.

Question: Here's the question:
Suppose $f: U \rightarrow V$ is a smooth map, for $U \subset R^k$ and $V \subset R^\ell$ open sets. That is, all partial derivatives (of all orders) of $f$ exist and are continuous. Show that the map $Df: U \rightarrow \mathrm{Mat}_{\ell \times k} \simeq R^{k\ell}$ given by $Df(x) = df_x \in \mathrm{End}(R^k,R^\ell) = \mathrm{Mat}_{\ell \times k}$, is smooth.
And here's my thought:
Give all partial derivatives (of all orders) of $f$ exist and are continuous, that is $d^n f_x$ exists and continuous. And by definition, Df is so.
So my doubt is - I didn't really do anything here, right? Thank you very much for helping me clear this out!
Answer: I think you could be more explicit in relating the partial derivatives of $f$ with the partial derivatives of $Df$. We want $Df$ to be smooth; this requires each one of its coordinate functions, $(Df)_{i,j}:U\to\mathbb R$ for indices $i\leq k$ and $j\leq\ell$, to be smooth. In other words, we want each partial derivative $\partial_\alpha(Df)_{i,j}$ to exist and be continuous for each multi-index $\alpha$.
Now, $(Df)_{i,j}=\partial_i f_j$, where $\partial_i$ is the partial derivative with respect to the $i$-th coordinate, and $f_j:U\to\mathbb R$ is the $j$-th coordinate of $f$. By hypothesis, $f$ is smooth, so $f_j$ is smooth. That means that every partial derivative of $f_j$ exists and is continuous. In particular, $\partial_\alpha \partial_i f_j$ exists and is continuous for all $\alpha$, which is exactly what we want!
(Note that I haven't been terribly careful about using subscripts versus superscripts. You might want to clean up the notation, and you might also want to specify just what kind of partial derivative $\partial_\alpha \partial_i$ is.)


Organize array in PHP from mysql

Question: Hi i have a social networking website.
what i want it to do is pull out my friends status updates.
basically what it does is i have a mysql query that pulls out all of my friends and in that while loop there is another mysql query that pulls out the status's from my friends.
i want it to be in order of date but since its one while loop in another what it does is pull out all status's from friend 1 then 2 then 3 and not in order by date. i even tried ORDER BY DATE but that just ordered it by date within the friend..
my thought is that i could putt it all in an array and friends is one thing and the values is the stats. then just sort by values would this work and how could i do it.
the friend and stats are in two differants tables
THANKS SO MUCH
CODE:
$friendssql = mysql_query("SELECT * FROM friends WHERE sender='$id'");
while($row = mysql_fetch_object($friendssql)) {
    $friendid = $row-> accepter;
    $frsql = mysql_query("SELECT * FROM myMembers WHERE id='$friendid'");
    while($rowa = mysql_fetch_object($frsql)) {
        $ufirstname = $rowa-> firstname;
        $ulastname = $rowa-> lastname;
    }

$blabsql = mysql_query("SELECT * FROM blabbing WHERE mem_id='$friendid' ORDER BY blab_date DESC");
while($rowb = mysql_fetch_object($blabsql)) {
    $blab = $rowb-> the_blab;
    $blabd =$rowb-> blab_date;

    $ucheck_pic = "members/$friendid/image01.jpg";
    $udefault_pic = "members/0/image01.jpg";
    if (file_exists($ucheck_pic)) {
    $blabber_pic = ""; // forces picture to be 100px wide and no more
    } else {
    $blabber_pic = ""; // forces default picture to be 100px wide and no more
    }

Comment: Posting some code might help.
Answer: why not do it all in one query?  this is psuedo sql, so you'll have to modify with your real tables and relationships.
select f.name,s.statustext
from friends f
     inner join status s
         on s.friend_id = f.id
     inner join myfriends mf
         on mf.friend_id = f.id
where mf.myid = 'myid'
order by f.name, s.datestamp

or something similar.
Comment: im having this issue because i am very new to php.
sorry but i have no clue what any of that means.

but from looking at it i think i forgot to say that the friend and stats are in two differants tables
Answer: Once you've put your data into the array, you could take a look at some of the various array sorting functions in PHP: http://php.net/manual/en/array.sorting.php

Organize array in PHP from mysql


Mixing probabilities in mixture models using EM

Question: Assume we want to estimate the mixing probabilities ($\pi_{k}$) for each member distribution in the mixture model.
We know that $\sum_{m}^{K}\pi_{m}=1$, so we can formulate the optimization problem as a Lagrangian multiplier; the function we want to maximize (w.r.t. $\pi_{k}$) is is given by,
$$ Q = \sum_{l}^{n}\sum_{m}^{k} p(m|l)\text{ log}q(m|l)+\delta(\sum_{k}\pi_{k}-1) $$
where:
$q(k|l) = \pi_{k}g_{k}(x_{l};\theta_{k})$, i.e. the joint probability of selecting mixture component $k$ and the probability of datapoint $x_{l}$ given component distribution $g_{k}$
and
$p(k|l) = \frac{q(k|l)}{\sum_{m}^{k}q(m|l)}$
Setting $\frac{\partial Q}{\partial \pi_{k}}=0$, yields,
$$ \frac{\partial Q}{\partial \pi_{k}} = \sum_{l}^{n}\frac{p(k|l)}{\pi_{k}}+\delta = 0 $$
Now, in order to solve for $\pi_{k}$, we must solve for $\delta$; apparently, $\delta=-n$. This results in,
$$ \pi_{k} = \sum_{l}^{n} \frac{p(k|l)}{n}$$
However, I'm not sure why this is; more specifically, how can it be shown that,
$$ \sum_{l}^{n}\frac{p(k|l)}{\pi_{k}} = n $$
Answer: You hand-waved over the derivation of $\delta=-n$, when in fact it comes from the derivative with respect to $\delta$ and is essentially the condition that $\sum\pi_k=1$. If you want to see the reverse, just plug in the definition of $p(k|l)$ into the formula, and use the fact that $\sum g_k$ over its component is 1, because it is a distribution.
Answer: When doing EM for a GMM, the log-likelihood of the data is given by: $$\ln p(\mathbf{X}, \mathbf{Z} | \boldsymbol{\mu}, \boldsymbol{\Sigma}, \boldsymbol{\pi}) = \sum_{n=1}^N\sum_{k=1}^K z_{nk} \{\ln \pi_k + \ln \mathcal{N}(\mathbf{x}_n | \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)\}$$
To solve for the mixing proportions, we can write first write the Lagrangian $$\mathcal{L}(\mathbf{x}, \delta) = \ln p(\mathbf{X}, \mathbf{Z} | \boldsymbol{\mu}, \boldsymbol{\Sigma}, \boldsymbol{\pi}) + \delta(\sum_{k=1}^K\pi_k - 1)$$ and then take the derivative with respect to $\pi_k$. This gives us $$\frac{\partial\mathcal{L}}{\partial\pi_k} = \sum_{n=1}^N\frac{z_{nk}}{\pi_k} + \delta$$
Equating this to zero and solving for $\pi_k$ gives $\pi_k = \frac{-\sum_{n=1}^N z_{nk}}{\delta}$. Summing both sides over $k$ and employing the constraint $\sum_{k=1}^K\pi_k = 1$, we find that $1 = -N/\delta$. Thus, $\delta= -N$.


How can I clean (reset cache) of React Native using Expo. Not sure if it is cache issue

Question: I am building a simple React Native app with create-react-native-app using only react-navigation and base-64(base 64 encode/decode) as dependencies. Over time starting the app became slower and now sometimes it is getting stuck(waiting for about 30min) on "Starting packager..." after running NPM start.
I've tried starting it with "npm start -- --reset-cache" but nothing changed
Once it is up and running I don't get any error or warning in DevTools or terminal. Also there are no issues with the performance of the app
What can I do to make NPM start smooth and quickly as before? Are there any general steps I can take to troubleshoot this issue?
Here is my package.json
{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "jest-expo": "~27.0.0",
    "react-devtools": "^3.2.3",
    "react-native-scripts": "1.14.0",
    "react-test-renderer": "16.3.1"
  },
  "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
  "scripts": {
    "start": "react-native-scripts start",
    "eject": "react-native-scripts eject",
    "android": "react-native-scripts android",
    "ios": "react-native-scripts ios",
    "test": "jest"
  },
  "jest": {
    "preset": "jest-expo"
  },
  "dependencies": {
    "base-64": "^0.1.0",
    "expo": "^27.0.1",
    "react": "16.3.1",
    "react-native": "~0.55.2",
    "react-navigation": "^2.11.2"
  }
}

Comment: Official Documentation [clearing bundler caches](https://docs.expo.dev/troubleshooting/clear-cache-windows/)
Answer: expo r -c
According to this forum post from an Expo dev, that's the command to start it cache clean
EDIT: They have changed it from exp r -c to expo start -c
Comment: `exp` is now `expo`, `r` is short for `start`, and `-c` is short for `--clear`
Comment: what if I don't want to start expo and just clean the cache?
Comment: @kwoxer none of the above will remove actual React Native cache
Comment: based on the new npx method, it is now ``npx expo start -c``
Answer: The command to run is:

expo r -c
Answer: For Expo 45 and below
expo r -c

For Expo 46 and above
npx expo start --clear

Comment: npm start --reset-cache just gave me the error:
There was a problem loading the project. This development build encountered the following error. Value of type java.lang.String cannot be converted to JSONObject.
Answer: In you console run:
expo start

After the QR code is shown, press SHIFT + R keys.
Answer: I was running expo build:ios and needed to clear the cached provisioning profile. I did so with:
expo build:ios --clear-provisioning-profile

To see the full list of commands that clear specific elements from the cache, run expo build:ios --help
Comment: Is expo build:ios --clear-provisioning-profile the same as expo start --clean?
Answer: --clear-cache is a flag to clear cache before the build:
eas build -p android --clear-cache
eas build -p ios --clear-cache

all available flags, commands etc. you can see by input command:
eas build --help

Answer: npm start -c or npx expo start -c
Answer: 2023 update
npx eas build -p ios --clear-cache

or
npx eas build -p android --clear-cache

Answer: It will clear the cache and start the project
expo start -c


consulta Sql trazendo os produtos que estao com saldo zerado

Question: Tenho as tabelas :
Produto                Departamento
=========              ========
Id_Prod                id_Departamento
Descricao              Descricao

Produto_Departamento
=====================
id_Prod
Id_Departamento
Saldo

Tendo o seguinte cenário, gostaria da ajuda de voces para desenvolver essa consulta.
Gostaria de trazer todos os produtos cujo a soma do saldo seja = 0. Lembrando que o produto poderá esta cadastrado em vários departamentos
Comment: Não seria somente dar um sum do saldo e agrupar por produto?
Comment: Qual banco de dados você está usando? Pro caso de uma query mais especifica, claro.
Answer: Você não especificou muito bem, mas acredito que esse exemplo irá te ajudar:
select p.id_Prod, prod.Descricao from Produto_Departamento p
inner join Produto prod on prod.Id_Prod=p.Id_Prod
having sum(p.Saldo)>0
group by p.id_Prod,prod.Descricao

Comment: Mas essa query não traz os produtos, somente a soma.
Comment: Sim, é só ele fazer o join e trazer os produtos
Comment: Mas é justamente isso que ele pede na pergunta. Sugiro que adicione na resposta, assim ela atenderá a pergunta e fica completa :)
Comment: Ok, completei a resposta.


Add a file-download widget on a form

Question: I have a file field called "contracts" between my user fields.
What I want is that in the user-profile form this field would come up as a file-download widget.
Does anyone have an idea how to show a file-download widget (even a link to a file would work) in the user-profile form?
Comment: Can you define "file downloadable widget"? Maybe it's just me but I have no clue what you mean. Did I understand correctly that you want to offer a downloadable something in the user profile form? That seems contradictory: a form is for user input, not for retrieving information.
Comment: the requirement is such that there needs to be a downloadable file in user profile fields :)
Answer: Assuming that Your Drupal 7 site is a new site (not upgraded from a D6 installation), I think the best approach is to use the Profile 2 module (dependent on Entity API) and add a file field to the user profile.
Answer: Drupal 7 includes a file field, and you can add a file field to the user profiles in admin/config/people/accounts/fields.

In the row "Add new field," you select "File" as "Type of data to store;" the form element to show is automatically set to "File," as that is the only form field you can use, in this case. When you click on save, Drupal will allow you to set the settings for the field.
To be able to select the file field, you need to first enable the File module in admin/modules.
Once a file is uploaded, the field will appear as in the following screenshot, in the user profile. (I have used "File" as field label.)

The name of the file downloaded is a link that can be clicked.
The file field has also the option to show the content of the uploaded file, instead of the link to the file.

Add a file-download widget on a form