How to get user id from firebase in react native?

Question: I need your help I'm using firebase for my app. I'm trying to get the users ID not the logged users no all users I have. I want to show their (uid) simply like in an alert for each user. Also, I'm showing them in a flatlist and when I set item.uid in an alert it shows (undefined). But, all the other data of the user is shown correctly. This what I did until now:
**
users.js
**
export default class usersList extends React.Component{

  state = {
    loading: false,
    uid: '',
    users: [],
    items: []
   };

   componentDidMount() {
        let itemsRef = f.database().ref('users').once('value').then(snapshot => {
               var data = snapshot.val();
               var items = Object.values(data);
               this.setState({items});
               console.log(snapshot.val())

            });
       }

  renderItem({item}) {
    return (
       <View key={index} style={{width: '100%', overflow:'hidden', marginBottom: 5, justifyContent:'space-between', borderBottomWidth:1, borderColor: 'grey'}}>
          <View>
          <View style={{padding:5, width:'100%', flexDirection: 'row', justifyContent: 'space-between'}}>

              <Text>{item.email}</Text>

          </View>
          </View>
        </View>
      )
  }

  render() {
   return (

     <View style={styles.container}>
     <ScrollView>
                {
                    this.state.items.length > 0
                    ? <ItemComponent items={this.state.items} navigation={this.props.navigation} />
                    : <Text>No stores</Text>
                }
                </ScrollView>
            </View>

  );
 }
 }

//ItemComponent.js
export default class ItemComponent extends Component {

  static propTypes = {
      items: PropTypes.array.isRequired
  };

  render() {
    return (
      <View style={styles.itemsList}>
        {this.props.items.map((item, index) => {
            return (
                <View key={index}>
                <TouchableOpacity
                onPress={ () => alert(item.uid)}>
                    <Text style={styles.itemtext}>{item.email}</Text>
                    </TouchableOpacity>
                </View>
            )
        })}
      </View>
    );
  }
}

Comment: No when i runed console.snapshot.val(). i got this output >> [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'console.snapshot.val')]
* app/Components/FamiliesList.js:31:32 in
- node_modules/promise/setimmediate/core.js:37:14 in tryCallOne
- node_modules/promise/setimmediate/core.js:123:25 in
- ... 8 more stack frames from framework internals
Comment: if you don't get the correct data logged it means you are doing a wrong database call ````console.log(snapshot.val())```` -- run this
Comment: when i run console.log(snapshot.val()) i get the data correctly but when i run console.snapshot.val( ) i don't ,thanks i did not know that cuz i'm biggener, So,what do you advice me to do?? becuase eveything is depingind on that & i'm stuck,please anything could help?
Comment: in the method ````componentWillMount()```` when you run the ````console.log(snapshot.val())```` do you get all the IDs?
Comment: yes and this is and example of the log output  "tnUdqJkl0pcCuQLWGHBANV4l8XV2": Object {
    "age": "33",
    "avatar": "http://www.gravatar.com/avatar",
    "email": "firstname.lastname@example.com",
    "fName": "Store",
    "lName": "Store",
    "phoneNum": "1564237948",
    "storeName": "Store4 ",
  },
  "utZ60dru99ObFqrD042ZMBIlGN93": Object {
    "avatar": "http://www.gravatar.com/avatar",
    "email": "firstname.lastname@example.com",
    "name": "Enter name",
    "username": "@name",
  },
Comment: try setting the state as the following ````this.setState({items: items});````
Comment: unfortunately, nothing changed
Answer: firebase.database().ref('user').on('value', (datasnapshot) => {
    this.setState({
       _id: datasnapshot.key
     });`

Answer: This solution worked for me
<Text style={styles.yourStyleHere}>UID: {auth.currentUser?.uid} </Text>


How can I import a selfmade class into my gwt project?

Question: I am working at a gwt tutorial and now i must Import a class/Project on my gwt Project. In my gwt Project i have the class "Sortierer-GWT" where is my entry Point. In this class i want to call a method, which is in another class. This class is written by my own and is called "UserInput". The class UserInput also Needs other classes. All this classes are in the Right Folder for Import. But if i Import the UserInput class like this:
<code>Import eingabe.UserInput;
……
UserInput read = new UserInput();
</code>
The Compiler says error (No source code is available for type eingabe.UserInput; did you forget to inherit a required module?). Should i Import the other classes which are used for UserInput or should i write something in my .XML data to fix this error?
Comment: Add your class in the same package your EntryPoint is located. This will remove the error message.
Comment: okay but then i must add 14 classes to this package, because UserInput Needs this classes
Comment: thx it works. But how can i print my Array on the screen with gwt. I tried:   TextBox.setText(list) , but it doesnt work
Comment: The transpiler needs all of the classes to transpile the source. So, yes, you have to add them all. In case you need them on the client and server side, you need to put the classes into the shared package.
Answer: There are many places with a lot of useful informations. Take a look here: https://github.com/TDesjardins/gwt-knowledge, https://docs.google.com/presentation/d/1cr-rYGz58ngC6sgOy8c7oErhaj0VdLUHk9Y5mcvgrP0/edit#slide=id.p, https://dev.to/ibaca/modern-gwt-first-steps-509k, https://gwt.zeef.com/awesomegwt and of course www.gwtproject.org.
And here you get in contact with other GWT developers: https://gitter.im/gwtproject/gwt
If you would like to create a 'larger' GWT-poject, you can try the Nalu Project Generator: http://www.mvp4g.org/boot-starter-nalu/BootStarterNalu.html
(Attention: there is a bug due to an update in Domino-UI 'setLeftAddon' -> 'addLeftAddOn')
The project generator will create a slightly larger project, that might help you to understand how to organize a GWT project.
Regarding your question on how to display an array:

GWT: http://samples.gwtproject.org/samples/Showcase/Showcase.html#!CwCheckBox
Domino-UI: https://demo.dominokit.org/home

And here, you will find an example project, using Domino-UI, Domino-rest and Nalu. It is a three pager, with server access and displaying a list of data: https://github.com/NaluKit/nalu-examples/tree/master/devkexample


Second Derivative Of A Parametric Function

Question: If $ y = 2t^3 + t^2 + 3$  $ x = t^2 + 2t + 1 $  then what is $d^2y \over dx^2$ for t = 1?
This is the question. What I tried is that, I first took the derivative using the rule $dy/dt \over dx/dt$, and then took the second derivative by taking the derivative of the result with respect to t. To formulate: $$\frac{d}{dt}(\frac{dy/dt}{dx/dt})$$
But in the end, the result didn't come out right. Is what I'm doing wrong? How to solve this question.
Thanks in advance.
The answer is $5\over8$ by the way, so you'll know when you get it right.
Comment: Is it 5/8 or 9/8?
Comment: 5/8 it says. But if you can prove 9/8, that can be the right answer. Because I've encountered some questions with wrong answers in this book.
Comment: The domain of $t$ is $t>0$?
Comment: Doesn't mention anything.
Answer: The formula for the second derivative of a parametric function is $$ \frac {\frac {d}{dt} (\frac {\frac {dy}{dt}}{\frac {dx}{dt}})} {\frac {dx}{dt}} $$.
Given this, we find that $\frac {dy}{dt} = 6t^2+2t$ and $\frac {dx}{dt}= 2t+2$. Thus, $\frac {dy}{dx} =\frac {3t^2+t}{t+1}$. Differentiating this with respect to $t$ yields $${\frac {d}{dt} (\frac {\frac {dy}{dt}}{\frac {dx}{dt}})}= \frac {3t^2+6t+1}{(t+1)^2}$$
Now, divide by $\frac {dx}{dt}$ to find that $\frac {d^2y}{dx^2}= \frac {3t^2+6t+1}{(t+1)^2(2t+2)}$.
Evaluating at $t=1$ yields $\frac {d^2y}{dx^2}|_{t=1} =\frac {3+6+1}{(4)(4)}= \frac{10}{16}= \frac {5}{8}$.
Answer: You can do this on your own and you have proceeded in the right direction too. Just a small mistake in the last step. That is,
$$\frac{dy}{dx}=\frac{\frac{dy}{dt}}{\frac{dx}{dt}}$$ since both $x$ and $y$ are functions of $t$ and then,
$$\frac{d^2y}{dx^2}=\frac{d}{dx}\left(\frac{dy}{dx}\right)=\frac{d}{dt}\left(\frac{dy}{dx}\right)\cdot \frac{dt}{dx}=\frac{d}{dt}\left(\frac{dy}{dx}\right)\cdot \frac{1}{\frac{dx}{dt}}$$
Make that small correction and then whatever be the answer, your procedure is mathematically correct now and logical too.


Delete Frame Borders in matplotlib bar graph

Question: I have the Following plot:
<code>```forest = sk.ensemble.RandomForestRegressor(n_estimators=250,random_state=0)
   forest.fit(X, y)
   importances = forest.feature_importances_
   std = np.std([tree.feature_importances_ for tree in forest.estimators_],axis=0)
   indices = np.argsort(importances)[::-1]
   refclasscol=list(df.columns.values)
   impor_bars = pd.DataFrame({'Features':refclasscol[0:20],'importance':importances[0:20]})
   impor_bars = impor_bars.sort_values('importance',ascending=False).set_index('Features')
   plt.rcParams['figure.figsize'] = (10, 5)
   impor_bars.plot.bar()```
</code>
Which displays this : here
I want to remove borders and make something like this:here
Is it possible?
Comment: Yes the borderless is done. If i want the values on y axis to appear in the bars itself, can it be done?
Answer: Typically, people use
plt.axis('off')
to get plots that look like this:

An alternative is adjusting alpha to have very faint borders

The rationale is that the left and bottom axis have the ticks and labels and thus carry information, while the top and right add no value.    This style is inspired by the work of Edward Tufte, who recommends that, rather than repeatedly adding emphasis to important elements in a graphic, you apply de-emphasis to unimportant elements.


naukri registration page using selenium web driver

Question: While I am automating Naukri registration, given an alert box, if that specific mail id is already registered.
Can anyone help me how to handle that?
driver.get("https://login.naukri.com/nLogin/Login.php");
driver.findElement(By.linkText("Become a member")).click();
driver.findElement(By.id("email")).sendKeys("firstname.lastname@example.com");
driver.findElement(By.id("password")).sendKeys("123456789");

Thread.sleep(50000);

Alert alert= driver.switchTo().alert();
driver.switchTo().alert().dismiss();

Answer: The dialog that appears on naukri site is not a real alert, so you cannot switch to it or dismiss it. It's rather a div which you can close by click on X button with xpath:
//div[@id="forGotPassword"]//a[@title="Close"]

So instead of last 2 lines, you should do something like
driver.findElement(By.xpath("//div[@id='forGotPassword']//a[@title='Close']")).click()

Comment: thank you ... :) so how we can identify a pop-up alert windos
Comment: a real popup alert window is something like javascript `alert` function: http://www.w3schools.com/js/tryit.asp?filename=tryjs_output_alert (i.e. it's a modal popup o top of the browser). Everything else is done on HTML level, and is technically just a dynamic part of the page (usually a `div` that is either created when it's needed, or always present on the page, but hidden, but shows up when needed). So you treat it just like any other element on the page.


transform: translate é compatível com quais navegadores?

Question: Exemplo de uso funciona bem em navegadores modernos a duvida é em quais versões não será compatível.

Centralizado

Answer: De acordo com o CanIUse.com, esta é a situação atual:

Note que este gráfico é dos 2D Transforms como um todo, o que inclui o translate.
Os ítens marcados com [1] não suportam CSS Transform em SVG
Ainda, há que se nota isto:

Abaixo do FF42, o transform-origin não funciona em SVG também.
In IE9 the caret of a textarea disappears when you use translate.
No Android 2.3 a escala não afeta o background.

Link:

https://caniuse.com/#search=translate
Answer: Segundo o MDN: https://developer.mozilla.org/pt-BR/docs/Web/CSS/transform#Browser_compatibility
+---------------+---------------+----------------------------+-------------------+---------------+------------+
| Feature       | Chrome        | Firefox (Gecko)            | Internet Explorer | Opera         | Safari     |
+---------------+---------------+----------------------------+-------------------+---------------+------------+
| Basic support | (Yes) -webkit | 3.5 (1.9.1)-moz16.0 (16.0) | 9.0 -ms10.0       | 10.5-o12.5    | 3.1-webkit |
+---------------+---------------+----------------------------+-------------------+---------------+------------+
| 3D Support    | 12.0-webkit   | 10.0-moz16.0 (16.0)        | 10.0              | Não suportado | 4.0-webkit |
+---------------+---------------+----------------------------+-------------------+---------------+------------+

Note que para algumas versões é preciso usar -XXXX (ex: -webkit)


How can I get the count of the current iteration with pandas iterrows()?

Question: I am trying to iterate over a DataFrame, but the index, I get when looking through iterrows() is the index in the DataFrame. Instead, I want it to be 0, 1, 2, 3...
        for ind, row in last_10.iterrows():
            print('ind', ind)

This returns:
2345
2346
2347
...

I want it to be:
0
1
2
...

Comment: `for ind, row in last_10.reset_index(drop=True).iterrows()` but please don't use `iterrows`.
Comment: It is a temporary modification.... It doesn't stick
Comment: I don't want to modify `last_10`
Comment: What would you suggest instead of `iterrows`?
Comment: Something that is vectorised. Not going to elaborate unless you explain what you are doing here
Comment: Or you can do something I wouldn't recommend such as using enumerate with iterrows.  `for ind, row in enumerate(df_last_5.iterrows()):` to access row data, you'd do `row[1]`
Comment: Okay - can you create an answer and I can accept
Comment: @coldspeed has the best answer.  I think.
Comment: Also you could look at `pd.factorize(last_10.index)[0]` may be
Answer: Using enumerate
for ind, row in enumerate(last_10.iterrows()):
    print('ind', ind)
    print('data',row[1]['columns'])

range create
for ind in range(len(last_10)):
    print('ind', ind)
    print('data',df.iloc[ind]['columns'])

Answer:     from itertools import tee

    import pandas as pd
    df = pd.DataFrame(['AA', 'BB', 'CC'], columns = ['value'])

    def pair_iterate(iter):
        first, second = tee(iter)
        next(second, None)
        return zip(first, second)

    for (i1, row1), (i2, row2) in pair_iterate(df.iterrows()):
        print(i1, i2, row1["value"], row2["value"])


Counterintuitive result when comparing two groups of time series

Question: I have two groups of time series and I am testing the hypothesis that the groups can be distinguished in some way. Each time series is measurements of an individual's pupil size as they listen to an emotive story. At the end they answer one question concerning the story which they can get right or wrong. So I just want to see if there is something about changes in pupil size over time which distinguishes those who answer correctly ($n=75$) from those who don't ($n=27$).
I thought that a straightforward, very general and sensitive way would be this: For each individual, work out the mean zero-lag cross-correlation with each individual in the correct group, and the mean zero-lag cross-correlation with each individual in the incorrect group. I think my hypothesis then predicts that individual's will have a higher mean correlation with members of their own group than with members of the other group. So I did that and this I what I got:
<code>                   Mean correlation         Mean correlation
                 with correct group     with incorrect group     p(comparison)
Correct group                  0.12                     0.18            <0.001
Incorrect group                0.18                     0.20             0.104
</code>
So I have the very strange result that individuals in the correct answering group are more similar to the other group that they are similar to themselves. At first I thought this impossible and I checked my scripts very carefully. But I now believe this really is the result I got. I think I also have a way to interpret it: What specifically distinguishes the correct group is that their time series change more often and more unpredictably than the incorrect group. My questions are:

Do you think my interpretation could be right?
Is there something flawed in my method so that this is simply an methodological artefact?
Is there a better way to do this?

EDIT. In response to the very useful answer and comment, I add a little more information. The stories are recorded and the data is collected with a 60Hz eye-tracker so there are no problems there. The data looks like this (a little challenging to explore visually because there is a lot):

I plan to use methods like the suggested surrogate time series, and further summary statistics, to better understand the nature of the difference in my data. However I would appreciate comment on one further issue. I understand that the difference between Correct and Incorrect that I think I have demonstrated may not be meaningful from the perspective of certain hypotheses. I wonder however if you would agree with the following statement? The significantly p-value from the test I performed indicates at least that we should reject the null-hypothesis that the two sets of time-series come from exactly the same population (i.e. some aspect of the pupil-data is related to the subsequent answer).
Comment: The information in the table points to substantial variability in the correlation coefficients. That suggests it may be misleading, or even meaningless, to compare averages.  But the problem is likely more fundamental than that: why should these cross-correlations tell you much about the subjects' responses? Slight differences in response times between two otherwise identically-responding subjects could create strong *negative* correlations among them. It appears you may need to explore your data (or at least a small subset) in order to develop a relevant and useful similarity metric.
Comment: At this point I see few differences, either qualitative or quantitative, between the two groups of data--but that isn't saying much because these plots reveal little. One way to start making sense of a mess like this is described at http://stats.stackexchange.com/a/46350/919. That kind of exploration can be followed up by robust regression to remove common trends and identify isolated outliers. Only at that point could one even hope that a more sensitive (but far less method) like cross-correlation would help reveal anything.
Comment: I am no expert on eye dilation, but are those strong jumps really to be expected? They look like measurement artefacts to me, whose effects may very well exceed the impact of anything you are interested in.
Comment: @whuber: comment appreciated, more information added in edit.
Comment: The strong jumps are probably blinks. Really such data should be preprocessed. As this is an initial analysis I have not done this yet because for now I just want to test the hypothesis that there is any difference at all between groups, and such artefacts should be equal between groups. I take all your points but I am still struggling to understand how my improvised test could produce a clear group differences if there is none - even if it is e.g. just a difference in blink rate...
Answer: Regarding 1. and 2.
It can very well be that other features of your time series (than you want to measure) lead to higher cross-correlations. To give an illustrative example (that does not exactly apply to your situation), the absolute values of the cross-correlation between time series with the same frequency content will on average be higher. In your exapmle, it can very well be that your cross-correlation is mainly determined by the strong outliers and thus a few outliers decide about the value and you thus overestimate the significance of your result.
A good way to find out (and eventually correct this) is using surrogate time series, i.e., artefical control time series, which keep some features of the original data but destroy others. For example, if you generate IAAFT surrogates of your individual time series, you would keep their amplitude distribution as well as their autocorrelation function (and thus their frequency content). In particular you would keep the strong fluctuations for your "correct group".
On the other hand, you would destroy any information tied to absolute time, in particular your story. Relatedly, you also destroy any actual temporal correlations to other time series. Therfore any cross-correlation you measure between those surrogate time series only has to come from the individual features of those time series (amplitude and frequency content) but not from an actual correlated temporal evolution (because you destroyed that information in the surrogates).
So, if you observe comparable results when analysing surrogate time series instead of your actual time series, your observations are due to a methodological artifact.
Regarding 2. and 3.
The cross-correlation effectively measures whether two time series change synchronously. Using it thus relies on the assumption that they were taken under synchronous conditions with respect to your sampling frequency and the variability of your time series. For example, if the lowest relevant frequency content in your time series corresponds to a period of 10 s, you cannot allow for a comparable inaccuracy of the delay between the beginning of the recording and your story.
Moreover, this assumes, that everybody reacts to the story in a very similar way. For example, you would assume that the pupils of all subjects (or at least all subject in one group) dilate during a specific part of the story. I do not think that humans behave that predictably. From another point of view: You probably know a lot of things unrelated to your experiment that affect the pupil size on the relevant time scales. All these effects are already meaking univariate time-series analysis very difficult. Bivariate measures (such as the cross-correlation) may be rendered fully meaningless. In general, I would be very careful with applying bivariate measures to time series that were not recorded simultaneously.
Looking at your time series confirms this: The time series are highly individual, in particular the outliers and jumps. The result of the cross-correlation is therefore very likely to be meaningless.
Instead I recommend take a look at simple univariate time-series measures, such as the mean, the standard deviation, the frequency content, etc. and statistically compare them (be aware of multiple testing and cherry-picking though). This could in particular capture the observation you already made that "time series change more often and more unpredictably" in one group.
Comment: @user25676: My assessment stays more or less the same: I strongly recommend to take a look at simple univariate measures first.
Comment: comment appreciated, more information added in edit, one more response would be appreciated.


Percentages and Proportions

Question: Given two integer variables $x$ and $y$. We are given that each integer variable $x$ and $y$ can't be greater than a given integer $z$.
The problem: We are given the proportions $a$ and $b$ such that $a + b = 1$, $a = \frac x z$, and $b = \frac y z$. Is it possible to reverse solve $x$ and $y$?
Comment: I don't think so but you can generally say $y=z-x$.
Comment: In the future, please try to use Mathjax to format your post, and avoid putting everything in backquotes. Please also let us know what you've tried so we can tailor our answers appropriately. Also, please try to find a more informative title—percentages aren't even involved.
Answer: Assuming you know $a$ and $z$:
From $a+b=1$, you can conclude ….
From $a=\frac x z$ you can conclude ….
From $b=\frac y z$ you can conclude ….
Comment: yes but $a$ and $b$ will not be integers...if one chose to round them, it wont be consistently hold for all numbers
Comment: @user1234440, there isn't always a solution in integers. Can you come up with a description of just when there *will* be one?
Answer: Note: This isn't a rigorous answer, and is only meant to get you on the right track. Work out the 'Why?' parts I've mentioned below.
From the information you have provided, since $a + b = 1$, $x+y=z$ for a given $z$, along with the constraints $x \leq z$ and $y \leq z$. Also, note that if $x=0, y=z$ and that $x=z$, when $y=0$. Therefore, $x,y \in [0,z]$ (Why?). It's easy to see that there would be $(z+1)$ solutions counting all such $x,y \in \mathbb Z.$(Why?)

Percentages and Proportions


Why am I unable to call a function in a C DLL using P/Invoke in C#?

Question: Getting an error:

C:
#include <stdio.h>

const char *print(const char *message)
{
    if (message != 0) {
        const char *message1 = "Connected";
        return message1;
        message = "";
    }
    return "Message empty";
}

C#:
     public partial class Form1 : Form
     {
         /*Declaration*/
         bool T;
         string a;

         [DllImport("DLLC.dll")]
         static extern string print(string message);

         public Form1()
         {
             InitializeComponent();
         }

         private void button1_Click(object sender, EventArgs e)
         {
             a = print("Send");
             if (T)
             {
                 label1.Text = a ;
                 T=false;
             }
             else{
                 label1.Text = "BAD";
                 T=true;
             }

         }
     }

The idea is learn how to use functions from c in c#.
Also I would like use open62541 library for OPC UA SERVER and make UI with windows forms.
Comment: The code will not work as is.  In c a string is byte array terminated with a '\0'. In c# a string is a two byte object.  So you have to convert like this : Encoding.UTF8.GetBytes("Send\0")  You also need to change definition : static extern byte[] print(byte[] message);
Comment: You need to export the `print` function (`__declspec(dllimport)`). Also be careful about your calling convention.
Comment: Also, since `message` can be changed, although you never actually do that, you'll need to pass it in as `ref` from the C# side.
Comment: Can you explain more about what are you mean? because I'm really new to that...
Comment: @jdweng marshaling will take care of this ...
Comment: This is bad, bad code ... you should not return pointer to local variable outside the function (ok now it's literal so there should be no problem, but with "real" variable.. ) ... [this question was similar - check my answer](https://stackoverflow.com/questions/53198213/calling-c-function-with-lpstr-return-value-from-c-sharp/53201035#53201035)
Comment: @jdweng the marshaller should do that
Comment: okay after exporting getting different error after a = print("Send"); Program just stops working with error message Form1 program has stopped working.
Comment: @jdweng maybe you can write how its should look like?
Comment: There is no marshaling so it is needed.
Comment: @jdweng of course there is - the runtime does the marshaling as part of the p/invoke
Comment: @canton7 so show send string to dll do something with it and get it back?
Comment: No it doesn't without some instructions.
Comment: @jdweng I can promise you that string marshaling to a `char*` works. See e.g. https://learn.microsoft.com/en-us/dotnet/framework/interop/default-marshaling-for-strings
Comment: @jdweng calm down, please. You said that you can't pass a `string` into a p/invoked method, and need to instead to it into a byte array. I'm telling you that that is incorrect, because it is. That is all.
Comment: @jdweng is indeed wrong here. Default marshaling is used. Note however that default marshaling for a string return type assumes that the null terminated buffer was allocated on the COM heap and the marshaler will destroy it by calling CoTaskMemFree.
Comment: Where does it say on following page that a string get cast to a byte[]?  https://learn.microsoft.com/en-us/dotnet/framework/interop/default-marshaling-behavior
Answer: You need to mark the function in the DLL as exported. There are two ways of doing this. You may either create a .def file and name the exported functions, or add the specifier __declspec(dllexport) to the function signature.
To create a .def file, in Visual Studio with the C DLL project open, right click on "Source Files" and then under "Visual C++" -> "Code", select "Module-Definition File (.def)".  In the newly created .def file, you can list the functions you wish to export, like so:
LIBRARY mydll
EXPORTS
    function1
    function2
    function3

Then, when you build the DLL, function1, function2, and function3 are available.
Also, keep in mind that unless you manually specify a calling convention (e.g. int __stdcall function1(int a, int b);), the calling convention is __cdecl by default, and so when you add the line to import the function through P/Invoke, you must also have the attribute CallingConvention = CallingConvention.CDecl. Failing to match calling conventions will cause stack corruption in the calling code.


Porque um if pode ser redundante?

Question: Utilizando um método que retorna um boolean o sistema determinaria se um numero é positivo ou negativo. Então fiz desta maneira:
    public boolean isPositive(float num) {
    boolean positive;
    if (num >= 0) {

    positive = true;
        } else {

    positive = false;

    }return positive;

Então o Netbeans declarou que "a instrução if é redundante" e corrigiu para:
    public boolean isPositive(float num) {
    boolean positive;
    positive = num >= 0;
    return positive;

e funcionou.
Como não fez uso de if fiquei perplexo em saber q funcionou, já que não fica evidente em que situação ele retornaria um FALSE. A minha dúvida é: como? Qual a lógica da segunda declaração do método?
OBS: As duas declarações funcionam.
Answer: O if solicita uma condição boolean certo?
então quando você passa uma expressão (num >= 0), esta irá retornar um boolean.
Esqueça o if, qual é a função do seu Método?
Retornar true se for maior que zero, certo?
Então você passa esta informação à variável e a retorna.
Outra forma de implementar este método seria:
public boolean isPositive(float num) {
    return num >= 0;
}

Answer: Na verdade o NetBeans foi tão esperto quanto ele poderia ser. Deveria ser assim:
return num >= 0;

Ora, qual é o resultado da expressão num >= 0? É um booleano, certo? Ou seja, o resultado dessa "conta" será ou false, ou será true. Ou é "maior ou igual a zero", ou não é, só isto pode ocorrer.
Então por que não usar este resultado direto? Se o que você precisa é definir um true quando esta expressão resultar em true e você precisa de false quando a expressão resultar em false, então use o resultado da expressão e simplifique a lógica.
Percebo que muito programador acha que o if é uma coisa mágica, e que você tem uma condição lá dentro. Na verdade o if espera uma expressão booleana, uma expressão condicional. Pode ser um literal, uma variável simples (com valor do tipo booleano), ou uma expressão complexa cujo resultado seja um booleano.
Tudo isto é válido (mesmo que o primeiro não faça sentido a não ser para teste):
//literal
if (true)

//variável
variavel = outraVariavel > 0 && maisUmaVariavel.equals("teste");
if (variavel)

//expressão
if (outraVariavel > 0 && maisUmaVariavel.equals("teste"))

O if serve pra controle de fluxo, ele decide apenas se ele deve executar um bloco ou outro, se disponível. A decisão se dá por um valor booleano qualquer, de onde ele vem, não importa. O código deste método não precisa de um controle de fluxo, ele só precisa de de um resultado booleano.
O mesmo vale para o while e for, são apenas controles de fluxo cuja decisão se vai desviar para outra linha ou continuar na seguinte é determinada por um valor booleano que pode ser obtido por uma das três formas citadas acima.
Faça assim que estará simplificando o código e obtendo o mesmo resultado:
public boolean isPositive(float num) {
    return num >= 0;
}

Coloquei no GitHub para referência futura.
Leitura complementar.
Answer: Pode-se dizer que o Netbeans é uma IDE bem chata, pois ela não deixa vc programar e faz tudo sozinha. kkkk
É o seguinte, algumas IDEs, são inteligentes e fazem o possível para reduzir o código ou consertar possíveis bugs. No seu caso a IDE quis eliminar a quantidade extra de código, no caso a condição, então ela salvou o resultado da condição em uma variável e retornou.
public boolean isPositive(float num) {
    boolean positive;
    positive = num >= 0; // (num >= 0) returna true ou false
    return positive;
}

como não vai ficar chamando comandos dentro das condições, não há necessidade em tê-las, e é possível reduzir mais ainda o código.
public boolean isPositive(float num) {
    return (num>=0); // retorna diretamente true ou false
}

Quanto menos código no seu sistema, mais rápido passará pelos métodos, quando o software faz muitas verificações, ele tem uma sequencia necessária para efetuar a condição, por isso é recomendado fazer de tudo para ter menos linhas de códigos escritos.
Answer: num >= 0; é uma expressão que pode ser avaliada com retorno de true ou false É possível simplificar mais um pouco jogando a expressão direto no return, return num >= 0;

Porque um if pode ser redundante?


Mismatch in OSversion and other properties for Aspnet image on AppVeyor

Question: I am facing a very peculiar issue. I am using AppVeyor for my CI/CD and apparently the OS version of  mcr.microsoft.com/dotnet/framework/aspnet:4.8 image is different on AppVeyor server and on my system.
This is what I get when I inspect the image on AppVeyor
"Architecture": "amd64",
        "Os": "windows",
        "OsVersion": "10.0.17763.1637",
        "Size": 8424507509,
        "VirtualSize": 8424507509,
        "GraphDriver": {
            "Data": {
                "dir": "C:\\ProgramData\\Docker\\windowsfilter\\299141c3a9068896ce9f5928d7b9174570a2ef0b5f59a9b45ca0d768c3a09206"
            },
            "Name": "windowsfilter"
        },

as compared to this, which is what I have on my system
"Architecture": "amd64",
        "Os": "windows",
        "OsVersion": "10.0.19041.685",
        "Size": 5541468778,
        "VirtualSize": 5541468778,
        "GraphDriver": {
            "Data": {
                "dir": "C:\\ProgramData\\Docker\\windowsfilter\\c9c52e1316159499618a9c15b326fdc7457db4f4de07af590039086de5bc82e0"
            },
            "Name": "windowsfilter"
        },

There is a difference between OS  version and other properties. So I tried pulling the images again on the basis of digest
docker pull mcr.microsoft.com/dotnet/framework/aspnet@sha256:d7adec80a9a43d801891458040018972ac006aeca2903cdc6369f69f54951b22

but nothing gets updated.
The problem this is causing is that I have custom image that gets build on my system that has an OSversion "OsVersion": "10.0.19041.685". This image is used by Appveyor to build my final image, and as expected this throws an error as explained in Microsoft Container Versions.
Can anyone help me understand why that's happening.Thanks!
Answer: AppVeyor image is based on Windows Server 2019, so 10.0.17763 is the latest version of base image that can be run on AppVeyor. You are on Windows 10 version 2004 which is build 10.0.19041.
Comment: Yes, I figured that after looking at the official documentation for both Appveyor and Aspnet images. So just for my info Appveyor only support Windows server images ?
Comment: We can provide "Windows Server 2004"-based image as a paid option.
Comment: Although I think we have paid membership, but we are good for now.Thanks !


What kind of modulation is this?

Question: everyone.
I'm new to this forum and learning music theory by self, so excuse/correct me if I'm using wrong terms or my question is unclear.
I have a piece E minor that I'm learning right now and I was trying to structure and analyze it. My question is about bar [2], but I posted everything else for the context.
In the bar [1] we have Cmaj7 followed by something that looks like D chord (both from E minor), but then in bar [2] there are chords Eb->Bb->F, which are not in E minor. There are no such chords in neighboring keys, in fact only G minor has all of them and G minor is kind of far from E minor. On the other hand it feels like after D in [1] we are just make a half-step up for just one bar.
Is this some kind of (extreme) modulation or some other technique that I'm not aware of? It sound good, but I can't understand why something like this even works. I haven't seen such tricks before (I'm leaning piano for 1.5 years).
Comment: What happens in the measure(s) following the cadence on G major? The presence of F naturals in [2] are suggestive of setting up a transition to C minor or major, for which G would be the dominant.
Comment: After this, melody resets itself to E minor. Following part is pretty detached from this part
Answer: The bass notes are important.  The chord you have labelled E♭ has a C root.   Think of it as some sort of C chord. (It persists for the whole first half of that bar.  Where you've marked B♭ there's no D or F notes.) The second half of the bar has a D root.
Now we've got a basic root  harmony of C, D, G.  That makes a lot more sense in an E minor context, doesn't it!  A simple move to the relative major, G.
But an Eb upper structure over C?  An F upper structure over D?  It's a II, V, I bass line in G major.   With ♭VI, ♭VII, I superimposed.
We know II, V, I is a thing.  Cycle of 5ths and all that.     Well, ♭VI, ♭VII, I is a thing as well, though a rather jazzier one that maybe isn't taught in Harmony 101.  And they do the SAME thing - lead up to a tonic!   And, hey, you can combine them!
I don't think there's much point in analysing deeper than that. ♭VII can be used as a dominant, though it's rather hard to analyse WHY in 'theory' terms. (A lot of 'Why' in music theory can be difficult to pin down in functional 'cycle of 5ths' terms.  Just believe!  Or cop out by describing it as 'planing'.  It works because it's the same shape and adjacent.  Well OK, but that doesn't actually EXPLAIN anything.) And ♭VI can act like a dominant-of-the-dominant.  And when a chord is acting like a dominant, you can generally root it with the dominant note.   Likewise a dominant-of-the-dominant.
(Oh yes - and you don't HAVE to be diatonic.  That passage doesn't shift away from E minor/G major for a moment.  Chromatic chords exist, and don't have to change the tonality.)
Comment: Yes, I obviously made a mistake in marking those chords as Eb->Bb->F. G major makes sense and your reference to jazz is correct as well (it is present in this and other pieces of this composer). It is starting make more sense now. Last question: you said it is II-V-I in G major; isn't it  **iv-v-I** in G major? As you said it is C(m7), D, G not Bb, D, G
Comment: "♭VII can be used as a dominant" Isn't that because in the diatonic minor, bVII occurs naturally as the chord built on the seventh degree, which indeed functions as a dominant, and for the major keys we only need to apply modal interchange? And the bVI is the bVII if we temporarily pretend that we're in the key of bVII (in the key of C, pretend Bb is the tonic), so that would explain the bVI one
Comment: @Artem Mezhenin Yes, I was sloppy.  II,V, I is the underlying functionality, dominant-of-dominant-of-tonic.   IV, V, I is a variant.  But you were sloppy too!  II would be an A chord, not a Bb.
Comment: @Divide1918  But WHY does bVII act as a dominant in a minor key?  The question remains unanswered.  Can't it just do so in a major key as well without the complication of 'borrowing'?  Just let it be a chromatic chord in the major key.  No-one's trying to work out what scale would fit it.  The piece has been composed, improvisation isn't required!
Comment: @LaurencePayne ok, I think I have much better understanding of what is happening. Still need to level up my theory though :) Thank you very much for you help!
Comment: Try to see the wood within the trees!   What MATTERS in that progression is in the bass line.  The rest is decoration.  You'd be surprised what can be put over a dominant bass note and still act like a dominant!
Answer: Not really much of a modulation, except getting to the relative major of G.
It happens in many pieces. Your E♭ harmony I'd put at Cm7, the 'B♭' is included, then there's Dm7, followed by an F chord. That's the 'odd' one. It can be said to be borrowed from the parallel key (Gm), and is often used instead of V - it's called ♭VII.
Answer: Taking the left hand part into account, I would analyze [1] as Am9-Bm7, and [2] as Cm7-Dm7. So [2] functions in the same way as Cmaj7-Ddim7. In the key of G major, [2] is a iv-v-I, which may be analyzed as modal interchange by the parallel minor.
Comment: Indeed, in the original score there are Am7  and Bm7 in `[1]` and Cm7 and Dm7 in `[2]`, but it didn't understand that at all. So I replaced those notes with my own understanding (which was probably wrong decision). It is still confusing, because for Dm7 we just flattened F#, but for Cm7 we need to flatten two more keys. I feels like a lot
Answer: You are over complicating this greatly. It is a Cm7 = C + Ebmaj to a Dm7 to Gsus4.
This is just a iv v I in G, if you want. It is much like a iv v i in G minor, so we say it is borrowed from the parallel minor. This works well(assuming other conditions are met) because it is just a change of quality. The modulation that takes place is sort of sub-conscious or so transient as not to be too unsettling. Of course it may or may not work depending on the context but if other parts of the music support it then it adds new "spices" to the generally bland diatonic progressions without too much harmonic trouble. [The ascending diatonic bass line is very strong in making sure to tell our brains that it is just a diatonic progression in G with alterations in color.]
So it seems like you have four problems: 1. Not recognizing 7th chords in the complete context, 2. Not understanding modal interchange, 3. Not realizing that one can pretty much use any combination of chords one wants, it depends more about "feel"/"mood", voice leading, motivic connection, etc. 4. Not taking in to account the importance of the bass.
Theory is not about explaining the WHY, it is about explaining the WHAT. Why a composer does what he does is impossible to know(there, of course, are trends, fads, and logic, but...).
You also analyzed [1] incorrectly.
Yes, the RH chord is Cmaj7, but the bass is clearly Am, this is then really an Am9. Am9 = A + Cmaj7 = A + [C + E + G + B].
Next is a Bm7 chord, same idea: Bm7 = B + [D + F# + A] = B + [Dmaj]
So the progression is better thought of as Am9 Bm7 Cm7 Dm7 Gsus4. What is this? Just a ii iii iv v!! They are just walking but the scale playing the minor 7ths/9ths on each one! Notice the bass line on beats 1 and 3, ignore everything else, it's just A B C D. See how simple it is? All that other stuff is filler! He's just playing a scale in the bass, filling out some notes in the chord on the LH using arpeggios and playing some chordal stuff in the RH using "extensions" or "biharmony" if you will. That is, he's thinking of Am minor in the LH but playing Cmaj7 in the RH because he knows that this just gives an Am9 chord. It's sort of "bi" in the since of thinking of Am + Cmaj(7) but this is really just Am7(9). it's a way to get extended colors without thinking too complex.
You could change this, for example, to using a Cmaj in the bass, in fact, you could change the entire progression to what you think by shifting everything up a 3rd to get the bass to go C D E F# and then the progression would be Cmaj7, D7, E(b9), F#maj7. But in this case the two modal chords at the end would sound funky because they are not related to G(sorta like what you said). It wouldn't work out too well, but when you consider the base line properly things make sense(sorta how with a pedal tone we can do all kinds of weird stuff).
E.g., With Eb in the bass the Eb chord is out of context(it would be expected to be some sort of Ger chord or a V/V or whatever, but all sort of out of place). But with a C in the base it's just a iv chord, which is just a IV chord that has been modaly changed... not big deal.
The difference is that the bass provides context. A C in the bass clearly says "I'm in the key of G!"(in this example), but an "Eb" in the bass(which we don't have) would say "I'm doing something weird!". Nothing weird here.
Don't let yourself get confused. Most music "follows the rules" pretty closely and it won't do very strange stuff(because if it's following the rules then takes a complete 13th dimensional turn it will sound very bad). If you can hear it and it sounds normal and good then chances are it's not really "breaking any rules". Remember, a key is all 12 notes, not just 7! It's about how they are used. C in the bass here is pretty basic, Eb in the bass is not! And if it were then changes are it would resolve to D so it would be more of a chromatic move. If it when to Bb it would be odd because Bb changes the mode to G minor... it can be done, but it would be an "advanced" thing that simple music would generally  not due, it would require a more "experienced" composer to make it work(it's not hard, but there are just more traps).
Always try to reason from the bass up. The bass is what makes the harmony. The bass notes are usually the roots or at least a basic chord tone. Start reading some music and just read the bass lines, try to get a feel for it and then things will make a bit more sense. The bass tones are the more important in establishing harmony because everything will be heard relative to them. You can't have a Cmaj7 chord in the upper structure and a Db bass tone(unless it is passing/nonharmonic/irrelevant). Db + Cmaj7 will be heard as Db + C + E + G + B = Dbminmaj7#11#13 a very twisted sound(which is better just called a Cmaj7/Db, but this does suggest the Db is passing rather than the Cmaj7, but names do not always tell us exactly what is going on and sometimes can be counter to what we hear). Of course you probably could perceive the Cmaj7 on top because it doesn't mesh with a single bass note of Db but it just generally would not work UNLESS it was made to work by making one or the other or some sub-structure be "passing". E.g., If the Cmaj7 came from Dbmaj7 and went to Dbmaj7 then clearly the Cmaj7 is a lower chromatic motion which jazz does a lot, no big deal because it's so temporary and clearly the tonality is Dbmaj7. Essentially though you can't fit a large square peg in a small round hole or vice versa. Again, alternatively the Db could be passing/moving so that the Cmaj7 structure is clearly the main thing. Whatever is going on, we can't have tension without release or it will sound terrible/odd. Once you understand tension and release then everything makes sense and the better you understand it the better of a musician you become.

What kind of modulation is this?


Iterating Objects in Node.JS for HTTP response

Question: I have an object array in Node.JS and I wish to response.write(); it to the screen, I can manage to get the first object out but the loop stops and only the first is outputted, can anybody point me in the right direction...
db.collection('todo', function(err, collection){
                    collection.find(function(err, cursor) {
                        cursor.each(function(err, doc) {
                            for(docs in doc){
                                if(docs == "_id"){

                                }else{
                                    var test = docs + " : " + doc[docs];

                                }

                            }
                            data = data.toString("utf8").replace("{{TEST}}", test);
                            response.write(data);
                            response.end();
                        })
                    });
                });

Comment: Soon as you call response.end, that finishes the response.  Do you mean to call response.write instead?
Answer: Move response.end() out of the loop. That should do it.
Answer: You need to change response.end(data); to response.write(data);
Note that this will asynchronously append to the stream going to the user.
If you plan to dump them all at once you can also try:
cursor.toArray(function (err, docs) {
  /** run a for loop over each doc in docs **/
})

Comment: Your `response.end()` cannot be there. Read the code, it says: *"foreach document on this cursor, write the document to the response stream and then end the response stream"*... you are effectively ending the response for each object coming back from MongoDB. Clearly this is not what you want to do.
Comment: I've changed response.end(data); to response.write(data); response.end(); although the same issue still persists, I tried moving response.end() to the end of the db.open also but this resulted in nothing being responded at all
Comment: you need the response.end after the last adync callback; at the end of db.open likely fires before any writes.
Comment: Placed it there but still no iteration, see code in original post (updated it)


Applying if statements to for loops

Question: I am trying to use an if statement within a for loop to compare the values of two columns of a dataframe against each other.  A sample of three relevant vectors in my data frame is shown here.
HA
1.2
1.2
1.5
0.547502826
1.7
2.4
1.2846
0.5
0.466394974

HB
11.5,
10.5,
10.95,
0.547502826,
7.6,
6.7,
3.547,
3.85,
3.346972572,

HC
13.75,
11.55,
14.95,
0.547502826,
15.9,
11.05,
5.341,
3.75,
4.545342419,

What I want the loop to do is remove rows where HA and HB have identical numbers.  I have tried to do this using this section of code to remove these lines from the dataframe shown here as the object "data".
for(i in rmvalue){
if(as.numeric(rmvalue[i,"HA"])==as.numeric(rmvalue[i,"HB"])){
  data<<-rmvalue[-i,]
}

R however only appears to evaluate the first expression in the data frame.  Not sure how to proceed or if this process can be performed without an if statement  as a workaround.
Comment: Just try `data<-rmvalue[rmvalue[,"HA"]!=rmvalue[,"HB"],]`.
Comment: R isn't Java. It is a vectorized language. I would suggest reading some R tutorials. This will consume some time at first, but will save you lots of unnecessary headache as above in the future.
Comment: Thanks for all your feedback everyone!  I am actually new to programming in general only having just finished "Introductory R" by Robert Knell and Coursera's "Data Scientist's Toolbox" and "R-Programming."  I must admit that I have also dabbled in Java with Codecademy for fun which was likely confusing me.  Finding the work fun but also at times intensely difficult as no one else in the office uses R.  Cheers to an open source community!
Answer: As David mentioned, you are not using R properly which would make more challenging problems appear impossible.
To attempt to answer your question, I would quickly write the following, assming you have your data.frame as specified below.
D=data.frame(
 HA=c(1.2,1.2,1.5,0.547502826,1.7,2.4,1.2846,0.5,0.466394974),
 HB=c(11.5,10.5,10.95,0.547502826,7.6,6.7,3.547,3.85,3.346972572),
 HC=c(13.75,11.55,14.95,0.547502826,15.9,11.05,5.341,3.75,4.545342419)
)

result <- D[D$HA!=D$HB,]

Hope this points to a better direction.
Answer: As the other commentators have noted you're speaking R with a Java accent, but that's not bad! Everyone who learns R form another language goes through something like this because you apply your old mental model to the new language. Generally R doesn't like loops and rewards vectorized solutions. In this case subseting is a quick and succinct way of doing the job:
D=data.frame(
      HA=c(1,1.2,1.5,0.547502826,1.7,2.4,1.2846,0.5,0.466394974),
      HB=c(1.5,10.5,10.95,0.547502826,7.6,6.7,3.547,3.85,3.346972572),
      HC=c(13.75,11.55,14.95,0.547502826,15.9,11.05,5.341,3.75,4.545342419)
    )

D2 <- D[ D$HA != D$HB, ]

That said, under the hood R is running a loop, it's just that that loop is written in a more performant language. So it's actually a good aid to understanding to write loops sometimes, although you should try to not use them in real code. For your example I think I would write it as a loop like this:
library(dplyr) # for bind_rows
list <- vector(mode = "list", length = length(D$HA))

for(i in seq_along(D$HA)){
  if(as.numeric(D[i,"HA"])!= as.numeric(D[i,"HB"])){
    list[[i]] <- D[i, ]
  }
}

D2 <- bind_rows(list)

Basically this i just a longer, slower, way of subsetting, but it's a form that underlies a lot of R code. This is basically what lapply() is doing.
Answer: An alternative to @Nikos 's answer is to use dplyr. I suggest this package because it makes the syntax easier to follow for those new to R and vectorisation (IMO)
install.packages("dplyr")
require("dplyr")
HA <- c(1.2, 1.2, 1.5, 0.547502826, 1.7, 2.4, 1.2846, 0.5, 0.466394974)
HB <- c(11.5, 10.5, 10.95, 0.547502826, 7.6, 6.7, 3.547, 3.85, 3.346972572)
HC <- c(13.75, 11.55, 14.95, 0.547502826, 15.9, 11.05, 5.341, 3.75, 4.545342419)
d <- data.frame(HA, HB, HC)
d1 <- filter(d, HA != HB)

Applying if statements to for loops


Multicollinearity: Why does highly correlated columns in the design matrix lead to high variance of the regression coefficient?

Question: I came across the term "Multicollinearity" in statistics, particularly statistics. However, I never really understand mathematically why highly correlated (almost linearly dependent) columns in the design matrix $X$ lead to higher variance of the regression coefficient, given that the $$Var(\hat{\beta}) = \sigma^2 (X^TX)^{-1}$$ ?
Can someone explain to me mathematically the idea behind please ?
Comment: That formula doesn't even make sense if the columns of $X$ are linearly dependent, because then $X^TX$ is not invertible. Multicollinearity in statistics usually refers not to the case where the columns of $X$ are exactly dependent but to the case where the explanatory variables are highly correlated.
Comment: I meant to say high correlated columns, not perfectly correlated columns.
Answer: Lets start with your formula:
$var(\beta)=\sigma^2(X^TX)^{-1}$
First, note that is the columns of X are linearly dependent, then $X'X$ will not be invertible and have a determinant of 0. And, I won't prove this next part, but if $X'X$ is "close" to being linearly dependent, then the determinant will be "close" to 0.
Now, the inverse of a matrix A is related to the inverse of its determinant:
$A^{-1} = \frac{1}{det(A)}adj(A)$
where $adj(A)$ is the adjugte of the matrix A.
Therefore, as $X'X$ becomes more linearly dependent, its determinant becomes closer to 0, which means the elements of the inverse get larger.


Eb harmonic minor scale fingers?

Question: Can anyone help me with Eb harmonic minor piano scale fingering, please?
For both hands if possible.
Answer: The trick is to use 2, 3, and 4 on the G♭–A♭–B♭ stretch in the first half of the scale; otherwise you'll use a thumb, pinky, or have to oddly cross over during that stretch of black keys, none of which are ideal.
<code>R: 3 1 2 3 4 1 2 3
L: 2 1 4 3 2 1 3 2
</code>
Comment: Sure! The 3 is just to be internally consistent, since you'll use it at the top of the scale. But feel free to start with 2 if that's easier.
Comment: Thanks a lot! Is it fine to start with the second finger for R.H.? Seems o be more comfortable.
Comment: As Richard says. But in the near future you may want to carry on up for another octave. The second octave will start with 3 on Eb. So it **may** be better to learn it now.  Not **2** 1 2 3 4 1 2 **3** 1 2 3 4 1 2 3  but  **3** 1 2 3 4 1 2 **3** 1 2 3 4 1 2 3  But it's up to you.
Comment: Thanks, Richard. I've just found a useful site with the fingering for all major and minor scales, including all the 3 patterns natural, harmonic and melodic.
Comment: https://www.musicteacherguide.com/piano-harmonic-minor-scales/#harmonic
Comment: @ReadyToLearn Wow, that site has the left hand thumb on G♭? That is...unexpected.

Eb harmonic minor scale fingers?


Using IIS 7.5 with Tomcat and ARR to re-write URL

Question: I've tried searching but can't seem to get the answer I need.
I have some webservices managed by IIS. However, we need to host a web application run on Tomcat on the same server. The solution it seems, is to create a Server Farm in IIS, and this works for the most part.
The url we normally use to access the app run on Tomcat is localhost:8080.
By setting up the server farm in IIS, I've managed to get http://localhost to redirect to the same app. However, this creates a conflict with another app, so I want a custom path like http://localhost/Webapp/ to redirect there. How do I accomplish this?
Answer: Looks like your app on tomcat is the so called root application. This is the application that resides under the server root url.
You need to get the application run on tomcat under the url http://localhost:8080/Webapp. To achieve this, simply install the application under the context Webapp. How to do this depends on how your application is installed. If it is a folder inside tomcats webapp folder, simply rename this folder. If it is defined by an context definition (ROOT.xml) inside the tomcats conf folder, rename this context definition to Webapp.xml.


Why I am getting view mail not found?

Question: I am trying to send email in Laravel via my post route which has the following code:
 Mail::send('mail',['name','Ripon Uddin Arman'],function($message){
        $message->to('email@example.com')->subject("Email Testing with Laravel");
        $message->from('email@example.com','Creative Losser Hopeless Genius');
    });

When I run the code I get the error:

View [mail] not found.

I am using use Illuminate\Support\Facades\Mail;
Comment: you don't have a view named 'mail' ... why would there be one?
Comment: The view creates the body of the message
Comment: Can I do it wihtout view
Comment: @user123456 You can, but it's honestly harder and a bit silly. Laravel has a built-in mail templating system to quickly build basic HTML emails. https://laravel.com/docs/8.x/mail#markdown-mailables
Comment: You can use your existing code with a blank `mail.blade.php` file in your `views` directory, and it'll work as written. If you don't need a body, then `Mail::raw('', ...)` as written below is also fine. Lots of options when using Laravel
Comment: Did you ran php artisan view:clear ?
Comment: Why do I need a view I just want to send email
Answer: If you are looking to send a raw email you can use the raw method:
Mail::raw($content, function ($message) { ... });

If you want to send an HTML email you can use the html method:
Mail::html($htmlContent, function ($message) { ... });

These do not require any view as you are passing the entire message body/part to the method.

Why I am getting view mail not found?


Feature subset selection by stepwise regression for a random forest model?

Question: I would like to build a random forest model for regression. I have an abundance of potential features, and I expect only some of them to have a significant impact on the target variable. In addition, some of the features may be strongly correlated.
By trial and error, I have found a subset of features which seem to work well (in terms of mean squared error on the test set), but I would like to see if I can do better. I was thinking of some sort of autmatic feature subset selection, such as stepwise regression. However, I have two questions

Since random forest models already select features, is it possible to gain much by such a method?
What would be a suitable information criterion for a random forest model? Since it's nonparametric, standard information criteria (such as AIC or BIC) one would normally use for stepwise regression, don't work.
Answer:
Since random forest models already select features, is it possible to
gain much by such a method?

Yes and no. By selecting only a subset of features, and creating synthetic variables, you can help/accelerate the convergence of trees. But not necessarily improve it, because synthetic variables, which are a combination of one or more variables and one or more division rules, are nothing more than nodes in a decision tree, and so the tree will find them on its own if it finds them relevant.
But the idea is good, a widely used technique is to look at the results of a simple decision tree, and use the conditions of these first nodes to create synthetic features that we reinject in a logistic regression or stepwise regression.

(source: scikit-learn.org)

What would be a suitable information criterion for a random forest model? Since it's ninparametric, standard information criteria (such as AIC or BIC) one would normally use for stepwise regression, don't work.

The objective of each split is to find the division, or more precisely the variable and the division rule, which will contribute to the strongest decrease of the heterogeneity of the son nodes on the left $\kappa_l$ and on the right $\kappa_r$.
In the case where Y is a qualitative variable, several heterogeneity functions can be defined for a node: a criterion defined from the notion of Entropy or from the Gini concentration (actually there is also the CHAID criterion which is based on the statistical test of $\chi^2$).
In practice, it turns out that the choice of the criterion has little influence, and it is often Gini that is chosen by default.
In the node $\kappa$, where $p_\kappa^l$ is the number of element of the class $l$ in the node $\kappa$, and $m$ the number of classes.
\begin{align*}
\textit{Entropy} :
     &S_\kappa = - \sum_{l=1}^{m} p_\kappa^l log(p_\kappa^l) \\
\textit{Gini} :
    &G_\kappa = \sum_{l=1}^{m}p_\kappa^l(1 - p_\kappa^l) = 1 -
                \sum_{l=1}^{m}(p_{\kappa}^{l})^2
\end{align*}
As you mentioned earlier, we cannot directly use the Akaike information criterion or the Bayesian information criterion. Nevertheless, it is possible to easily apply a backward stepwise selection.
In the case of random forests, a method for selecting variables is based on the importance score of the variables (ability of a variable to predict $Y$). We thus employ a top-down (or backward) strategy where we remove step by step the least important variables as defined in the importance criterion.
At each stage of the algorithm, we calculate the prediction error. The subset finally chosen is the one that minimizes the prediction error.
The agorithm can be summed up as follows :
1. Construct a random forest and calculate the error
2. Calculate the measure of importance of each variables
3. Eliminate the least important variable
4. Repeat steps 1 to 3 until all variables have been eliminated

Comment: Many thanks, Thomas! However, I have a question concerning the second part of your post: I know entropy and Gini impurity as measures for the significance of a node in a single decision tree. Do I understand correctly that you suggest I use them as metrics for comparing different random forest models? I am not sure how I would go about this.
Comment: Do my clarifications answer your question?
Comment: Thanks! It does - however,  can you think of a "goodness of fit" criterion I could use to stop eliminating variables? At some point, I would like to arrive at an optimal model.
Answer: I find Thomas's answer very detailed, but I will nevertheless add a couple things to it:

Since random forest models already select features, is it possible to gain much by such a method?

You could have some gains from feature selection in cases of highly correlated features and when having many unimportant features. Many high correlated features might degrade the performance of your trees in the sense that, since they carry the same information, every split to one of them will affect the "remaining" information in the other ones. If you therefore split early in the tree on one of these variables, if features that are correlated to it will be selected for the subsequent splits they might generate useless or "bad" splits. In the same way, when you have too many unimportant features, since you are subsampling the variables for the splits you might find yourself making multiple splits on useless variables (to my knowledge, this is particularly a problem in application in biology).

Therefore, you might want to remove/work on highly correlated features, and at the same time always re-tune your mtry parameter, as this will need to change based on the importance and number of your variables.

What would be a suitable information criterion for a random forest model?

Here I also agree with Thomas. RF will give you variable importances and these can be used to do some selection. However: you should only use variable importances if your model is strong enough. If your model is overfitting (can happen when having simple/few data) or too weak, your variable importances might fluctuate a lot from run to run.
Also, be aware that feature importances are ALSO strongly affected by correlated features, so if you plan on using this, take it into account.
Finally, default Variable Importances based on Gini/Entropy (or splits in general for the matter) are done on the train set and are strongly biased towards numerical features or categorical features that have many levels, since they allow for splitting more times within the same tree. For this reason, you might also want to check Permutation Importances, which are instead model agnostic and can be use on a performance metric of your choice. (More here)
Comment: Thanks for your additional comments!


Commerce Kickstart Facet Displays Item Count Even With Option Disabled

Question: I'm using Kickstart2.
I imported a new set of Categories and Collections using a Taxonomy Feed Importer. Seemed to work OK but now, the facet display -always- shows the item count for -all- search vocabularies, even though I have that option disabled in each search field eg.
admin/config/search/facetapi/search_api%2540product_display/block/field_category/edit
I can't see how the Feed Import would cause this so maybe that's a red herring, but regardless, this worked properly -before- my import and now I can't turn this item count off.
Any idea how to get rid of these?
Answer: You have to configure the settings under vocabulary, go to admin/structure/taxonomy/product_category/edit and then uncheck the Display number of items that's it, it will not show you the count anymore.
Comment: I disabled that and it still displays. I think I figured it out. It's hard-coded in the current commerce kickstart facetapi for widget-links. Will post answer later.


Rendering drupal login form in a modal window

Question: I need to display the login form in a modal window when a user clicks a link on my website. I am doing that using jquery ui dialog currently.
However, once the user logs in, i don't want a page refresh instead i just want the modal window to get reloaded with some content.
how do i achieve this?
Comment: Have you tried ctools ? There is an example that does that.
Answer: Modal forms module might help you.

Modal forms make use of the modal feature in the ctools module to open some common forms in a modal window.
Supported forms:

Log in (modal_forms/nojs/login) Request new password
(modal_forms/nojs/password) Create new account
(modal_forms/nojs/register) Contact (modal_forms/nojs/contact)
Comment (modal_forms/%ctools_js/comment/reply/%node) Webform
(modal_forms/%ctools_js/webform/%node)
Answer: Use Lightbox module and mention rel in the url tag like below i mentioned
Search

you can see a live example in my site www.richtown.ae(click Mortage Calculator in bottom)
or you can use popup module to achieve this.
Answer: The Colorbox Module provides this functionallity.


Can't Import Module

Question: First of all, this is very likely not a path issue.
I have a pydev project in eclipse. Here is the directory structure:
Genetic-Framework
  | Genetic-Framework
    | Genetic
      | __init__.py
      | GA.py
      | crossover.py
      | fitness.py
      | individual.py
      | mutation.py
      | population.py
      | selection.py
      | settings.py
      | visualization.py

In GA.py, I have the following line:
from Genetic import settings, selection, visualization as vis

And yes, Genetic is in sys.path. However, I get the following error:
  File "/.../Genetic-Framework/Genetic-Framework/Genetic/GA.py", line 17, in <module>
    from Genetic import settings, selection, visualization as vis
ImportError: cannot import name settings

However, when I remove settings from that line, everything else imports just fine.
Interestingly, among the first lines of settings.py is this:
from Genetic import fitness, selection, mutation, crossover, population, GA

And when I remove GA from that line, everything seems to import just fine.
Why am I getting this error? Is this some issue with circular imports? How can I fix this?
Comment: You need to have an `__init__.py` file for this to work at all.  Is there a file named `Genetic.py` somewhere on your path?  It's possible that it's not importing what you think it's importing.
Comment: There /is/ an `__init__.py` I excluded it from the dir list in my post because I thought it was irrelevant. Post edited
Answer: Yes, that's an issue with circular imports.
The problem
The problem is that when your GA.py is run, it first tries to import settings. This means that settings.py starts getting run, and it immediately tries to import GA.
However, GA is already in the process of loading, so GA.py doesn't get run a second time - instead, settings just loads the GA that is already in memory (which is currently mostly empty, because it's still executing its imports).
Thus, things in settings that try to use things out of GA fail, because the things they're looking for in GA haven't been defined yet (because processing of GA.py hasn't gotten past the imports yet).
This makes the evaluation of settings.py raise an exception, which manifests as a failure to be imported (because an exception raised during import makes the import fail).
The solution
a) Avoid the situation in the first place.
In general, you should try to avoid circular imports in the first place. They often mean that you have really odd dependency structure that will be hard to debug later.
One way to do this is to try to find things that are needed in both modules and break them out into a separate third module that can be shared between the other two - so instead of using A.x in B, and B.y in A, you instead use C.x and C.y in both A and B.
b) Don't actually try to use things from circular imports until everything's loaded.
Another thing you can do is to defer usage of something from another module until after all of the imports have finished. In other words, don't try to reference an imported module's contents from top-level code, but instead place it in a class initializer or a function that you can call later on, once all of the imports have finished.
For example, instead of this...
import Foo

class Baz:
    top_level_variable = Foo.bar

you can do this:
import Foo

class Baz:
    def __init__(self):
        self.instance_variable = Foo.bar

Obviously, instance properties are slightly different from class properties, but the idea is to defer actually having to look things up from other modules until after all of the modules finish executing and thus have their contents available. Also note that from Foo import bar would fail here because that tries to access the contents of Foo at the time of the import, which is what needs to be avoided.
Comment: @DanielRoseman: It's true that the module must be parsed, but that doesn't mean all objects have to be fully available.  What you say is true for `from ... import ...`, but when you use a bare `import foo`, as long as the imported module can be properly parsed, you can get away with a circular import as long as you don't attempt to access variables from it until after the module is loaded, it will be okay.  The crucial thing is that in the second example in B above, the code accessing `Foo.bar` is not executed at module-import time.
Comment: You were doing well until you got to option b, which doesn't work at all. Python doesn't care when you refer to the contents of a module: the very fact of importing it means it must be parsed, which leads to the circular dependency. Moving the *import* into the `__init__` function is, however, a potential solution.
Comment: @BrenBarn nailed it. The issue occurs not when the import happens, but when the importing module attempts to *access* something within the imported module. If you can avoid such accesses (which necessarily includes avoiding `from ... import ...` format imports), then it will work.
Comment: Is there a way that I could import everything (say with a call to import in `__init__.py`) so that I can remove import statements from the individual modules themselves?

Can't Import Module


12 bar blues notation

Question: I'm trying to put a 12 bar blues to paper but the notes don't add up. It will be C/F/G and I have the rhythm like so (just the C here):

but something isn't right (and what's more, my baby done left me!)
Can you tell me what am I doing wrong? Wrong time signature?
Audio:
https://drive.google.com/file/d/1LLA69qHa8bQmdCtgm4R1IR0FpU7Iys1Z/view?usp=sharing
Here's version 2. It adds up now but the rhythm isn't quite right. It doesn't swing.
Comment: Have a look here and see if it clears things up for you. The notes add up, but you have beamed (joined) them in a way that makes them hard to read: https://www.musicnotes.com/now/musictheory/note-beaming-and-grouping-in-music-theory/ I suspect that the actually rhythm you intend is different from the written one. Pay close attention to the dotted eights and dotted sixteenths. You probably will never need a dotted sixteenth in a blues transcription.
Comment: Was that notation automatically generated by software while you played it? If so, the software may have a "quantize" function to get the rhythmic placement simpler, more natural.
Comment: Thanks for your reply. I've just recorded a short audio clip on my phone and added to the question. This should make it clear what I'm trying to do.
Comment: That could very well be. Also, I decrease the speed of the playback which was helpful.
Answer: After hearing the audio and seeing your examples, the written example 2 is rhythmically accurate BUT I would suggest this rhythm be written in quarters and eighths, not eighths and sixteenths. My reasoning for this is it doesn't FEEL like 8ths and 16ths, it feels like a faster 4/4 with a backbeat on 2 and 4 as opposed to a slow 4 with the back beats on the off eighth notes. This groove is more easily read and felt this way:

Notice the 8th rest on beat 4. You need this to get the right feel, the preceding note should not be sustained. In order to show the sustained bass notes without writing out a polyphonic staff I used slurs on the C and G to indicate they should be sustained.
Also, since it's in the key of C it's better not to use an F key signature but instead use C and put in the Bb accidentals.
Comment: It's an interesting question why we use key signatures for major and minor but not the other modes. Blues is typically mixolydian, and C mixolydian has a B♭. One could imagine a world where the key signature for C mixolydian has 1 flat, for E phrygian has no sharps or flats (lots of Spanish guitar music) and so on. It's just not the world we live in.
Comment: @AdamChalcraft I completely see the logic in what you're saying and there's nothing to prevent someone from writing in a modal key signature. I'm sure it has been done. Because it isn't commonly done especially in a genre like blues I suggested calling it a major key in C with Bb accidentals. I know if someone put this chart in front of me with one flat my first thought would be "Oh, it starts on the V chord." Maybe using modal key signatures but identifying them as such at the top would be a good way to do it.
Answer: Not knowing your intended rhythm, I'd bet your dotted sixteenth notes are incorrect; these demand thirty-second notes, which result in a "more correct" notation of the following, which I can't imagine is what you intended:

After hearing your recording, your intended rhythm is:

So as it turns out, your rhythm is actually correct—at least, once you remove the dots!
Otherwise, I'd recommend re-spelling the chord to include a B♭ instead of an A♯. This note is the seventh of the chord, and since the root is C, we prefer to spell it as the note a seventh above C (that is, some kind of B) as opposed to some kind of A.
Comment: Though augmented sixth chords _may_ be used in Blues as well, usually in minor blues in some ♭Ⅵ⁺⁶ - Ⅴ⁷♯9 where the standard major blues would have Ⅴ⁷ - Ⅳ⁷. For the tonic, the +6 interpretation would certainly be weird though!
Comment: I just added some audio to the question to show the rhythm.
Comment: @AlLelopath See edit; your rhythm was very close!
Comment: Thanks for the  B♭ suggestion. I've attached a new image to the question, take a look please. I hope I've followed you correctly.
Comment: @AlLelopath Looks correct, even if the notation program could separate the B-flats and Cs a bit better!
Comment: Its a screen capture so the resolution is not the best. It looks better in real life, so to speak.

12 bar blues notation


Can this pivot be done more efficiently?

Question: I managed to find a solution for formatting the shown driver table. The result is exactly what i need: One row for every QuoteID with the columns Birthdate and DriverType seperated by DriverIndex. My real drivertable has millions of rows.
Yet i am not convinced that this is the way to go. It just seems odd to me. But i am not a SQL expert. My Question: Can this be done in a more efficent way?
SQL Fiddle
MS SQL Server 2017 Schema Setup:
CREATE TABLE driver
([QuoteID] int, [DriverIndex] int,[Birthdate] date,[DriverType] int)
;
INSERT INTO driver
([QuoteID], [DriverIndex],[Birthdate], [DriverType])
VALUES
('72', '1','2022/01/01','11'),
('72', '2','2022/02/01','12'),
('73', '1','2022/03/01','13'),
('74', '1','2022/04/01','13'),
('73', '2','2022/05/01','10'),
('73', '3','2022/06/01','11');

Driver Table:
| QuoteID | DriverIndex |  Birthdate | DriverType |
|---------|-------------|------------|------------|
|      72 |           1 | 2022-01-01 |         11 |
|      72 |           2 | 2022-02-01 |         12 |
|      73 |           1 | 2022-03-01 |         13 |
|      74 |           1 | 2022-04-01 |         13 |
|      73 |           2 | 2022-05-01 |         10 |
|      73 |           3 | 2022-06-01 |         11 |

Query:
with sq as(select QuoteID AS QuoteID_sq, [1] AS DriverIndex_1_DriverType , [2] AS DriverIndex_2_DriverType , [3] as DriverIndex_3_DriverType
from
( select [QuoteID], [DriverIndex],[Birthdate], [DriverType] from driver) src
pivot
( max([DriverType]) for DriverIndex in ([1], [2], [3]) ) piv),
sq2 as(select QuoteID as QuoteID_sq2, [1] AS DriverIndex_1_Birthdate , [2] AS DriverIndex_2_Birthdate , [3] as DriverIndex_3_Birthdate
from
( select [QuoteID], [DriverIndex],[Birthdate], [DriverType] from driver) src
pivot
( max([Birthdate]) for DriverIndex in ([1], [2], [3]) ) piv),
sq3 as(Select * from sq,sq2 Where sq.QuoteID_sq=sq2.QuoteID_sq2)
Select QuoteID_sq as QuoteID, max([DriverIndex_1_DriverType]) AS DriverIndex_1_DriverType,MAX([DriverIndex_2_DriverType]) AS DriverIndex_2_DriverType,Max([DriverIndex_3_DriverType]) AS DriverIndex_3_DriverType ,
max([DriverIndex_1_Birthdate]) AS DriverIndex_1_Birthdate , max([DriverIndex_2_Birthdate]) AS DriverIndex_2_Birthdate , max([DriverIndex_3_Birthdate]) as DriverIndex_3_Birthdate
from sq3
group by QuoteID_sq

Results:
| QuoteID | DriverIndex_1_DriverType | DriverIndex_2_DriverType | DriverIndex_3_DriverType | DriverIndex_1_Birthdate | DriverIndex_2_Birthdate | DriverIndex_3_Birthdate |
|---------|--------------------------|--------------------------|--------------------------|-------------------------|-------------------------|-------------------------|
|      72 |                       11 |                       12 |                   (null) |              2022-01-01 |              2022-02-01 |                  (null) |
|      73 |                       13 |                       10 |                       11 |              2022-03-01 |              2022-05-01 |              2022-06-01 |
|      74 |                       13 |                   (null) |                   (null) |              2022-04-01 |                  (null) |                  (null) |

Answer: You can simplify this by using only conditional aggregation.

SELECT QuoteID
, MAX(CASE WHEN DriverIndex = 1 THEN DriverType END) AS DriverIndex_1_DriverType
, MAX(CASE WHEN DriverIndex = 2 THEN DriverType END) AS DriverIndex_2_DriverType
, MAX(CASE WHEN DriverIndex = 3 THEN DriverType END) AS DriverIndex_3_DriverType
, MAX(CASE WHEN DriverIndex = 1 THEN Birthdate END) AS DriverIndex_1_Birthdate
, MAX(CASE WHEN DriverIndex = 2 THEN Birthdate END) AS DriverIndex_2_Birthdate
, MAX(CASE WHEN DriverIndex = 3 THEN Birthdate END) AS DriverIndex_3_Birthdate
FROM driver
GROUP BY QuoteID
ORDER BY QuoteID;

QuoteID
DriverIndex_1_DriverType
DriverIndex_2_DriverType
DriverIndex_3_DriverType
DriverIndex_1_Birthdate
DriverIndex_2_Birthdate
DriverIndex_3_Birthdate

72
11
12
null
2022-01-01
2022-02-01
null

73
13
10
11
2022-03-01
2022-05-01
2022-06-01

74
13
null
null
2022-04-01
null
null

Demo on db<>fiddle here
Comment: Darn, too slow by 20 seconds ;-) +1
Comment: Thank you both!
And this approach is faster due to the fact that there are less table scans? At least thats what the sql fidlle execution plan says

Edit:
Added comparision [here](https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=7ab60a0a57f0f87d478e35a4d1043e17)


How to display specific error message using Java web.xml?

Question: New to Java, so ...
Right now the call stack is like this, (there could be details missing, but these are basic steps)

In prehandle of the http request in java, if the authentication information is wrong say wrong password from UI, we send 403 back

 response.sendError(403);

web.xml has this

   <error-page>
        <error-code>403</error-code>
        <location>/error/forbidden</location>
    </error-page>

ErrorController.java mapps /error/forbidden to

    new ModelAndView("errorPage");

errorPage.jsp will display static error messages to user.

Now at step 1, we have function can append detailed error message for the http response
sendErrorInResponse(response, errorMessage);

basically we send back specific error message with 403.
My Question is, how to pass this specific error message to the errorPage.jsp for display ?
Comment: If you're new to Java, the first thing to be aware of is that `web.xml` configuration is legacy. Modern Java Web applications (Spring Boot, Micronaut, Dropwizard) use Web servers that are configured by the tools instead of the "external deployment" model.
Comment: you are right, our existing code base in on Java 1.8, so i'm still using old stuff ...
Comment: "web.xml configuration is legacy." since when? Sure you could use a framework if you like and last I checked web.xml is still part of Servlet 4.
Comment: Probably he thinks that since he do not write web.xml, it is deprecated XDD. Maybe someday, but for the moment all those tools does the work for you (including create web.xml and many other trash).
Answer: You can do like the following in errorPage.jsp:
The error code is <%=response.getStatus() %>

Error message: <%=exception.getMessage() %>

Comment: the 1st line response.getStatus() dos work, but not the 2nd line, "exception" cannot be resolved in my errorPage.jsp. Any suggestions ?
Comment: i did this: myHttpServletResponse.sendError(403, "my msg"). So in jsp file, which method to use to get "my msg" pls ?
Answer: Where you do:
new ModelAndView("errorPage");

Change into:
ModelAndView model = new ModelAndView("errorPage");
String yourCustomErrorMessage = "This is a test";
model.addObject("msg", yourCustomErrorMessage);

Then replace yourCustomErrorMessage with the value you want.
In the JSP use ${msg} directly in the html code. Something like:

HTTP 403 Forbidden

${msg}

Hope that helps.


Read zipped binary file in C++

Question: I used to use popen(("zcat " + filename).c_str(), "r") to read zipped text files. However, I need read zipped binary file this time. I tried popen(("zcat " + filename).c_str(), "rb") but it failed as the return value is NULL even if the file does exist.
May I know why the way fails? I can guarantee the filename is properly defined, no special characters and the file does exist. The similar way works when I read zipped text files.
May I know what should be the correct way to do that?
Comment: @JerryChou "Another reason not using libraries is because the old codes does not." A bad code is bad no matter how old or 'traditional' it is. Better isolate the way you read a zip in a function/method/interface and revisit/refactor your old code to call into it. At later time, if you want to change the 'archive reading provider' you only need to make the change in exactly one place.
Comment: What's wrong with [`zlib`](http://www.zlib.net/)?
Comment: Why don't you use a proper Zip library rather than depending on pipes? Smashing the filename on the end of the command-line is wrong, it won't work with filenames that have spaces or irregular characters in them, so if you can avoid doing that, do avoid it.
Comment: Hi @tadman, the problem is that we want to avoid installing more libraries. If we can do it without library that will be great.
Comment: What operating system are you targeting? Many have some kind of library already installed, and that's a non-issue if you can build an executable with the library statically linked inside. It's better to depend on a library than some arbitrary shell command being available. This code allows people to execute arbitrary shell commands if they control the filename, so that's super bad.
Comment: @tadman, we are targeting Ubuntu. What libraries are you recommending?
Comment: @AdrianColomitchi I was going to say the same thing. It's super standard and I'm not even sure you can find a distro without that even on the most stripped down install.
Comment: Another reason not using libraries is because the old codes does not. I prefer using a similar way when I extend the functionalities. Should I do that?
Comment: I will try zlib first. I am not familiar though. Any idea why my way fails? I can confirm the filename is correct.
Comment: @AdrianColomitchi got it, thanks!
Answer: "rb" isn't a valid mode for popen. If you read the man page for popen, you'll see it says:

FILE *popen(const char *command, const char *type);

[...]
The type argument is a pointer to a null-terminated string which must contain either the letter 'r' for reading or the letter 'w' for writing.
[...]

If you look at the POSIX documentation for fopen, it says (regarding the mode string):

The character 'b' shall have no effect, but is allowed for ISO C standard conformance.

Thus, on a POSIX-ish system (like Linux), you don't need to specify binary mode when opening files, especially when using popen. Any FILE* you get back from popen will always be opened in binary mode.

If you decide to use zlib instead of popen("zcat", ...) (as other suggest, and I might recommend) there's a lot of good documentation, and I have personally found the zpipe.c demo very helpful.
Comment: Yes. I just updated my answer to talk about this point. In Linux (and other POSIX systems), `FILE*` handles from `popen` are *always* in binary mode, so you don't need to worry about specifying that with your call to `popen`.
Comment: So is it still possible to use popen for what I need?

Read zipped binary file in C++


Rudin's Proof on Riesz Representation Theorem

Question: The proof is from Rudin's Real and Complex Analysis. I am having a hard time understanding part of the Riesz Representation Theorem
The Theorem states: Every open set $E$ satisfies $$\mu(E)=sup\,\{\mu(K):K\subset E,\,K\,\,\text{compact}\}\,\,\,\,\,\,(3)$$ (Note: Here $\mu$ is a positive measure, $\Lambda$ is a positive linear functional and $\mathfrak M_F$ is the class of all $E\subset X$ that satisfies $(3)$ and $\mu(E)<\infty$.)
Hence $\mathfrak M_F$ contains every open set $V$ with $\mu(V)<\infty$
The proof goes as: Let $\alpha$ be a real such that $\alpha<\mu(V)$. There exists $f\prec V$ with $\alpha<\Lambda f$. If $W$ is any open set which contains the support $K$ of $f$, then $f\prec W$, hence $\Lambda f\leq\mu(W)$. Thus $\Lambda f\leq \mu(K)$. This exhibits a compact $K\subset V$ with $\alpha<\mu(K)$, so that $(3)$ holds for $V$.
Here is what I understand along with what I don't:
$\alpha<\mu(V)$ is clear since $\mu(V)$ is a positive measure. I am not sure whether there exists an $f\prec V$ with $\alpha<\Lambda f$. I think that the reasoning comes from the definition that $$\mu(V)=\sup\{\Lambda f:f\prec V\}$$
where $V$ is any open set in $X$. Here the $f\prec V$ with $\alpha<\Lambda f$ would be the supremum. However, I am not sure if that is true.
The fact that if $W$ is any open set which contains the support $K$ of $f\Rightarrow f\prec W\Rightarrow \Lambda f\leq \mu(W)$ is clear. However, I don't understand why this implies that $\Lambda f\leq\mu(K)$. Nor do I understand how $K\subset V$. Lastly, I don't think that I understand why the fact that $K\subset V$ and $\alpha<\mu (K)$ implies that $V$ satisfies $(3)$.
Wouldn't $\Lambda f\leq \mu (W)\Rightarrow\Lambda f\leq \mu (K)$ make sense only if $W\subset K$? But that is not the case since $K\subset W$...
Also, how was the connection between $V$ and $K$ made, i.e. $K\subset V$? What is the connection between $W$ and $V$? And lastly why does   $K\subset V$ and $\alpha<\mu (K)$ implies that $V$ satisfies $(3)$?
Any advice or hints would be appreciated. Thank you in advance.
Answer: The existence of $f$ follows from the definition of $\mu(V)$, as you said. Note, however, that this definition holds only for open sets and the definition for every set $E$ of $\mu$ is given by
$$
\mu(E)=\inf\{\mu(V):E\subset V, V\quad \mathrm{open}\}
$$
Using this, in particular we have
$$
\mu(K)=\inf\{\mu(V):K\subset W, W\quad \mathrm{open}\}
$$
The fact that $\Lambda f\leq \mu(W)$ for every $W$ such that $K\subset W$ implies that $\Lambda f$ is a lower bound of the set $\{\mu(V):K\subset W, W\quad \mathrm{open}\}$ so, by definition of infimum, we must have $\Lambda f\leq \mu(K)$. $W$ and $V$ doesn't need to have any connection, $W$ was introduced to show this last inequality.
Finally, note that Rudin defines $f\prec V$ if the support of $f$ lies in $V$. Since $K$ is the support of $f$, we must have $K\subset V$


Geometric Riemann-Roch for Hyperelliptic curves

Question: I'm studying Riemann surfaces and I just met the geometric interpretation of Riemann-Roch according to which
$$
dim(span(D))=deg D- l(D)
$$
where $D$ is a positive divisor and $span(D)$ is the intersection of every hyperplane intersecting the image of $supp(D)$ via the canonincal map $\phi$ with proper multiplicity.
I see that it is always assumed that the Riemann surface $X$ is not hyperellip^tic so that $\phi$ is an embedding. It is possible to generalize this to the hyperelliptic case?
I tried to repeat the argument for an explicit example: the hyperelliptic curve of genus 3 arising by considering $y^2 = x^8+1$.
Take the points $p_1 = (1,0),\ p_2=(-1,0),\ p_3=(i,0),\ p_4=(-i,0) $ and consider the positive divisor $D = p_1+p_2+p_3+p_4$. Consider now the canonical map $\phi:(x,y)\rightarrow (1:x:x^2)\in\Bbb{P}^1$, so that the images of the $p_i$ are
$$
\phi(p_1) = (1:1:1)\\
\phi(p_2) = (1:-1:1)\\
\phi(p_3) = (1:i:-1)\\
\phi(p_4) = (1:-i:-1)
$$
It seems to me that the only linear combination annihilating them is $-x_0+x_1-ix_2+ix_3$, so I would say that the smallest linear subspace of $\Bbb{P}^3$ containing the images is
$$
H: -x_0+x_1-ix_2+ix_3=0
$$
whose dimension is 2.
By algebraic Riemann-Roch I know that $l(D)=1+4-3=2$ and so, in fact, I get $2 = 4-2$.
First question: is this argument right?
Second question: if previous answer is "yes", how does it differ from non-hyperelliptic case?
Now I consider a divisor whose support is made of point where tha canonical map fails to be an embedding. Consider $D'=q_1+q_2+q_3+q_4$ where $q_1=(0,1),\ q_2=(0,-1),\ q_3=(1,\sqrt{2}),\ q_4=(1,-\sqrt{2})$.
Now we get
$$
\phi(q_1)=\phi(q_2) = (1:0:0)\\
\phi(q_3)=\phi(q_4) = (1:1:1)
$$
And we have two hyperplanes $H_1,\ H_2$ annihilating them, namely
$$
H_1 = x_0-x_1\\
H_2 = x_2-x_3
$$
$H_1\cap H_2$ should be the projective line given by $(x:x:y:y)$, so it has dimension 1, but again $degD' = 4$ and $l(D')=2$ and $1\neq 4-2$
Am I missing something?
Thank you for any hint!
Answer: First, yes, the geometric interpretation of Riemann-Roch works for hyper-elliptic curve.
You seems to be confused with the target of the canonical map. The canonical map for a genus 3 curve has target $\mathbb P^2$, instead of $\mathbb P^3$. The map is given by $(x,y)\mapsto x\mapsto (1:x:x^2)$ as composite of projection to the $x$-line and the degree-$2$ Veronese embedding. So for the divisor $D$ in your first example, $\text{Span}(D)=\mathbb P^2$, and the formula reads
$$2=4-l(D),$$
so $l(D)=2$.
For the divisor $D'$ in your second example, it actually coincides with the canonical divisor $K_X$ on $X$, so $\text{Span}(D')=\mathbb P^1$, and the formula reads
$$1=4-l(D'),$$
so $l(D')=3$, which coincides with the genus of the curve. You know $h^0(K_X)=3$, so the formula is correct.


Human intuition behind SVD in case of recommendation system

Question: This does not answer my question. I struggled very hard to understand the SVD from a linear-algebra point of view. But in some cases I failed to connect the dots. So, I started to see all the application of SVD. Like movie recommendation system, Google page ranking system, etc.
Now in the case of movie recommendation system, what I had as a mental picture is...
The SVD is a technique that falls under collaborative filtering. And what the SVD does is factor a big data matrix into two smaller matrix. And as an input to the SVD we give an incomplete data matrix. And SVD gives us a probable complete data matrix. Here, in the case of a movie recommendation system we try to predict ratings of users. Incomplete input data matrix means some users didn't give ratings to certain movies. So the SVD will help to predict users' ratings. I still don't know how the SVD breaks down a large matrix to smaller pieces. I don't how the SVD determines the dimensions of the smaller matrices.
It would be helpful if anyone could judge my understanding. And I will very much appreciate any resources which can help me to understand the SVD from scratch to its application to Netflix recommendation systems. Also for the Google Page ranking system or for other applications.
I am looking forward to seeing an explanation more from human-intuition level and from a linear-algebra point of view. Because I am interested in using this algorithm in my research, I need to understand as soon as possible: how does the SVD work deep down from the core?
Answer: You're confusing the SVD with a matrix completion algorithm. The SVD takes an $(m \times n)$ data matrix $M$ and factors it into $M = U \Sigma V^\text{T}$, whereas a matrix completion algorithm takes a matrix with missing entries and fills them in according to some criterion. In particular, the SVD is not a collaborative filtering technique for recommendation systems like you're talking about, and it factors any matrix into three matrices, not two, and it cannot accept a matrix with missing entries as input.
If what you really want is some intuition about matrix completion algorithms, you need to understand that the key assumption behind them is that the given $(m \times n)$ matrix $M$ has a low rank, which means that $\text{rank}(M) < \min(m, n)$. In the case of the Netflix problem, we suppose that all Netflix customers fall into one of several groups that all rate movies approximately the same way. If we have only 5 movies that are being considered, and 6 customers, you might have a ratings matrix like this
$$
\left[
\begin{matrix}
1 & 1 & 5 & 5 & 5 & 2\\
2 & 2 & 1 & 1 & 1 & 1\\
5 & 5 & 5 & 5 & 5 & 3\\
5 & 5 & 4 & 4 & 4 & 4\\
3 & 3 & 2 & 2 & 2 & 4
\end{matrix}
\right]
$$
where each row corresponds to a movie and each column corresponds to a customer. The customers fall into three different groups, with the customers 1 and 2 having identical ratings for all 5 movies, customers 3, 4, and 5 having identical ratings for all 5 movies, and customer 6 having a group with only themselves. This makes the matrix have $\text{rank}(M) = 3$, because there are only three linearly independent columns. If this is the true ratings that each customer would give if they saw and rated all 5 movies, then if we erased an entry to make a matrix
$$
\left[
\begin{matrix}
1 & 1 & 5 & 5 & 5 & 2\\
2 & 2 & 1 & 1 & 1 & 1\\
5 & 5 & 5 & * & 5 & 3\\
5 & 5 & 4 & 4 & 4 & 4\\
3 & 3 & 2 & 2 & 2 & 4
\end{matrix}
\right]
$$
where $*$ denotes an unknown or erased entry, knowing that $\text{rank}(M) = 3$ is enough information to fill in the missing entry because if it was anything other than a 5 the rank of the matrix would then be 4 not 3.
To intuitively understand how the SVD relates to solving this problem, you also need to understand that the entries of the matrix $\Sigma$ (called the singular values of the matrix $M$) also tell you about the rank of $M$. To be specific, $\text{rank}(M) = \text{(the number of non-zero singular values)}$. Matrix completion algorithms are more complicated in reality, but the idea is essentially the same as in this simple example with one erased entry.
To learn what you need to understand matrix completion algorithms, you're going to have to learn a fair amount of linear algebra. A textbook might be the best place to start, but you could try learning about these topics in sequence:

The rank of a matrix
The eigenvalue decomposition of a matrix (precursor to the SVD)
The SVD
Matrix completion
Comment: I don't have depth knowledge on linear algebra. I am reading two text book. **Introduction to Linear Algebra by Dr.Gilbert  Strang** and http://textbooks.math.gatech.edu/ila/
Comment: In case of netflix problem, as per your explanation, it will have incomplete matrix. So, matrix completion algorithm is required, here. Again in many blogs why they say SVD is used in recommendation system? What SVD actually do here, then?. You pointed out some links to study in sequence, very appreciated. Do you think I will get proper picture of SVD by reading in this sequence?
Comment: The SVD is only one part of a matrix completion algorithm (of which there are many types). Whether you can learn these from reading Wikipedia depends on your linear algebra background. If you know linear algebra very well then they are probably sufficient, but learning linear algebra itself is best done from a good text book.