Am I allowed to add a b9 to a maj7 chord in Jazz?

Question: On Jazzology chapter 2, exercise 6, example 2, I have to identify this chord (if it exists):

I thought that it was Cmaj7(b9). My train of thought was (from the lowest to the highest note):
C -> 1st
E -> 3rd
B -> major 7th
Db -> b9.
But I looked at the solution and I saw that the chord displayed is wrong; there isn't any such chord.
The only thing I can see that could be wrong is adding the b9 to a major 7th chord.
Am I allowed to add a b9 to a major 7th chord in Jazz?
Comment: You're allowed to do whatever you want, really. But I get your meaning. :)
Comment: Are you allowed to go outside after 9pm? ;)
Comment: There seems to be some errors in the answer key to Jazzology. Some were fixed in a later edition I believe. There are erratas on the internet though. (Not saying that it's the case here, but it's good to know). It's a shame they didn't do better proofreading, since it's an otherwise good book.
Answer: Adding a b9 to a major 7th chord creates a very dissonant sound because the chord has two different notes that are each a half step away from the root. The resolution would be tricky because the b9 would want to go down a half step and the 7th, and the root also needs to go somewhere.
That being said, however, I found a few voicing that sounds good for it, though, and it seems it would work well as a non-functional passing tone. As Sir Duck would say :

If it sounds good, it is good.

So as long as it fits wherever you put it, it should be fine.

When playing around with the concept, I found going from a C9, then letting the D go down to a Db, then going to a Fsus4 finally resolving to an F. Not the prettiest sounding progression ever, but I like it.

As for voicings, inverting the chord does not work. I did not find a voicing outside of root position that would work. Spacing out the root, 7th, and b9 really help pad the dissonance. Omitting the 5th also helps. The voicing I liked best is below and has all the notes.
Comment: @Shevliaskovic - I find it important to bring up Function when we talk about unorthodox chord voicings/extensions. You can add whatever you want to a chord and describe it by what best represents it but the function changes. Typically you would not find a b9 on a Maj7 chord because of its dissonance. This dissonance desires a resolution, while usually a Maj7 chord does not require resolution. Real point being that you can create these chords and call them what you like but it would not be advisable to place a b9 on a Maj7 chord when reading a chart because it has a different Function.
Answer: You can do whatever you want, which is the beauty of Jazz.  It's more a matter of whether other people will still call what you're doing Jazz.
I've implied a flat 9 (which I generally consider an energized root rather than a lowered second) in a piece with a simple, whole-note melody within Db maj of Db-C-D-Db.  This works to my ear (and is followed by G maj, E maj, Db maj, to give you some context).
It is going to have more 'dissonance', but this can work fine depending on how you do it.  Playing those notes in different octaves and/or with different instruments whose timbres are distinctively different are two tricks.  Avoiding playing the two notes together or moving among them quickly are two more.  On the other hand, sometimes the extra tension among notes is something we want.  Consider Jerry Lee Lewis's right-hand clusters in some of his soloing, or the tense, stabbing strings portion of Stravinsky's Rite of Spring (Dance of the Young Maidens).
Digressing a little, I think that a lot of Westerners, upon hearing a raised root, expect a minor 7th, but you can get a nice and unusual mood from having both a leading tone above and below, and providing the same for the 5th can work well with this, too.  For example, C-C#-E-F#-G-G#-B-C .
Here's an example of this sort of thing, and note that if we were analyzing as Jazz we'd probably imagine a 9th which contains the elements you mentioned:

There are major 7ths in this piece, though not all of the 7ths are major.  To feel how the notes play together, if you have the means you may want to run this piece through delay or reverb so that the +1 and -1 rub together.
Not all music picks either a major or minor 7th, 3rd, or raised 4th over a "perfect 4th" and sticks to it, and in fact I prefer to avoid this scale-derived nomenclature unless communicating with others who rely on it, as it engenders a mindset which encourages such adherences.  To me it's more freeing to think in terms of scale degree while remembering the relative resonances between degrees.  Some Jazz traditionalists seem to frown upon this and prefer the prevailing nomenclature.
One more I happened to notice after my last edit:

Very nicely expresses the idea of those two notes, at least one way of using them.  Start at 4:30.
Answer: I believe this is exactly what BBNG makes use of in their song CS60 on the first chord when the melody comes in. It sounds to me like an Ab major7 (b9) than then resolves down to the root, G minor and continues to Eb and then basically D minor (the diatonic VI and v respectively). The melody notes go from b9 to major 7 and it works really well in giving it this strange Eastern flavor that people in previous answers have referred to. I think the quavering pitch on the lead synth helps to make enforce that as well. The half and whole step movement throughout the progression on this first part, I think, contribute to the overall strength of the progression.
Answer: The major chord is generally the most stable of structures and is where harmony comes to rest, like a full stop. Major chords can feature a raised 4th aka the Lydian sound, but these alterations to the basic chord are more to add colour and nuance whereas a minor 9th tends to destabilise the chord and thus it conflicts with its purpose in life, which is to provide stability and consonance. One would expect dissonance to be provided by the dominant chord. So in conclusion, I'd not expect to see a major with a flat 9. It conventionally would have a 1, a major third, a fifth and in the context of a major 7th chord, a major seventh note. Potentially an added 9th too for more upper tone colour.
Answer: You can sometimes hear maj7b9 chords in the music of Brad Mehldau. A possible function is as a replacement for the V7♭9 in minor: In the key of E minor, I might play a Bmaj7b9 chord, where the major seventh comes from E minor blues scale. But Mehldau usually uses the maj7b9 chord as part of a four-chord vamp section without an especially strong sense of key center. I believe he uses a maj7b9 (or inversion of it) in the third bar of "You Can't Go Back Now":

To add onto Dom's answer, another way jazz pianists sometimes create a sense of order in dissonant chords like this is to try to create a major triad in the RH. For this chord, an A triad gets us two of the chord tones, and then we can play a B in the left hand if we want.
On paper, this looks like Cmaj13b9, which you might expect to sound more dissonant, but I find its sound preferable to a Cmaj7b9 voicing that includes the fifth. When the fifth is present, the note G creates a tritone dissonance with the ♭9 that both adds to and distracts from the ♭9 dissonance that gives this rare chord quality its unique character.
My voicing is the first, while the one Dom likes is the second:
X: 1
K: C
M: 4/4
L: 1/4
%%staves {V1 V2}
V: V1 clef=treble
V: V2 clef=bass
[V: V1]|[EA^c]4 |[EG^c]4 |
[V: V2]|[C, B,]4 |[C, B,]4 |

Notice that your textbook also omits the fifth, probably for the same reason.


Using `mutate_at` or a `map` function in R to calculate distances between multiple locations

Question: I have on dataframe (controids) with longitudes and latitudes for a set of locations:
library(tidyverse)

centroids <-
  tribble(
    ~city, ~long, ~lat,
    "A", -89.92702, 44.19367,
    "B", -89.92525, 44.19654,
    "C", -89.92365, 44.19756,
    "D", -89.91949, 44.19848,
    "E", -89.91359, 44.19818)

I have a second dataframe (towns) with longitudes and latitudes for a second set of places.
towns <-
  tribble(
    ~town, ~long, ~lat,
    "greentown", -89.92225, 44.19727,
    "bluetown", -89.92997, 44.19899,
    "redtown", -89.91500, 44.19600)

I want to add three columns to centroids giving the straight-line distance, in kilometers, between each city and  each of the three towns in towns. So the final dataframe would look like this (distances not correct, just for illustration):
output <-
  tribble(
    ~city, ~long, ~lat, ~greentown_dist, ~bluetown_dist, ~redtown_dist,
    "A", -89.92702, 44.19367, 5.3, 2.0, 1.2,
    "B", -89.92525, 44.19654, 4.4, 2.3, 9.9,
    "C", -89.92365, 44.19756, 3.7, 5.4, 3.3,
    "D", -89.91949, 44.19848, 2.6, 3.9, 6.7,
    "E", -89.91359, 44.19818, 10.2, 2.2, 3.1)

I have to do this for a large number of towns so I'm trying to write some code that's easy to generalize. Here is what I have so far.
library(sf)

towns <- towns %>% st_as_sf(., coords=c('long', 'lat')) %>% st_geometry()

output <-
  centroids %>%
  st_as_sf(., coords=c('long', 'lat')) %>%
  mutate(greentown_dist = st_distance(geometry, st_point(c( unlist(towns[1]) ))),
         bluetown_dist = st_distance(geometry, st_point(c( unlist(towns[2]) ))),
         redtown_dist = st_distance(geometry, st_point(c( unlist(towns[3]) ))))

I want to know if there's a way to do this using mutate_at and/or a purrr map function - one that fills in the TOWN_dist column name automatically and inputs the correct row from the town dataframe.
Comment: I think one way to something to what you want is to do a `full_join()` and then do your calculation then transpose with `pivot_wider`, `newdf <- full_join(centroids, towns, by = character())` this will give you one row per comparison
Answer: We can use map to loop over the 'towns'
centroids[paste0(c("green", "blue", "red"), "town_dist")] <- map(towns,
       ~ centroids %>%
            st_as_sf(., coords = c('long', 'lat')) %>%
            transmute(dist = st_distance(geometry, st_point(c( unlist(.x))))))


How to prove $ \sum_{0}^{\infty} (\sin(x))^n $ converges or diverges for different values of x?

Question: I have attempted to use the idea of geometric series and got that it converges for x< arcsin(1) and x> -arcsin(1). But since this is a series showing this doesn't seem sufficient ?
Comment: Hmmm, what are $\arcsin(\pm1)$ already? And when is $|\sin x|<1$?
Comment: Why isn't it sufficient ? if $x\neq \pi/2\pmod{\pi}$ then $|sin(x)|<1$ so this is the term of a convergent geometric series as you mentioned. And elsewhere the series is $\sum 1$ or $\sum(-1)^n$ both divergent.
Answer: I think that the $\sin(x)$ is just disguising a simple question.  All that matters is the value of $\sin(x)$, let's call that $y$.  We know that $-1 \leq  y \leq 1$.  So, we are looking at:
$$ \sum_{0}^{\infty} y^n $$ which is known to converge provided that $|y| < 1$.  We have 3 cases: $y = 1$ which clearly does not converge, $y = -1$ which also does not converge but for a slightly different reason, and $-1 < y < 1$ which will converge.
Comment: Would you be able to expand on the 'slightly different reason' ?
Comment: When $y = 1$, all the terms are $1$ and the partial sums are $1, 2, 3, . . .$. They grow without limit. .  When $y = -1$, the terms alternate between $1$ and $-1$ so the partial sums alternate between $1$ and $0$.  They do not grow without limit but they do not converge either.
Comment: Is using the term "sum of a geometric progression" not the simplest answer... with its classical condition of convergence ?
Comment: @JeanMarie I guessed that the OP was thrown by the $sin(x)$ which I guessed was intended to cause confusion.  So, I concentrated on that aspect.


Definition of direct products of two cones or of two convex subsets?

Question: When reading a comment after this reply, I was wondering what the definitions of direct product of two cones?
More generally, what is the direct product of two convex subsets? This case is what I found by googling (here and here), but I haven't found the definitions yet.
Is there a more general definition?
Thanks and regards!
Answer: As a set, the direct product $C_1\times C_2$ of $C_1$ and $C_2$ is just the set of ordered couples $\left\{(c_1,c_2)\mid c_1\in C_1, c_2\in C_2\right\}$. Moreover, if $C_1$ and $C_2$ have some additional structure, usually their product can be endowed with the same kind of structure, for example that of topological space. This is true for convexity, too.

You are asking about a more general definition, too. The most general definition of (direct) product I can think of is this one, but I'll just link to it as a reference. Just notice that some mathematicians would call that kind of language "abstract nonsense". ;)
Comment: Thanks! So it is the same as Cartesian product?
Comment: Yes, Cartesian product is direct product of sets. If those sets have additional structure which is preserved by taking the product, you call it (direct) product (of topological spaces, vector spaces, etc).


How to simulate an unreplicated factorial design?

Question: I am dealing with a unreplicated factorial design. I have some illustrative examples but I need to simulate some unreplicated factorial designs. I do not how and what to use. Can $R$ handle this?
For example, I would like to analyse a $2^{4}$ factorial design (factors are A, B, C and D) with only one run and 15 contrasts. I have a single column for response. I would like to compare some methods in the literature to see which method detects active effects better. Thus, I set the active effects to have the same magnitude of $1.5\sigma$ and I would like to generate $100$ response vectors using errors that are i.i.d. with $\mathcal N(0 ,1)$. My true model has four active effects and I would like to simulate $100$ response vectors using this true model $y=3+1.5A+1.5B+1.5C+1.5BC$. But I do not know how to generate data like this using R.

Thanks gung for your reply. I just wrote a simple code before I saw your answer here. I think, I need to build up a bit more R knowledge. Anyway, here it is:
For the analysis of unreplicated factorial designs with $k$ factors and $p=2^{k}-1$ factorial effects (the main effects and interactions), the following model is generally used
\begin{equation}
y=\sum\limits_{i=0}^{p}x_{i}\beta_{i}+\varepsilon_{i}
\end{equation}
So, Firstly I introduced my sign table for $2^{4}$ and $\beta$ coefficients of so-called active effects.
Sign table consists of rows (runs) and columns (contrasts with general mean).

And then, I created my regression equation with magnitudes of active effects and zeros of remaining inactive effects. My simulated model, for example, was $y=3+1.5A+1.5B+1.5C+1.5BC$.

And then, I run the code below
x=read.csv("sign2.txt", header=TRUE)
sign= as.matrix(x)
is.matrix(sign)

y=read.csv("beta2.txt", header=TRUE)
beta= as.matrix(y)
is.matrix(beta)

signt=t(sign)

bs=t(beta %*% signt)

epsilon=matrix( rnorm(16*1,mean=0,sd=1), 16, 1)

response=bs+epsilon

However, unfortunately, it's for one simulation. I will put a loop command to run the simulation n-times.
Comment: Note that there are some differences between your approach & what you had posted originally / I responded to. Eg, the slopes were $1.5$ in the question, whereas this uses $2$. Also, I used reference level coding for the categorical variables, but you are using effect coding.
Comment: I just changed the magnitudes in the regression equation. It is my mistake. Sorry. By the way, how you coded factors is not correct, I believe. You need to use (-1, 1) not (0, 1). Because in order to get predicted values or response values for each run (in this case, it's 16) you need to multiply every row in the sign table with the corresponding β coefficients of active effects in the regression equation and then, to add error that is normally distributed.
Answer: The key to generating random data like yours in R is to use ?rnorm.  You may also want to set the random seed to a fixed value, via ?set.seed, so that your simulation can be exactly replicated in the future.  For convenience, you may prefer to use ?expand.grid to create your factor combinations, although you can do it manually as well (and could conceivably prefer that for the clarity it affords).  Once you have set up your simulation, you will want to run it many times to get a sense of its long run behavior; this can be done using ?replicate, I believe, but I usually just nest it in a for loop because I specialize in writing comically inefficient code.
Here is an example:
> set.seed(1)
> A <- c(0,1)
> B <- c(0,1)
> C <- c(0,1)
> D <- c(0,1)
> Xmat <- expand.grid(A=A, B=B, C=C, D=D)
> Xmat
   A B C D
1  0 0 0 0
2  1 0 0 0
3  0 1 0 0
4  1 1 0 0
5  0 0 1 0
6  1 0 1 0
7  0 1 1 0
8  1 1 1 0
9  0 0 0 1
10 1 0 0 1
11 0 1 0 1
12 1 1 0 1
13 0 0 1 1
14 1 0 1 1
15 0 1 1 1
16 1 1 1 1
> y <- 3 + 1.5*Xmat$A + 1.5*Xmat$B + 1.5*Xmat$C + 1.5*Xmat$B*Xmat$C +
+      rnorm(n=16, mean=0, sd=1)
> y
 [1] 2.373546 4.683643 3.664371 7.595281 4.829508 5.179532 7.987429 9.738325
 [9] 3.575781 4.194612 6.011781 6.389843 3.878759 3.785300 8.624931 8.955066


How to delete files from Firebase storage while deleting a document that has reference for that file?

Question: I have an Angular App, it includes items each item has its image uploaded on Firebase storage and has an img property that refers to the URL of the image.
I want to delete the image while deleting the document, how to do it properly?
Comment: To delete files, see https://firebase.google.com/docs/storage/web/delete-files. If you're having problems making that work, edit your question to include the [minimal, complete, standalone that reproduces the problem.](http://stackoverflow.com/help/mcve)
Comment: @FrankvanPuffelen You should probably write an answer following this JavaScript SDK approach!
Answer: You may use a Cloud Function triggered via the onDelete event, see  https://firebase.google.com/docs/functions/firestore-events.
Then, in the Cloud Function, you would use the Google Cloud Storage Node.js Client API to delete the file in Cloud Storage.
Let's imagine that bucket and name are fields of the Firestore document that hold the file bucket and name (i.e. "img property that reference to the URL of the image"), then you could do something along the following lines:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const defaultStorage = admin.storage();

exports.deleteFile = functions.firestore
    .document('fileDocuments/{fileDocID}')
    .onDelete((snap, context) => {
      const deletedValue = snap.data();
      const fileBucket = deletedValue.bucket;
      const fileName = deletedValue.name;

      return defaultStorage
        .bucket(fileBucket)
        .file(fileName)
        .delete();

});

You may study the following official Cloud Functions sample or documentation items:
https://firebase.google.com/docs/functions/video-series
https://github.com/firebase/functions-samples
https://cloud.google.com/nodejs/docs/reference/storage/2.5.x/File#delete
Comment: so how to actually use this in an angular app?
Comment: @OmarAbdelhady You may have a look at the solution proposed by Frank, in the comment to your question. It is actually an easier solution, which consists in deleting the file from your app through the JavaScript SDK (https://firebase.google.com/docs/storage/web/delete-files). The solution based on Cloud Function, as proposed in my answer, is probably not necessary and is more complex.


How to Upload png file to database using url?

Question: hi fellow programmers I am new in web programming, I have a task that I need to upload png files to sql database using url.
<code>$connection = mysqli_connect ($db_host,$db_user,$db_pass);

mysqli_select_db($connection,$db_name);

$sql1 = "INSERT INTO figures (Id, Name, Image) VALUES ('".$_GET['id']."', '".$_GET['Name']."', '".$_GET['Image']."')";
mysqli_query($connection,$sql1);
</code>
Those lines used to send data , in this way I can upload VARCHAR to the database how ever I cannot upload png ,I don't know how to define file path here. (Maybe if you know easier solution for my task, could you please share it? )
Thanks in advance!!
Answer: First of all you need to set the sending form to multipart and also change the method to POST:
<code><form method="POST" action="uploader.php" enctype="multipart/form-data">
 <input type="file" name="myfile">
 <input type="text" name="id">
 <input type="submit" value="Upload">
</form>
</code>
in PHP page you can get the file data using $_FILES:
<code>// I dont know what is id here? shouldn't be the auto Primary key?
$id=$_POST['id'];
//Get name of file
$imageName=$_FILES["myfile"]["name"];
//Get the binary data of the image
$imageData= addslashes(file_get_contents($_FILES['myfile']['temp_name']));

$connection = mysqli_connect ($db_host,$db_user,$db_pass);
mysqli_select_db($connection,$db_name);
$sql1 = "INSERT INTO figures (Id, Name, ImageData) VALUES ('$id', '$imageName', '$imageData')";
mysqli_query($connection,$sql1);
</code>
Please note that the type of column imageData should be BLOB.
As you have mentioned the you are new to programming, I have to say that you have this option too to uplaod the file directly to the server and only save the url in database.
Comment: Thank you Ali , I know how to do it by submit button but what I really want is :
    "http://localhost/Website/imageData.php?Name=first_pic.png&Image=_____ " using url in a way that I can upload pics to database in from my file folder ,I need to write the file path to the place of underscores. :) . Is there a way to do it ?
Comment: There is no way to pass the direct address of local files to the forms inside browsers. The browser only parses the file data and restricts access to the actual path of files.@KananYagublu If you need this to wok just on your own localhost or just on the server files, I have to suggest another way to read files using serverside process. But you can not read visitors file from server.


dialog box memory leak or stubborn GC?

Question: When I load up my app, It takes around 3Mb of RAM. I have an ImageButton on my first activity which calls the function showRecords, which displays a dialog box.
Once the dialog box is opened, it can be closed by clicking outside of the box (which will call the dismiss() method). The problem I am having is that after the dialog box has been closed, the amount of RAM used by my application does not return to its previous state (it can remain over a Mb more If I include another stuff in the dialog - but the example retains around 2-300kb)
 public void showRecords(View v){
    Dialog recordDialog = new Dialog(this);
    recordDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    recordDialog.setCanceledOnTouchOutside(true);
    recordDialog.setContentView(R.layout.record);
    recordDialog.show();
}

The record.xml file has an image background, and another ImageView with a picture.
I have tried using MAT, and can see no Dialog objects in memory. I also have been using DDMS to perform garbage collections, but the RAM value never returns to what I would expect.
Is something binding to the activity? or is the GC just refusing the reclaim the memory?
As this is my first activity, it remains alive throughout and doesn't seem to close if I need more heap space  (for some reason). Therefore I'm quite interested in reclaiming a Mb of memory when it is not needed.
EDIT - record.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:orientation="vertical"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/recordspage"
        android:layout_marginTop="5dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />

</LinearLayout>

Comment: What is in `R.layout.record`? "doesn't seem to close if I need more heap space (for some reason)" -- that's normal behavior, activities do not automatically close due to low heap space (despite hints in the documentation to the contrary).
Comment: Ah, I didn't realize that was normal behaviour. Thanks for clearing that up. I will add record.xml to my question. I seem to lose some memory even without setting the contentView though.
Comment: Well, some of the memory will be `@drawable/recordspage` and `@drawable/background`, as drawable resources are held onto even if they are not being referenced presently, IIRC. "Is something binding to the activity?" -- if you BACK-button out of the activity, it should no longer show up in MAT. You might also use MAT to sort by biggest memory culprits, and examine it before and after launching the dialog.
Comment: I have tested creating a duplicate of my activity, launching my dialog in the second activity and going back to the first activity. The 2nd does not show up in MAT. I can't find any large amount of memory being used in MAT, I assume this is because bitmaps do not show up. I seem to have around 1.4Mb still remaining from the 2nd activity. Surely they should be removed at this point? Or is the GC so clever that it knows the first activity might like to use these resources at some point. Either way, is there any way I can force the GC to not keep these drawables? perhaps by using recycle.
Comment: "I assume this is because bitmaps do not show up" -- they should on Android 3.0+. In earlier versions of Android, bitmap memory is not reported by MAT. " Either way, is there any way I can force the GC to not keep these drawables?" -- not that I am aware of, if you wish to use them as resources. Bitmaps you load yourself from files or teh Internets you would `recycle()`. You don't have the `Bitmap` objects for the resources, though.
Comment: I can set the background and imageView programatically in the showRecord function and override the dismiss method on the dialog to recycle them perhaps? is there a better way
Comment: I'm testing it out on my phone with is running 2.3.3 I will try it out on an emulator
Comment: I just want to say thanks CommonsWare. You've helped me understand some things. I believe the key issue was, as you correctly pointed out, that the drawable resources were being kept in memory.
Comment: Interesting...when running android 3.0 the memory is freed as soon as the dialog box closes. I guess I must just be sure to Recycle everything that I've finished with!
Comment: They may have gotten smarter about dealing with resource memory.
Answer: OK, now that the analysis is out of the way... :-)
Bitmap resources take a hunk of heap space, because in memory they are held in their raw uncompressed format, compared to the PNG or JPEG you probably started with.
I notice that you are setting a background on a LinearLayout. That certainly works, but bitmap backgrounds are perhaps the biggest memory hogs, just due to their size. You might consider going with a flat color, a ShapeDrawable, or something that is less heap-intensive, if you are concerned about the space. This is particularly true for a dialog box, which is usually on the screen for mere moments.

dialog box memory leak or stubborn GC?


Dynamically write defines in PHP settings file

Question: I have a PHP configuration file which stores settings like:
  define("SETTING_1", "");
  define("SETTING_2", "");
  define("SETTING_3", "");

By default the settings are empty, but then I have an installer file install.php which basically prompts the user for the settings. What is the best way to open the settings file, find the define for each setting and insert the value? Finally, need to save the settings file to disk.
I want to ovoid using regular expressions or simple string search. I came across token_get_all(), does it makes sense to use it?
Answer: token_get_all() isn't really going to help you, since it's just going to give you all of the parse token from the string. You'll spend more time writing a state machine or parser for the result of token_get_all() than you would with the other methods you listed.
The easiest way to achieve this is to use some sort of known, unique placeholder, like this:
define("SETTING_1", "{{SETTING_1}}");
define("SETTING_2", "{{SETTING_2}}");
define("SETTING_3", "{{SETTING_3}}");

Then, you open the file, and replace all of those placeholders with their actual values using str_replace():
$file = file_get_contents( "config.php");
$config = str_replace(
    array( '{{SETTING_1}}', '{{SETTING_2}}', '{{SETTING_3}}'),
    array( $setting1, $setting2, $setting3),
    $file
);
file_put_contents( 'config.php', $config);

That's all it takes.
Answer: Is the define() way definite?
If you really NEED to have it in PHP format, use var_export() and include, which is on the other hand totally insecure... or use INI files, i.e. create ini file, write values in PHP
Comment: Really? Why is something like http://pastie.org/4368244 more insecure than using .ini files? Thanks.
Comment: @Justin because as I see it, you have two options: 1) parse it by hand, 2) include it directly ... option 2 is dangerous, when someone modifies your configuration file by hand and you interpret it each time, your application is executed. that's why i would never allow script to modify a file, which is directly included... and of course, option 1 is ineffective as there are better ways to store configuration...
Answer: Don't use Constants for user settings. It is a bad idea. Create an array and store in a file with var_export with second parameter 'true'. It is readable and can be regenerated by using array functions.


Differential form vs Hyperreal vs Dual number

Question: Differential forms, hyperreals, and dual numbers all seem to sort of do something similar: formalize the notion of the infinitesimal.
How are they related to each other, and in what ways are they different? I know that the hyperreals sort of "extend" the real numbers via nonstandard analysis, but how is this different from what dual numbers do? Similarly, differential forms seem to be able to generalization to higher dimensions -- is this something that hyperreals cannot do?
Comment: One difference of hyperreals vs dual numbers is the hyperreals still form a field, but the dual numbers can't always have division (specifically the element $0 + b\epsilon$ has no inverse).  Also, see [here](https://math.stackexchange.com/questions/1839497/what-is-the-difference-between-hyperreal-numbers-and-dual-numbers) for more discussion.
Comment: Another difference of all of the above is that hyperreals/dual numbers are sort of number systems (elements of them are "constant"), and differential forms are certain kinds of functions (elements of them are not as constant).  Additionally, while $d^2\omega = 0$ for differential forms, and $\epsilon^2 = 0$ for dual numbers, $d^2\omega = d\circ d(\omega)$ is applying the differential operator twice, while $\epsilon\times \epsilon$ is multiplication.
Comment: You may be interested in looking at some texts on [synthetic differential geometry](https://en.wikipedia.org/wiki/Synthetic_differential_geometry)
Answer: The dual numbers are useful both in physics and mathematics, including developing the theory of Lie algebras. In fields like analysis and differential geometry, the dual numbers are of limited utility because they don't have a number of properties possessed by more advanced number systems such as the hyperreals.
The dual numbers cannot be included in the hyperreals because of the peculiar properties of the dual numbers that make them useful in physics, e.g., $\epsilon^2=0$ which cannot be satisfied in the hyperreals since the latter form a field.
When Riemann originally developed a field today called Riemannian geometry, he defined a metric as a weighted sum of quadratic expressions in the differentials $dx^i$ where each $dx^i$ is an infinitesimal increment of the coordinate $x^i$.
The expressions of the form $dx^i$ were eventually reinterpreted in an infinitesimal-frei manner, as sections of the cotangent bundle on the manifold. This is a more advanced topic than infinitesimal calculus.
Answer: I think the notation can causes confusion:
Differential forms are not infinitesimal quantities although the notation (e.g. dx) may incorrectly suggest otherwise. The d in front of dx stands for the exterior derivative and should not be confused with the d in front of an infinitesimal, e.g. dX. As a form dx is a linear function that takes a vector as argument and spits out a finite real number.


How to prove that it is a topology on this set?

Question: How to prove that this  $$D_n=\{p\in E, p|n\}$$ can define a topology on the set $$E=\{n\in\mathbb{N}, n\geq2\}$$
Comment: Do you mean that these are the sets in the topology or that $D_n$ form a basis for the topology?
Comment: Have you started with the axioms? Is $\emptyset$ in $D_n$ for any $n$? If you say $D_1 = \emptyset$, then yes. Next, let $\mathcal{T} = \{D_n,n\ge 1\}$. Is $E \in \mathcal{T}$? Because a topology requires the full set be there. Then, for any subset $\mathcal{T}' \subset \mathcal{T}$, do you have $\bigcup_{D_n \in \mathcal{T'}} D_n \in \mathcal{T}$? Can you take unions?
Finally, you need finite intersections (you can just show that you can take the intersection of any two arbitrary sets $D_n$ and that suffices).
Comment: @InterstellarProbe: Why $n\ge 1$?
Comment: @celtschk because without $n=1$, you do not get the empty set.
Comment: @InterstellarProbe i don't know how to obtain $E$ in the topology, and also what is the intersection an the union i don't know how to use p is a devisor of 2
Comment: @Poline Sandra, let $P = \{p_1,p_2,p_3,p_4,\ldots \} = \{2,3,5,7,\ldots \}$ be the set of prime numbers. First show that $D_{p_k} \subset D_{p_k^n}$ for any prime $p_k$. Next, show that $D_{p_k} \subset D_{np_k}$ for any $n\in \mathbb{E}$ and any prime $p_k$.

Then, because $\mathcal{T} = \displaystyle \bigcup_{n\ge 1}\{D_n\}$, use the prime factorization theorem to show that you can find a tower of proper subsets of $D_n$'s by multiplying by prime powers and increasing the powers of each prime already in the list. Example:
Comment: $D_{p_1^1} \subsetneq D_{p_1^2p_2^2} \subsetneq D_{p_1^3p_2^3p_3^3} \subsetneq \cdots$

Use that to show that the limit of this tower must be $E$.
Comment: what  is $p_k^n$ ?
Comment: @InterstellarProbe: I actually meant, why do you exclude $n=0$.
Comment: @celtschk that is a good point. If you include $n=0$, that instantly gives you $E$.
Comment: @Poline Sandra it is the $k$-th prime number taken to the $n$-th power. Have you not seen subscript and power notation before?
Comment: No i don't understant this set at all, what i understand in the comments is that $D_0=E$, $D_1=\emptyset$ and the topology may be all the union of the sets $D_n$ @InterstellarProbe
Comment: You have $p_k$ is the $k$-th prime number. For example, if $k=1$, then $p_k=2$.

$D_2 = \{n\in E, n|2\} = \{2\}$ because 2 is the only divisor of 2 that is greater than 1.

Now, if we consider $p_1^5$, we are looking for the divisors of $2^5=32$. Thus

$D_{p_1^5} = D_{32} = \{2^1,2^2,2^3,2^4,2^5\} = \{2,4,8,16,32\}$
Answer: @Osama Ghani asked, this question really boils down to whether you are looking for the basis of a topology or the topology itself. If you are looking for the topology itself, then no, the set of all such $D_n$'s is not a topology.
To prove it, let $\mathcal{T} = \{D_n, n\in \mathbb{N} \}$. Then $D_2 = \{2\}$ and $D_3 = \{3\}$, but $D_2 \cup D_3 = \{2,3\} \notin \mathcal{T}$ since any number divisible by 2 and 3 is at least 6, and so would also be divisible by 6. (We can check 2,3,4,5 and show that none of them are divisible by both 2 and 3, and any number larger than 5 that is divisible by 2 and 3 is also divisible by 6).
But, if you were to define $\mathcal{B} = \{D_n, n\in \mathbb{N} \}$ and take all intersections and finite unions, you can easily show you would have a topology. But, that is how you can extend any subset of the powerset of a set into a topology on that set, so there is nothing special about sets of divisors.
Comment: can you explain me more,  i steal don't know how to solve this exercise
Comment: To prove that the set $\{D_n\}_{n \in \mathbb{N}}$ forms a basis for a topology, you need the following:
1. For each $e\in E$, there exists $n\in \mathbb{N}$ such that $e\in D_n$. To prove this, use $D_e$ since every natural number divides itself.

2. For any $e\in E$ and $n_1,n_2 \in \mathbb{N}$ such that $e \in D_{n_1}\cap D_{n_2}$, there exists $n_3 \in \mathbb{N}$ such that $e \in D_{n_3} \subset D_{n_1}\cap D_{n_2}$ (hint: think GCD).


Bootstrap:Fixed nav-static-top on top but scrollable nav-collapse

Question: I am using bootstrap to design my site.
Base on this fiddle
I made the nav-static-top to fixed on top. Now the nav-collapse has to many contents and i can't browse to the last menu because the nav-static-top is fixed on top. Is there a way that the
nav-static-top is fixed on top and the nav-collapse content is scrollable so i can scroll to choose on the content?
i've than something like this but i doesn't look good
and i think android devices don't support  overflow:scroll
Answer: Found a better way. Tested on iphone 4 only.
@media (max-width: 767px) {
.body-container {
    padding-top: 50px;
    margin-left:10px;
    margin-right:10px;
}

.navbar-static-top{
    position:fixed;
    width:100%;
    z-index:1000;
}
.navbar-responsive-collapse{
    max-height: 350px;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

}

Answer: This doesn't completely fix your problem, but it definitely looks better if you do this:

instead of

I don't know about doing it on Android, but if it really doesn't work this way, you'll probably have write some JS or something.


Finding partial log-likelihood of survival data

Question: Cox proportional hazards model:
$$ \lambda_2(t)=\lambda_1(t)e^{\beta}$$
I have survival data for two different groups
I would like to find the profile of the partial log-likelihood for the data as a function of $\phi$, the hazard ratio $\phi=\exp(\beta)$, and produce of plot in R but I am not sure of where to start.
I understand how to calculate the partial log-likelihood for $\beta$ but not sure how to do it for $\phi.$
Answer: Just use the formulas $\phi=\exp(\beta)$ and $\beta=\log \phi$ to move between the $\beta$ and the $\phi$ scales. Most simply, after you have found the profile of the log partial likelihood in terms of $\beta$, you can just re-express the values on the horizontal axis in the $\phi$ scale. The plots will be most symmetric, however, if you log-transform the $\phi$ scale of the horizontal axis, which effectively maps back to a linear $\beta$ scale.
To illustrate in detail, the Cox partial likelihood can be written in terms of $\beta$ and covariate values $X_i$ for individuals $i$:
$$\prod_{i=1}^{n}\left(\frac{\text{exp}(\beta X_i(t_i))}{\sum_{j\in\mathcal{R(t_i)}}\text{exp}(\beta X_j(t_i))}\right)^{\delta_i}$$
where the product is over the event times $t_i$, $\delta_i$ is 0/1 for censored/uncensored observations, and $\mathcal{R(t_i)}$ is the set of cases at risk at time $t_i$.
In terms of the hazard $\phi_i=\exp(\beta X_i)$ for case $i$ with time-constant covariates, the partial likelihood can be written:
$$\prod_{i=1}^{n}\left(\frac{\phi_i}{\sum_{j\in\mathcal{R(t_i)}}\phi_j}\right)^{\delta_i}.$$
The log partial likelihood is then:
$$\sum_{i=1}^n \left(\log(\phi_i)-\log\sum_{j\in\mathcal{R(t_i)}}\phi_j\right),$$
where the sum is only over cases $i$ with observed event times.
With the data you provided in an earlier version of the question, containing only 5 event times and your single binary Group predictor, you can do this pretty much by hand.  Take $\phi_i=1$ for Group 1 ($\log(\phi_i)=0$) and $\phi_i=$HR for Group 2. Then you proceed event time by event time. Your data, in event-time order, are:
    time status group
1  0.028      1    G1
9  0.217      1    G2
2  0.547      0    G1
10 0.598      0    G2
11 0.822      0    G2
3  0.934      0    G1
4  1.194      0    G1
12 1.395      0    G2
5  1.415      1    G1
13 1.626      0    G2
6  1.908      0    G1
14 3.347      1    G2
7  3.378      0    G1
8  3.440      1    G1

Based on the cases at risk at each of the 5 event times, you can write the following for the log partial likelihood as a function of HR:
lplHR <- function(HR){
              (log(1)-log(8+6*HR)) +
              (log(HR)-log(7+6*HR)) +
              (log(1)-log(4+2*HR)) +
              (log(HR)-log(2+HR)) +
              (log(1)-log(1))}

and then plot the results.
curve(lplHR(x), from = exp(-1.5), to = exp(2.5), log ="x",
       xlab = "HR", ylab = "log partial likelihood", bty = "n")

The log scaling of the HR horizontal axis is a linear scaling in terms of the corresponding $\beta$ values.
For more complicated situations, as you understand, you start with a set of hypothesized values of $\beta$. For each value, you extract the log partial likelihood from a Cox model that is restricted to have that value. For a single coefficient as in your case, this can be done by initializing $\beta$ to that value and preventing the function from iterating beyond the initial estimate. That's illustrated on this page. When there are more predictors in the model, for each hypothesized $\beta$ for the predictor $X$ of interest you fit a model with an offset term for $\beta X$ and let the function optimize the other coefficients. That's illustrated on this page.
To do either of those in terms of $\phi$ instead of $\beta$, set up your hypothesized set of values of $\phi$ and use $\log \phi$ wherever you would have used $\beta$ or $\beta X$ previously. Following the first method for a set of hypothesized HR values and your data in a data frame sData:
library(survival)
HRs<-seq(exp(-1.5), exp(2.5), length=100)
llik<-double(100)
for(i in 1:100){temp <- coxph(Surv(time,status) ~ group ,data = sData,
                init =  log(HRs[i]), iter.max = 0); llik[i ]<- temp$loglik[2]}
plot(HRs, llik, type = "l", bty = "n", log = "x")

The plot (not shown) is the same as above.


Almost-identity: $[\int_0^\infty{\rm d}x-\sum_{x=1}^\infty] \prod_{k=0}^N\text{sinc}\left(\frac{x}{2k+1}\right) = \frac{1}{2}$

Question:
Show that the identity
  $$\int_0^\infty \prod_{k=0}^N \text{sinc}\left(\frac{x}{2k+1}\right)\,{\rm d}x - \sum_{n=1}^\infty \prod_{k=0}^N \text{sinc}\left(\frac{n}{2k+1}\right) = \frac{1}{2}$$ where $\text{sinc}(x) = \frac{\sin(x)}{x}$ holds for $N=0,1,2,\ldots,40000$ but fails for all larger $N$.

I remember seeing this strange identity a few years ago and it stuck to my mind, but unfortunately I can't find the source of this right now and it's reconstructed from memory (and checked with a computer for small $N$ although $40000$ might not be accurate). This is why I'm asking it here.
If I remember correctly it's closely linked to Fourier transforms and for $N$ larger than $\sim 40000$ the difference between the left and right hand side should be smaller than $\sim 10^{-10000}$ so the agreement is extremely good.

Do anyone know the source of this problem or otherwise how to solve it? What is the theory behind it (i.e. why does it break down at some finite value)?
Comment: [This paper](http://arxiv.org/pdf/1105.3943v2.pdf) has the identity, and states that it holds for $N \le 40248$.
Comment: This seems like a more extreme version of the [Borwein Integral](https://en.wikipedia.org/wiki/Borwein_integral).
Comment: @JimmyK4542 Thank you very much for the reference. You should add it as an answer if you feel like it. A search using $40248$ gives [the paper](https://www.carma.newcastle.edu.au/jon/sinc-sums.pdf) I remember reading.
Comment: Now everything makes much more sense: $\sum_{k=0}^{40248}\frac{1}{2k+1} = 0.99999\cdot 2\pi$ (and the sum exceeds $2\pi$ on the next number) so that explains the strange number...and there is nothing special about $1/(2k+1)$ one can use any positive sequence $a_k$ here as long as it sums to less than $2\pi$.
Answer: There is a theorem stated on page 2 of this paper, which states that for $N+1 > 1$ positive numbers $a_0,a_1,\ldots,a_N > 0$ the identity
$$\dfrac{1}{2}+\sum_{n = 1}^{\infty}\prod_{k = 0}^{N}\text{sinc}(a_kn) = \int_{0}^{\infty}\prod_{k = 0}^{N}\text{sinc}(a_kx)\,dx$$
holds provided that $\displaystyle\sum_{k = 0}^{N}a_k \le 2\pi$. (Also, for $N = 0$, the identity holds if $a_0 < 2\pi$).
Since $\displaystyle\sum_{k = 0}^{40248}\dfrac{1}{2k+1} \approx 6.283175 < 2\pi < 6.283188 \approx \displaystyle\sum_{k = 0}^{40249}\dfrac{1}{2k+1}$, the identity holds for $1 \le N \le 40248$ but fails for $N \ge 40249$.
The proof of this theorem has to do with Fourier transforms. Evaluating the Fourier transform of a function at $0$ gives you the integral of the function over $\mathbb{R}$. The Fourier transform of the product of sinc functions is a convolution of "rectangle" functions whose widths are proportional to $a_k$. If you convolve enough rectangle functions together, the value of the result at $0$ changes. This is an intuitive/non-rigorous explanation. I'm sure a more rigorous explanation can be found online.


An inequality in expected values of Random Variable with different PDFs

Question:
Problem. Prove that
$$E[\log f(X)] \ge E [\log q(X)], $$
if $X$ is a random variable with PDF $f(x)$ and $q(x)$ is any valid PDF.
Hint provided: Use Jensen's Inequality.

This means:
\begin{gather*}
E [\log f(X)]
= \int f(x) \log f(x) \, \mathrm{d}x
\ge E[\log q(X)]
= \int f(x) \log q(x) \, \mathrm{d}x \\[0.75em]
\implies
-\int f(x) \log \biggl(\frac{q(x)}{f(x)}\biggr) \, \mathrm{d}x \ge 0.
\end{gather*}
Now, I am unable to understand how to move forward to use Jensen's inequality and thus get according expression.
Answer: $$ E\biggl[ \frac{g(X)}{f(X)} \biggr]
= \int \frac{g(x)}{f(x)} f(x) \, \mathrm{d}x
= \int g(x) \, \mathrm{d}x
= 1 .$$
By Jensen's inequality applied to the convex function $-\log x$ we get
$$ 0
= -\log E\biggl[ \frac{g(X)}{f(X)} \biggr]
\le - E \biggl[ \log \frac{g(X)}{f(X)} \biggr]
= - E [\log g(X)] + E [\log f(X)]. $$
Hence, $E[\log g(X)] \leq E[\log f(X)]$.
Comment: Perfect
Thank you!!


Exchange: Allow non-owner of meeting to change location/room

Question: We've been asked to do something with rooms and not too sure how..

We have 4 rooms at a conference that can be booked by any of the sales team
We have a group of admins that should be able to change these meetings, including:

Change times (done)
Change Room

We're ok with the time change, setting them as owner on the \Calendar folder of the room mailbox allows this.
What I cant find is a way to allow them to change the room/location. They can type over the "location" field, and save this, but the meeting stays in the "old" room's calendar.
eg: Bob books a meeting in room 1. An hour later, Fred needs to book it - Fred is friends with the boss, so it must be done. The admin can open Bob's meeting and see attendees, but cannot use the scheduling assistant to select a new room, and overtyping the "Location" field just changes text, it stays in room1's calendar.
Any idea how we can allow these admins to change the room for a meeting they do not own?
Answer: Does the "admin" have delegate access to Bob calendar?  That's the only way they'll have the right to change a meeting room.  Bob would need to delegate the right to the admin.  This is not a problem of the meeting room, rather a problem with delegate access or the "organizer".
Edit 1:
From the meeting room side, one option might be to give the admin the power to decline a meeting.  If nothing else, that would force "bob" to re-book a different room.
Comment: Thanks Eric, i feared this - I'm not sure we can give delegate access to all the sales folks, but I'll leave this as a last resort solution. As for meeting room delegate, this is another possibility, but the issue we're going to have to solve is moving rooms post-booking.


digitalocean api : get droplets without tag name

Question: How can I get droplets without having tag-name using cli reference?
doctl compute droplet list --tag-name "what should be provided here"

Tried:
doctl compute droplet list --tag-name ""

doctl compute droplet list --tag-name ''

Answer: I think (haven't tried) that you can't test for the absence of a tag.
doctl compute droplet list only accepts a single --tag_name and so I assume (!?) the (server) implementation filters the list of Droplets by those that include the tag value.
What you may (!?) be able to do is to list everything as JSON and then post-filter the results for those with empty ([]) tags.
Comment: Just for reference, in Powershell you can do this by
` doctl compute droplet list -o json|ConvertFrom-JSON|Where-Object -Not tags`


Secure File Upload

Question: I need the ability for clients to send me files. I would like them to use web services, but they are unable to do this (they want to just FTP a file over like they are used to, so this is what I have accepted).
I run Windows servers but am a little skeptical about housing an FTP server in my network with my app servers/DB servers since I am aware of the security issues surrounding an FTP server.
What is the best way to deal with this?
Comment: Remote services like cjc suggested are one way. The other is to vlan isolate an FTP box on your side and just firewall it off from anything except accepting FTP connections from your desktop machines and the outside world.
Answer: You can set a FTP on, say, a VPS or EC2 instance far, far away from your network and tell them to put the files there.  After they upload, you can go fetch them yourself.  That way, they have FTP/SFTP, and that service is separate from your own network.  An EC2 Micro instance runs around $15/month, plus whatever you'd need for storage if you attach an EBS to it.  You can spin the EC2 instance up when you need it, if the transfers don't happen that often.
Answer: The issue with FTP server is that mainly its unencrypted, you would ideally want to install an SSL certificate to ensure you are protected.  Also as having strong passwords for all accounts on the FTP server is important.
If you are still worried, use a 3rd Party FTP server such as Filezilla's:
http://filezilla-project.org/download.php?type=server
You can then have a very limited set of users and just start and stop the service for when it is needed.
Comment: Alternatively there are many 3rd party applications both Open Source and Proprietary that implements sftp for windows
Answer: Have a look at CrushFTP  The install is simple and you can enable many different protocals.  We use HTTPS and it works very well.  Too many features to list here but very resonably priced and support is great.  Free trial as well.  You can brand the site with your logos.  It also has great security and will ban attacks like dictionary attacks and alert you.
Really works well for us and users simply need a browser.
Answer: Security issues on FTP servers are primarily related to clear-text passwords on the wire.
I see here three possible solutions to this issue:

Webdav with SSL. Can be used with some FTP clients, web browsers should work fine also. Lot of authentication methods, can run on a webserver (thus eliminating the need of a separate daemon).
FTPS: FTP over SSL. Server certificate for encryption, optional client certificate for authentication. Servers have also a lot of user auth methods: database, local users, ldap...
SFTP: FTP over SSH. Also compatible with some FTP clients. ssh auth options available. passwords, ssh keys... May not be windows friendly however (server-side).
Answer: It's kind of interesting that people are so stuck in their mind that they still want to use FTP.
FTP is a prehistoric protocol that was never designed for secure communication.
What about providing a secure web file manager where the users can simply drag and drop multiple files directly into the browser?
Or use a secure WebDAV server, where the users can map the far side server as a Windows disk?
Take a look at BarracudaDrive, which provides a solution to the two above mentioned uses cases.
http://barracudaserver.com/products/BarracudaDrive/

Secure File Upload


What do I have to do to see the generated SQL with JBoss 6?

Question: How can I make JBoss 6.0M2 show me the SQL that it generates and sends to the database?
Answer: I have used p6spy for this in the past:
http://sourceforge.net/projects/p6spy/
Hope that's helpful.


Estimator that maximizes t-value

Question: Consider the linear regression model:$$y=\beta_{0}+\beta_{1}x+u$$
The OLS estimators minimze the squared sum of residuals. By the Gauss-Markov theorem, OLS estimators give the lowest variance of estimates in the class of unbiased estimators. However, we also know that estimators such as Ridge estimators exploit the Bias-Variance tradeoff and are able to achieve higher efficiency. Is there an estimator that would maximizes the t-value of a test of the null:$$H_{0}:\beta_{1}=0$$
  or the ratio$$\frac{\hat{\beta}}{SE(\hat{\beta})}$$
Comment: In multiple regression? Which t-value are you going to maximize?
Answer: You do know that not all estimators of $\beta$ have a $t$-distribution when divided by an estimator of their sampling variation, right?
If you want to maximize that ratio, just use an estimator that is always equal to some constant, $c$ (pick any value for $c$ that you want). Then, the standard error is zero, and that ratio is infinite.

Estimator that maximizes t-value


Lambda Expression : How to Concat 2 Fields?

Question: I want to merge 2 database field and populate as an item to Dropdownlist by using Lambda Expression. Here is my lambda expression for populating dropdownlist on my Controller. I have tried lots of combination, but I could not merge the fields. Could you help me please how can I do this? Thanks.
Note : Here I want to merge CityName and MeetingDate fields like Paris 01.01.2014 as one item of dropdownlist.
 private void PopulateMeetingsDropDownList(object selectedMeetings = null)
    {
        var meetingsQuery = repository.Meetings
            .Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
                    (m, c) => new
                    {
                        CityID = c.CityID,
                        CityName = c.CityName,
                        MeetingDate=m.MeetingStartDate
                    })
             .OrderBy(x => x.CityID).ToList();
        ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "CityName", selectedMeetings);
    }

Answer: What about instead of separated CityName and MeetingDate return concatenated DisplayValue.
Updated code:
private void PopulateMeetingsDropDownList(object selectedMeetings = null)
{
    var meetingsQuery = repository.Meetings
        .Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
            (m, c) => new {
                CityID = c.CityID,
                CityName = c.CityName,
                MeetingDate=m.MeetingStartDate
            }
        )
        .OrderBy(x => x.CityID)
        .AsEnumerable()
        .Select(
            i => new {
                CityID = i.CityID,
                DisplayValue = string.Format(
                    "{0} {1:dd.MM.yyyy}",
                    i.CityName, i.MeetingDate)
            }
        ).ToList();
    ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "DisplayValue", selectedMeetings);
}

.AsEnumerable() "splits" this query to two parts, first LINQ2SQL and the second LINQ2OBJECT. The 1st part will be executed in database, the 2nd in .NET (locally). Usually this is preferred way (hint: performance) - filtering, sorting, grouping, etc. on db, other things in application.
More details: https://stackoverflow.com/a/17996264/1027198, https://stackoverflow.com/a/17968688/1027198.
Comment: Thank you very much Michal for your good answer. I proposed your reply as an answer. But unfortunately I cannot Vote Up your reply due to not having enough reputation :(
Answer: You rock! Thank you very much for your great help. Actually I have encountered "LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression." error message after first applying your method, but by adding ".ToList()" to the third line I have succeeded. So, for informing those who might need this great functionality here is the last state of the method in my Controller:
private void PopulateMeetingsDropDownList(object selectedMeetings = null)
{
    var meetingsQuery = repository.Meetings.ToList()
        .Join(repository.Cities, m => m.MeetingCityId, c => c.CityID,
                (m, c) => new
                {
                    CityID = c.CityID,
                    DisplayValue = string.Format("{0} ({1:dd MMMM yyyy})", c.CityName, m.MeetingStartDate)
                })
       .OrderBy(x => x.CityID).ToList();
    ViewData["MeetingId"] = new SelectList(meetingsQuery, "CityID", "DisplayValue", selectedMeetings);
}

I think there is no problem with using ".ToList()" 2 times on the 3rd and 10th lines. Could you clarify please? Thank you very much again for your help. BR.
Comment: Ok, thanks for editing so that other people can use the corrected example.
Comment: Sorry, I've forget about that. I've corrected answer, check it and added links for further details.


método não aplicado em java

Question: Estou fazendo um programa para cadastrar dados de uma locadora de filmes, onde cada registro é composto pelo nome do filme, pelo gênero e pelo código do filme, usando métodos e vetores. Ele possui 3 arquivos:
1. Código principal
package Atividades;

import java.util.Scanner;
import Atividades.Filme;
import application.AlunoVet;

public class AlunoVetMain {

    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) {

        char opcao;

        LocadoraVet Locadora = new LocadoraVet();

        do {
            System.out.println("[1]Cadastrar Filme" + "\n[2]Consultar Filmes por gênero"
                    + "\n[3]Excluir filme do arquivo pelo nome\n[4]Sair");
            opcao = scan.next().charAt(0);
            switch (opcao) {
            case '1':
                cadastrarFilme(Locadora);
                break;
            case '2':
                consultarFilmes(Locadora);
                break;
            case '3':
                System.out.println("c");
                break;
            case '4':
                System.out.println("Fim das operações");
                break;
            default:
                System.out.println("Opção Inválida. Tente novamente");
            }
        } while (opcao != '4');

    }

     static void cadastrarFilme(LocadoraVet t) {
        Filme filme = new Filme();
        String nome;
        char genero;
        int codigo;
        System.out.println("Digite o nome do filme: ");
        nome = scan.next();
        System.out.println("Digite o gênero do filme:"
                + "\n[T]Terror"
                + "\n[D]Drama"
                + "\n[A]Ação");
        genero = scan.next().charAt(0);
        System.out.println("Digite o código do flme:");
        codigo = scan.nextInt();
    }

    static void consultarFilmes(LocadoraVet t) {
        int posicao;
        char genero;
        System.out.println("Digite a matricula do aluno:");
        genero = scan.next().charAt(0);
        posicao = t.consultarFilmes(genero);
        if (posicao == t.getQuantVetor()) {
            System.out.println("o aluno não foi encontrado.");
        } else {
            System.out.println(t.getVetor(posicao));
        }
    }

    }

2. A classe com os atributos, construtores e getters e setters:
package Atividades;

public class Filme {
    private String nome;
    private char genero;
    private int codigo;

    public Filme() {
        this.nome = nome;
        this.genero = genero;
        this.codigo = codigo;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public char getGenero() {
        return genero;
    }

    public void setGenero(char genero) {
        this.genero = genero;
    }

    public int getCodigo() {
        return codigo;
    }

    public void Codigo(int codigo) {
        this.codigo = codigo;
    }

    @Override
    public String toString() {
        return "Filme [nome=" + nome + ", genero=" + genero + ", codigo=" + codigo + "]";
    }

}

E a classe do vetor com seu construtor, getters e setters e onde eu guardo os métodos das operações:
package Atividades;

import application.Aluno;

public class LocadoraVet {
    private Filme[] vetor;
    private char quantVetor;

    public LocadoraVet() {
        this.vetor = vetor;
        this.quantVetor = quantVetor;
    }

    public Filme[] getVetor() {
        return vetor;
    }

    public void setVetor(Filme[] vetor) {
        this.vetor = vetor;
    }

    public char getQuantVetor() {
        return quantVetor;
    }

    public void setQuantVetor(char quantVetor) {
        this.quantVetor = quantVetor;
    }

    // inserir um aluno no objeto vetor
        public boolean cadastrarFilme(Filme novo) {
            if (this.quantVetor == this.vetor.length) {// saber se o arquivo cheio
                return false;
            } else {
                this.vetor[this.quantVetor] = novo;
                this.quantVetor++;
                return true;
            }
        }

        // pesquisar a posição de um aluno no vetor
        public char consultarFilmes(char genero) {
            for (int i = 0; i < this.quantVetor; i++) {
                if (this.vetor[i].getCodigo() == genero) {
                    return i;
                }
            }
            return this.quantVetor;
        }

}

Estou fazendo o método para consultar os filmes de um determinado gênero. O gênero é informado pela letra ( [T]Terror, [D]Drama, [A]Ação ) e uso a tipo primitivo char, no método consultarFilmes(LocadoraVet t)  o filme está dando erro, alguém pode me ajudar a resolver? Agradeço desde já.
Answer: public char consultarFilmes(char genero) {
        for (int i = 0; i < this.quantVetor; i++) {
            if (this.vetor[i].getCodigo() == genero) {
                return i;
            }
        }
        return this.quantVetor;
    }

Voce esta retornando um int nao um char;
Para corrigir
import java.util.Scanner;

public class AlunoVetMain {
   static Scanner scan = new Scanner(System.in);

   public static void main(String[] args) {
       Integer index = 0;
       Filme[] filmes= new Filme[100];
       char opcao;

       LocadoraVet Locadora = new LocadoraVet(filmes, index);

       do {
           System.out.println("[1]Cadastrar Filme" + "\n[2]Consultar Filmes por gênero"
                   + "\n[3]Excluir filme do arquivo pelo nome\n[4]Sair");
           opcao = scan.next().charAt(0);
           switch (opcao) {
               case '1':
                   cadastrarFilme(Locadora);
                   break;
               case '2':
                   consultarFilmes(Locadora);
                   break;
               case '3':
                   System.out.println("c");
                   break;
               case '4':
                   System.out.println("Fim das operações");
                   break;
               default:
                   System.out.println("Opção Inválida. Tente novamente");
           }
       } while (opcao != '4');

   }

   static void cadastrarFilme(LocadoraVet t) {
       Filme filme = new Filme();
       System.out.println("Digite o nome do filme: ");
       String nome = scan.next();
       System.out.println("Digite o gênero do filme:"
               + "\n[T]Terror"
               + "\n[D]Drama"
               + "\n[A]Ação");
       char genero = scan.next().charAt(0);
       System.out.println("Digite o código do flme:");
       int codigo = scan.nextInt();

       t.cadastrarFilme(new Filme(nome,genero,codigo));

   }

   static void consultarFilmes(LocadoraVet t) {
       char genero;
       System.out.println("Informe o Genero :   +" +
               " \n[T]Terror" +
               " \n[D]Drama" +
               " \n[A]Ação");
       genero = scan.next().charAt(0);
       System.out.println(genero);
       try{
           var filme = t.consultarFilmes(genero);
           System.out.println(filme.getNome());
       }catch (FilmeNaoExisteException ex){
           System.out.println("Filme nao existe");
       }

   }
}

public class Filme {
    private String nome;
    private char genero;
    private int codigo;

    public Filme() {
    }

    public Filme(String nome, char genero, int codigo) {
        this.nome = nome;
        this.genero = genero;
        this.codigo = codigo;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public char getGenero() {
        return genero;
    }

    public void setGenero(char genero) {
        this.genero = genero;
    }

    public int getCodigo() {
        return codigo;
    }

    public void Codigo(int codigo) {
        this.codigo = codigo;
    }

    @Override
    public String toString() {
        return "Filme [nome=" + nome + ", genero=" + genero + ", codigo=" + codigo + "]";
    }

}

    public class FilmeNaoExisteException extends RuntimeException{

    public FilmeNaoExisteException(String message) {
        super(message);
    }
}

    public class LocadoraVet {
    private Filme[] vetor;
    Integer index;

    public LocadoraVet() {
        this.vetor = new Filme[30];
    }

    public LocadoraVet(Filme[] vetor, Integer index) {
        this.vetor = vetor;
        this.index = index;
    }

    // inserir um aluno no objeto vetor
        public Filme cadastrarFilme(Filme novo) {
            this.vetor[index] = novo;
            index++;
            return novo;
        }

        // pesquisar a posição de um aluno no vetor
        public Filme consultarFilmes(char genero) {
            if (this.vetor.length==0){
                throw new FilmeNaoExisteException("nao existe nenhum filme");
            }

            for (int i = 0; i < this.vetor.length; i++) {
                if (this.vetor[i].getGenero() == genero) {
                    return this.vetor[i];
                }
            }
            throw new FilmeNaoExisteException("Filme nao encontrado");
        }

}

Comment: Vc precisa retornar a lista de filmes, Recomendo vc utilizar um list em vez de array puro e criar um filter https://www.baeldung.com/java-stream-filter-lambda
Comment: Deu certo amigo, muito obrigado pela atenção, tá funcionando normal
Comment: mas agora quando eu cadastro 2 filmes, quando vou consultar os filmes por gênero só aparece o primeiro filme cadastrado, o segundo não aparece, sabe o que pode ser isso?

método não aplicado em java


Azure VM: Why do I only have an option for Windows Server 2016 Data Center

Question: Very Simple question. Not new to Azure, but new to setting up environments.  Im trying to setup a simple DEV env and I don't need Data Center.  Why do I only have an option for Windows 2016 data center and not just a regular Windows 2016 server?
Answer: The Windows Server 2016 Standard edition image is currently not available in Azure Marketplace. You can, however, procure a Windows Server 2016 Standard image and upload it to Azure in the form of a VHD. Then, you can spin up a VM with that OS.
Refer Create a Windows VM from a specialized disk


Find relation between sum of square roots?

Question:
If $x=\sqrt{11}+\sqrt{20}$, $y=\sqrt{15}+\sqrt{17}$ and $z=\sqrt{14}+\sqrt{18}$, then which of the following holds true?

A. x < y < z
B. y < z < x
C. y < x < z
D. x < z < y
E. None of these

Please can anyone tell me how to solve this problem in Aptitude point of view
Comment: What is Aptitude point of view?
Comment: Means i need tricks to solve such question in less than a minute because I am preparing for aptitude.
Answer: $x^2=31+2\sqrt{220}$
$y^2=32+2\sqrt{255}$
$z^2=32+2\sqrt{252}$
Comment: how to find relation now from this?
Comment: @KARTIKAGARWAL You're preparing for an aptitude test. You shouldn't need any hint for this part.
Answer: Note that $x<\sqrt{12}+\sqrt{20}$ and $\sqrt{x}$ is concave.
By the majorization inequality, we get $x<z<y$.
Comment: can you please explain it in detail
Comment: Basically, note that $f(x)=\sqrt{x}$ is a concave function, in the sense that $f''(x)<0$ everywhere in the domain. Thus for all $a>0$ if $a>b>c>0$ then $\sqrt{a-b}+\sqrt{a+b}<\sqrt{a-c}+\sqrt{a+c}$. In your problem I use $a=16$. See https://en.wikipedia.org/wiki/Karamata%27s_inequality.


How does pitch bending work on keyboard percussion

Question: There is a less known technique for keyboard percussion that allows some form of pitch bending.
For example:

These are some way of getting it, but a similar effect is also possible by keeping the bottom of the mallet, and pushing it on a key by applying some horizontal friction in the direction of the key with different pressure, possibly near the string (I'll eventually add a video as soon as I can). While this normally gets the known pitch of the key, applying a varying pressure on it may also affect the tuning to some level.
It can be done on most metal keyboard percussion (vibraphone, glockenspiel, etc.), and some wooden keyboard percussion too, as long as their sustain allows it.
I've known about this since my initial studies, but I never had the opportunity to use it for performance, and therefore I never cared to learn more about it.
Still, I'm curious.

how does this happen?
what is the physics behind it?
what is the extent of the bending for a given "key"?
Comment: I too would like to know the extent of the bending. I wouldn't understand the physics! The percussionists (Hirota/Yamash'ta) with the Red Buddha Theatre used to produce beautiful, down-bending, 4-note chords on the marimba. They would ripple-trem a long-held 4-note Cm chord and, at the peak of a crescendo, quickly move the mallet-heads towards the fronts of the keys, then slide them off and - sub>p - onto the Gm chord a 4th below, resuming the ripple-trem. The instrument seemed to sigh, and by leaning in towards the keys at each climax they gave the impression that the wood itself was bending.
Comment: Sorry - what I described isn't on the record. That section ("Mandala") IS there, but without the bending. Also, I was wrong: it's in Dm. Sorry. So the 'slide' I described must have been from F major down to C major before returning to Dm. (It's at least 50 years ago!)
Comment: ... So at least a fourth in a downward direction.
Comment: @OldBrixtonian That's quite interesting. Can you provide some reference for that? I honestly didn't know Red Buddha Theatre, I've found some sources on Youtube, but they're quite long and I didn't have time to search carefully enough.
Answer: For those who are really curious and not afraid of math, there is a full research article about the subject.
The short summary is that pitch bending by pressing a flexible mallet against the bar works so that the mallet head vibrates together with the bar, effectively adding mass to the bar. A bar that is heavier, but not more rigid, vibrates at lower frequency, somewhat similar to thicker string at same tension. The researchers tried attaching magnets to the bar, and it produces similar pitch change.
Because the strongest vibration mode in the bar is the one where center moves and the nodal points near ends where the bar is suspended stay in place, adding the mass to the center has stronger effect than adding it to near the nodes, and smooth movement of the mallet that touches the bar produces the smooth pitch bend. The amount of the bend is limited by how heavy mallet head can be held against the bar without chattering or absorbing too much energy.
The researchers claim that the practical limit is a semitone, but in the comments to the question there is a story of bending a full 4th, Cm to Gm, on marimba.
Comment: "the strongest vibration mode in the bar is the one where center moves and the ends stay in place"  That is not correct.  In the fundamental mode, the center of the bar moves AND the ends of the bar move.  The bar is stationary at two nodes where the string holes are drilled.
Comment: @ojs » pitch more than the mentioned limit of one semitone. I've some neodymium magnets around I use for fixing sheet music on stands while playing outside, I'll do some experiments and check the result by using both magnets *and* "mallet pitch". I'll eventually add a further answer about my findings, but right now I'm accepting yours as it clearly explains most of my questions. Thanks again!
Comment: I can only speculate on this, but I think that because the wooden bars on marimba are lighter than metal ones on vibraphone, the mallet head is heavier in comparison and changes the frequency more.
Comment: So at one position it should also be possible to change the pitch by pressing more or less hard (adding different weight) as long as it does keep the contact? Nice answer!
Comment: Does that mean the mallet in actuality is vibrating along with the bar, in effect becoming one vibrating object?
Comment: Yes, the mallet head is vibrating along with the bar. I'll update the answer to make it clearer. The trick requires that the mallet shaft is flexible enough to keep up with the vibration and maintain the pressure, and the mallet head is just the right hardness so that it doesn't bounce or absorb too much energy. As far as I can tell, the pressure should not have as much effect as the mass of the mallet head and the position, but I don't have an instrument I could try this with.
Comment: @Edward technically correct, the best kind of correct. I edited it into the address, happy?
Comment: @ojs Thank you for that! I'm not so afraid of the math as I am about trying to dig into it, though ;-) Now that I think about it, what makes it work is that the added mass vibrates along with the bar, and it obviously makes sense: the added mass of a body that can still vibrate with the source without absorbing will decrease the frequency, which is why larger/heavier keys give lower pitch. This may also explain the comment by Old Brixtoninan (and negate that point on the paper, but I'll eventually read it more carefully): if a mallet with more weight is used, it may be possible to affect the »


Survival analysis with incomplete data

Question: I would like to estimate the association between exposure to an environmental contaminant and all-cause mortality. Unfortunately the dataset I can get access to is incomplete - exposure data are only available for approximately 5% of the entire cohort, and these 5% are reasonably random (essentially based on accessibility in a freezer - but not linked to collection date etc).
In an ideal world, I would like to conduct a survival analysis as I could do with other data - but I don't know whether this is really appropriate. Would it be more appropriate to use logistic regression here?
Edit: they are reasonable random as samples were treated after collection and processed in a way that was not linked to collection date/site.
Answer: If the reasons for missingness invalidate a survival regression model, they certainly invalidate a logistic regression as well.
If missingness still gives you a sample that is a simple-random-sample (SRS), then you can analyze those data as-is. The only issue might be small sample size: then collect more data. Weighting or imputation methods would only make a difference if you "knew something" about the remaining samples in the freezer.
It does concern me somewhat that "availability in a freezer" is listed as reason you don't think you might have a non-representative sample. If you "sampled" the average expiry of milk at the grocery store by picking the first cartons from the shelf, the estimate would be biased because of a FIFO system: the older milks are setup to be bought first. In general, anything preferentially sampled because of convenience, time, or space tends to be non-representative (meaning some methods must be applied to calibrate estimates toward a target population).
You can confirm this by actually performing an SRS of specimens from the freezer and ascertaining the collection date and also the exposure to determine if there is an association or otherwise.
Comment: Thanks - the way they were stored ensures that their position in the freezer is not linked to collection site/date. They also don't differ from the rest of the sample in main factors (age, sex, collection date).

Survival analysis with incomplete data


How can I filter records returned by a stored procedure call?

Question: I'm trying to use a stored procedure to search for users in my WinFomrs application. I have this code:
Regex regPattern = new Regex(searchTextBox.Text);
dataGridView1.DataSource = users.GetAllUsers()
                            .Where<users>(item => regPattern.IsMatch(item.FirstName))
                            .ToList<users>();`

But it doesn't work, because my GetAllUsers in my DAL doesn't have a where statement because I used a stored procedure instead of a regular SQL statement so how can I fix this, guys?
Here is my BAL:
public List<GetAllUsers> GetAllUsers()
{
    List<GetAllUsers> UserDatails = new List<GetAllUsers>();
    try
    {
        CacheConnection.ClearParameters();
        dataTable = CacheConnection.GetDataTable("AGSP.Users_GetAllUsers2");
        if (dataTable.Rows.Count != 0)
        {
            foreach (DataRow r in dataTable.Rows)
            {
                GetAllUsers oBo = new GetAllUsers();
                Type myType = typeof(GetAllUsers);
                System.Reflection.PropertyInfo[] properties = myType.GetProperties();
                foreach (System.Reflection.PropertyInfo p in properties)
                {
                    if (dataTable.Columns.Contains(p.Name))
                    {
                        if (ReferenceEquals(r[p.Name], DBNull.Value))
                        {
                            if (p.PropertyType.ToString() == "System.DateTime")
                            {
                                p.SetValue(oBo, DateTime.Now, null);
                            }
                            else if (p.PropertyType.ToString() == "System.String")
                            {
                                p.SetValue(oBo, "", null);
                            }
                        }
                        else
                        {
                            p.SetValue(oBo, r[p.Name], null);
                        }
                    }
                }
                UserDatails.Add(oBo);
            }
            return UserDatails;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return UserDatails;
}

BTW, I'm not using SQL, I'm using Caché programming language for my database but it also has a stored procedure.
I fixed it guys
what i did in my search button is this
Regex regPattern = new Regex(searchTextBox.Text);
        lstUsers.DataSource = users.GetAllUsersByUsername(searchTextBox.Text);

instead of this
Regex regPattern = new Regex(searchTextBox.Text);
dataGridView1.DataSource = users.GetAllUsers()
                            .Where<users>(item => regPattern.IsMatch(item.FirstName))
                            .ToList<users>();`

And also
this is what i did in my DAL
public List<GetAllUsers> GetAllUsersByUsername(string username)
    {
        List<GetAllUsers> UserDatails = new List<GetAllUsers>();
        try
        {
            CacheConnection.ClearParameters();
            CacheConnection.AddParameter("UserName", username);

Comment: You have two options, a, show us more of your code, and a result set of what you expect vs what you get, and also tell us more about what you need, or b, change your stored proc to include a where clause and pass it a parameter.  using a SQL Call \ Stored Proc without a parameter and a where clause is also bad practice.  What will you do when you hit 1 million or so lines and your data is huge and youre bringing back 1 million results for just one record and create yourself performance issues
Comment: It doesn't matter whether it's a stored procedure or a regular `SELECT` statement. What does `GetAllUsers()` return?
Answer: You could pass the search value to the stored procedure instead and then instead of returning all users from the query use a statement like '%Search Term%' in a where statement that returns the results.
or you could put a row filter on the data table to filter the results that way. Before you enter your for loop.
this page has a good description on filtering
https://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter(v=vs.110).aspxenter link description here
this has good example of searching:
http://www.csharp-examples.net/dataview-rowfilter/


Como exibir informações de formulário obtidos em PHP em um modal do Boostrap?

Question: Eu criei um formulário simples com dois campos em uma página construída usando o Bootstrap para cálculo de consumo de combustível. As informações desse formulário são obtidas e tratadas usando PHP. Depois que o usuário clica em 'Calcular', gostaria que os resultados da minha programação PHP fossem exibidas em um modal.
<?php

$mensagem = "";

if ($_POST){

    $distancia = $_POST['distancia'];
    $mediaVeiculo = $_POST['media-veiculo'];

    if (is_numeric($distancia) && is_numeric($mediaVeiculo)){

        if ($distancia > 0 && $mediaVeiculo > 0){

            $valorGasolina = 4.80;
            $valorAlcool = 3.80;
            $valorDiesel = 3.90;

            $calculoGasolina = ($distancia / $mediaVeiculo) * $valorGasolina;
            $calculoGasolina = number_format($calculoGasolina, 2, ',','.');

            $calculoAlcool = ($distancia / $mediaVeiculo) * $valorAlcool;
            $calculoAlcool = number_format($calculoAlcool, 2, ',','.');

            $calculoDiesel = ($distancia / $mediaVeiculo) * $valorDiesel;
            $calculoDiesel = number_format($calculoDiesel, 2, ',','.');

            $mensagem.= "
";
            $mensagem.= "O valor total do gasto será de:";
            $mensagem.= "
";
            $mensagem.= "
Gasolina: R$ ".$calculoGasolina."
";
            $mensagem.= "
Álcool: R$ ".$calculoAlcool."
";
            $mensagem.= "
Diesel: R$ ".$calculoDiesel."
";
            $mensagem.= "
";
            $mensagem.= "
";

        } else {
            $mensagem.= "
";
            $mensagem.= "
Nenhum dos valores informados pode ser igual a 0
";
            $mensagem.= "
";
        }

    } else {
        $mensagem.= "
";
        $mensagem.= "
O valor recebido não é numérico
";
        $mensagem.= "
";
    }
}else {
    $mensagem.= "
";
    $mensagem.= "
Os valores de entrada não foram informados.
";
    $mensagem.= "
";
}
?>

<!DOCTYPE html>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cálculo de consume de combustível</title>
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

Instruções

Esta aplicação tem como finalidade demonstrar os valores  que serão gastos com combustível durante uma viagem, com base no consumo do veículo e com a distância determinada por você!

Os combustíveis para esse cálculo são:

Álcool

Gasolina

Diesel

Cálculo do valor do consumo (R$)

                <form method="POST">

                        Distância em quilômetro a ser percorrida
                        <input type="number" class="form-control" name="distancia" id="distancia" placeholder="Distância em Km" required>

                        Consumo de combustível do veículo (Km/l)
                        <input type="number" class="form-control" name="media-veiculo" id="media-veiculo" placeholder="Consumo médio (utilize vírgula como separador de casas decimais)" step="0.01" required>

                        <button class="btn btn-info" type="submit" data-toggle="modal" data-target="#modal-combustivel">Calcular</button>

Consumo do Combustível

                                    <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
                                        ×
                                    </button>

                                        <?php
                                        echo $mensagem;
                                        ?>

                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>

                </form>

Answer: O botão que abre a modal é o mesmo que faz o submit do formulário. Com isso quando você clicar em Calcular sem preencher nenhum dos campos irá abrir a modal vazia, pois os dois campos possuem o atributo required.
E mesmo quando você submeter o formulário retornando um HTML dentro da modal através da variável $mensagem, esse HTML dentro da modal ficará fixo mostrando o que já tem lá quando você clicar em Calcular novamente sem preencher os campos, e não a mensagem de que nada foi inserido.
O que você tem que fazer é retirar do botão Calcular a função de abrir a modal, pois ele é um botão de submit. Basta retirar dele os atributos data-toggle="modal" e data-target="#modal-combustivel" (pode retirar também o type="submit", pois um button dentro de um form sem o type, já é de submit por padrão), ficando apenas assim:
<button class="btn btn-info">Calcular</button>

Feito isso, é preciso alterar a estrutura dos if's no PHP. Precisa criar um outro if logo após o if ($_POST){ para verificar se os valores enviados não são vazios. E declarar uma variável $modal = false; antes do if ($_POST){. Essa variável vai servir para saber se a modal será aberta após refresh do submit. Se for false, a modal não irá abrir, se for true você irá abrir a modal via JavaScript.
Dentro do if ($_POST){ você irá alterar a variável $modal para true, indicando que o POST foi recebido. Vai ficar assim o código todo (com a nova estrutura de if):
<?php
$mensagem = "";
$modal = false;

if ($_POST){
   $modal = true;
    $distancia = $_POST['distancia'];
    $mediaVeiculo = $_POST['media-veiculo'];

    if (isset($distancia) && isset($mediaVeiculo)){

       if (is_numeric($distancia) && is_numeric($mediaVeiculo)){

           if ($distancia > 0 && $mediaVeiculo > 0){

               $valorGasolina = 4.80;
               $valorAlcool = 3.80;
               $valorDiesel = 3.90;

               $calculoGasolina = ($distancia / $mediaVeiculo) * $valorGasolina;
               $calculoGasolina = number_format($calculoGasolina, 2, ',','.');

               $calculoAlcool = ($distancia / $mediaVeiculo) * $valorAlcool;
               $calculoAlcool = number_format($calculoAlcool, 2, ',','.');

               $calculoDiesel = ($distancia / $mediaVeiculo) * $valorDiesel;
               $calculoDiesel = number_format($calculoDiesel, 2, ',','.');

               $mensagem.= "
";
               $mensagem.= "O valor total do gasto será de:";
               $mensagem.= "
";
               $mensagem.= "
Gasolina: R$ ".$calculoGasolina."
";
               $mensagem.= "
Álcool: R$ ".$calculoAlcool."
";
               $mensagem.= "
Diesel: R$ ".$calculoDiesel."
";
               $mensagem.= "
";
               $mensagem.= "
";

           } else {
               $mensagem.= "
";
               $mensagem.= "
Nenhum dos valores informados pode ser igual ou menor que 0
";
               $mensagem.= "
";
           }

       } else {
           $mensagem.= "
";
           $mensagem.= "
O valor recebido não é numérico
";
           $mensagem.= "
";
       }
   }else {
       $mensagem.= "
";
       $mensagem.= "
Os valores de entrada não foram informados.
";
       $mensagem.= "
";
   }
}
?>

Agora a parte do JavaScript para abrir a modal:
<?php if($modal){ ?>

<?php } ?>

Se a variável $modal for true irá escrever o script acima e abrir a modal após o carregamento da página.


what is the best way to generate a reset token in python?

Question: I'm trying to make a validation process for a password reset, what i've used are two values: the epoch time, and i want to use the users's old password (pbkdf2) as a key,
Since i dont want to get non ASCII characters, i've used SimpleEncode library because it's fast since it's only a BASE64 with a key used, but the problem is that the password is too long (196 chars) so i get a long key!
What i've done is split the result code = simpleencode.encode(key,asci)[::30], but this will not be unique!
To get an idea how it works, i've tried Facebook reset process, but what is given is a number! so how this process works, don't they use a key to make it hard for someone to forge a link to reset someone's password?
Update: how the algorithme will work:
1- get the time using epoche time.time()
2- generate the Base64 of the epoche time (to use for the URL) and the epoch time value + a key, this key is PBKDF2(password).
3- generate the url www.example.com/reset/user/Base64(time.time()) and send this URL + the simpleencode.encode(key,asci)[::30]
4- when the user clicks on the URL, he put the generated code, this generated code, if it matches with the URL, then let him modifiy the password, else, it is a forget URL!
Comment: It doesn't need to be unique if you confirm it's not already in the database and it's long enough that no one will guess it.
Comment: You should *not* expose (a hash of) the user's password in any form as part of the reset token.  That would allow an attacker to learn the hash, which they could then attempt to brute-force.
Comment: You are exposing PBKDF2(password).  That is reversible by brute force if "password" is weak, which it is likely to be if it is, in fact, the user's password.
Comment: @AbdelouahabPp It's still a security hole, and a security hole is only unlikely to be exploited until it has been. Just don't introduce them when they're avoidable. You can always securely hash the password with a salt that's different than the one you use to store the passwords.
Comment: @AbdelouahabPp Also, 30 characters of base64-ed random-ish data (i.e. a hash) are definitely "unique enough".
Comment: @AbdelouahabPp In that case you can include the user ID in the hashed key. Since the internal user ID is always unique, the user ID+password will be unique, and it's therefore very likely a 320-bit hash thereof will be unique. (That said the random GUID method where you explicitly store reset requests in the database seems simpler.)
Comment: @AbdelouahabPp Also, if you can produce PBKDF2(password), that means you're storing passwords in cleartext in your database, which is a **bad idea**. Don't do this. Your database should contain hashes of passwords, preferrably salted with a user-specific random value. Bonus points for also salting them with a random number from a small range that you don't even store anywhere. (Although this complicates the login algorithm a bit.)
Comment: what i did is to push this value to the user's document (table), so it's in the database.
Comment: @Zack a hacker cant get access to the password, it'sonly it's hash that is used to calculate something in the server side, i've updated the algorithme
Comment: how this is exposed? the user will never know that it is his password that has been used with epoche time value to generate the token.
Comment: yes but the user will not enter all those 30 characters, users are always lazy :p
Comment: no, the password are stored using pbkdf2, and this algorithme uses salt, so i just tried to use the generated text and not the password itself, but now i think i'll stick with the randomly generated digits ''.join(choice(digits) for i in xrange(4)) , and about the user ID, i'm using their emails as primary key, so it is a little bit complicated :p
Answer: Not sure it's the best way, but I'd probably just generate a UUID4, which can be used in a URL to reset the password and expire it after 'n' amount of time.
>>> import uuid
>>> uuid.uuid4().hex
'8c05904f0051419283d1024fc5ce1a59'

You could use something like http://redis.io to hold that key, with a value of the appropriate user ID and set its time to live. So, when something comes in from http://example.com/password-reset/8c05904f0051419283d1024fc5ce1a59 it looks to see if it's valid and if so then allows changes to set a new password.
If you did want a "validation pin", then store along with the token, a small random key, eg:
>>> from string import digits
>>> from random import choice
>>> ''.join(choice(digits) for i in xrange(4))
'2545'

And request that be entered on the reset link.
Comment: @AbdelouahabPp I personally think your method is over-complicating the problem - and there's no reason to use any information about the user to generate the token...
Comment: @AbdelouahabPp If you mean "forged" - it's remarkably unlikely (1 in 340,282,366,920,938,463,463,374,607,431,768,211,456 chance) of generating an issued token (let alone while it's still valid). Have added a simple PIN example if that makes you feel more comfortable though...
Comment: thank you for this tip, and it is url safe, but, what about the method i've used, updated the question
Comment: so if i use the UUID method, i must generate the code that the user must input to enter?
Comment: to verify the the URL has not been forget
Comment: hihihihihhi i take your solution :p
Comment: I don't recommend this. The output of the `random` library is not secure and very easy to predict. Use secure random numbers, for example the `secrets` module (3.6) or `os.urandom` (<3.6)
Comment: @Atsch what's that got to do with UUID's which is the key point in this answer?
Comment: `uuid` is fine, since that uses `os.urandom`, but the pin solution is potentially dangerous
Comment: @Atsch okay - I do say that the PIN should be stored along with the token - so they're used in combination. It's fairly pointless but entering something sometimes makes people feel like there's some security going on kind of thing.
Comment: my apologies, I had skimmed it before and was alarmed to see somebody using `random` for tokens, but it turns out that's not very accurate. It might be handy to disclaimer it though, before somebody decides to use it without the token.
Comment: @Atsch it's late here and I'm on mobile so I'll add a little bit when I can to clarify. Thanks for pointing it out.
Answer: Easiest way by far is to use the ItsDangerous library:

You can serialize and sign a user ID for unsubscribing of newsletters into URLs. This way you don't need to generate one-time tokens and store them in the database. Same thing with any kind of activation link for accounts and similar things.

You can also embed a timestamp, so very easily to set time periods without having to involve databases or queues. It's all cryptographically signed, so you can easily see if it's been tampered with.
>>> from itsdangerous import TimestampSigner
>>> s = TimestampSigner('secret-key')
>>> string = s.sign('foo')
>>> s.unsign(string, max_age=5)
Traceback (most recent call last):
  ...
itsdangerous.SignatureExpired: Signature age 15 > 5 seconds

Comment: can anyone tell me how timestampsigner works, I am developing a web application, for that i need to generate time based forget password links and after sometimes the links should be expired
Comment: If you need to see how it actually works-- you can look at the source code for the library https://github.com/pallets/itsdangerous/blob/master/src/itsdangerous/timed.py
Answer: Why not just use a jwt as token for this purpose, its also possible to set an expiration time to the link, as its possible to put an expiration date to the token.

Generate token(JWT) encrypted with a secret key
Send mail containg a link with the token as a query paramater(When the user opens the link the page can read the token)
Verify the token before saving the new password

For generating jwt tokens I use pyjwt. The below code snippet shows how it can be done with an expiry time of 24 hours(1 day) and signed with a secret key:
import jwt
from datetime import datetime, timedelta, timezone

secret = "jwt_secret"
payload = {"exp": datetime.now(timezone.utc) + timedelta(days=1), "id": user_id}
token = jwt.encode(payload, secret, algorithm="HS256")
reset_token = token.decode("utf-8")

Below snippet shows how the verification of token and the new password can be set in django. If the token has expired or has been tampered with, it will raise an exception.
secret = "jwt_secret"
claims = jwt.decode(token, secret, options={"require_exp": True})
# Check if the user exists
user = User.objects.get(id=claims.get("id"))
user.set_password(password)
user.save()

Comment: Thank you for this additional answer. Are there any disadvantages if the user id (inside jwt) is provided by the client (step 2, verification and store new password)?
Comment: that should be fine, since ur backend code will send the email with the reset link, even if a malicious user enters someone else's id, only the correct user will have access to his email inbox and only he will be able to click on the reset link