How is the learning process for a NN implemented?

Question: Thanks to BP and some scoring/loss functions, a NN can train to do the job as demanded. Or at least try to do so.
I think I've understood BP but still I wonder about the following:
Let's say there is a certain NN and the input is a single number. The NN will propagate the information through all layers and all weights and activation functions and so on will finally output a single number as well. If the output number is the same as the input number, nothing will happen. In case, it is different, the error will result in a change of the weights such that the NN will provide this number according to that certain input number.
I think I also understood this.
What I don't get is: How is it done such that the NN can handle multiple/different inputs?
Assume $x = 3$.
The NN will now adjust the weights to provide this as output.
Next iteration, $x = 8$.
The NN will now adjust the weights to provide this as output.
From my understanding, the NN is now trained to put out $8$. And it lost its capability to reconstruct $3$.
Obviously this is not the fact. So how is it done that the NN can handle both the $x = 3$ and $x = 8$?
Something like a lookup table is probably too plain (and dumb). I think the NN learns to generate something like a (single) function. Does that make sense?

Despite that, what does the implementation concept look like? Just the idea of it.
Comment: On the other hand, you seem uncertain of whether a neural network is a mathematical expression (it is) or a lookup table (it is not). But your citation of the backpropagation article seems to indicate that you know that a neural network is a mathematical expression because the whole article is oriented around explaining how to update weights and biases to predict inputs well. So I don't understand what it is you're asking: do you need confirmation that a neural network is not a lookup table? Or an explanation of how a neural network makes a prediction?
Comment: The latter :) Yes, finally it is about the prediction. This is actually the precise question I am concerned with.
Answer: A node is a (usually non-linear) function that combines the input. The entirety of the neural network is an additive model of nodes that in turn are connected to either the next hidden layer, or the output layer. Training learns the weight of connections: How different combinations of the input relate to the output.

From my understanding, the NN is now trained to put out 8.

This is where your confusion arises. The networks has learned neither to output $8$, nor $3$, but instead how to combine the input such that the predicted output is close to the actual recorded output.$^\dagger$
I may be reading too much into your question, but it also seems you might be confused about what the input is: It is possible for the input to be the same as the output (this is the case for autoencoders), but more generally, you have a completely different input (e.g. images of animals) used to learn the output (e.g. the type of animal).
$\dagger$: I say close because we do not want to learn how the input relates output of the sample, but rather how the input of any sample from this population relates to the outcome of any sample from this population. If it would predict exactly the recorded output in the sample (e.g. $8$ and $3$), the problem is either very simple, or it is overfitting.
Comment: Yeah, with the caveat that we don't just want a network so vastly overcomplete that it can memorize the training data. Instead, it should learn the *structural* part of the data-generating process, while ignoring the noise. In other words, we don't want it to learn which specific input corresponds to which specific output, we want it to learn how input in general relates to output.
Comment: @Ben Okay, think of it this way: From a suitable random initialization, each example shifts the weights *slightly* towards a state that would minimize the reconstruction error on that example. By seeing many different examples, each of which shifts the state of the neural network *slightly* towards one that would recover that particular example, the network will learn combinations of features that occur in many different examples, not something about any specific example. That way, if those relevant combinations also exist in unseen data, it can generalize to some larger population.
Comment: @Ben In addition, mini batch sizes are usually greater than $1$, in which case backpropagation moves along the gradient of a multitude of examples.
Comment: (...) Now consider an example with noise: The input is still $6$, but the output is $3.014\dots$. By using a low enough learning rate, we move *towards* $\hat{\beta}=0.507\dots$, but not *exactly* there. Repeat for another noisy example: Input $16$, recorded output $7.924\dots$, we this time move *towards* $\hat{\beta}=0.495\dots$. Repeat this many times and we (hopefully) learn the function that generalizes towards a larger group $f(x)=0.5\cdot x$. The big difference in a neural network is the number of parameters, but it still works like any other function.
Comment: Thanks! Yes, you're right, I am diving into Autencoders so I didn't pay attention how I describe my question. Nevertheless, let's say the NN learns a specific output for a specific input. How does it..  generalize? Why or how do different inputs result in different outputs? What actually is the crucial component for that? Is it "just" a n-dimensional function?
Comment: I think I understand it conceptually but still can't figure out how it works on a low-level(?). For example, the input for a NN is typically/always(?) an array of numbers. When I use the gradient, it will result in a zero.
When you describe that different examples "shift the state of the NN towards one which would recover the examples" -> What exactly is this state? How is it embodied? And/or even implemented?
Comment: The state refers to the current set of weights and biases.
Comment: Well, but how can one set of weights and biases (representing such a state) satisfy lots of several inputs? I understand how it works for a single number, but how does it work for..  100000 numbers?
Comment: A neural network needs input. If given nothing, it does not say "8" or "3"or anything else. It works just like any function. A simple linear regression can also predict an output for 100000 different numbers. Suppose you have some very simply function $f(x)=\beta x$. If you input $6$ and the recorded output is $3$, then the weight ($\beta$) moves towards $0.5$. If you then have another example with input $16$ and the recorded outcome is $8$, the single weight moves further towards $0.5$. Do this enough times and you learn the function $f(x)=0.5 \cdot x$. (...)
Comment: Finally: A new, unseen observation. This time we only have an input, $x=4$. Can you see now how we can arrive at $y=2$? We can enter any number for $x$ and it will still produce an output. Same goes for the function a neural network learns.
Comment: To conclude: A NN is basically a function and learning literally means, constructing this function? When I try to see that visually, it looks like quite the opposite of a SVM. There, a function is drawn inside the space to separate the data and none is incident with the function while a NN constructs a function where as most as data points live in as possible :)


Rails 5 - embed a register interest form in root page

Question: I've made a scaffold in my Rails app called Leads. The table has 1 attribute, being :email.
My objective is to render the form input field in the homepage.
I have a HomeController, to which I've added this "new" method.
class HomeController < ApplicationController

  def new
    @lead = Lead.new
  end

end

In the home/index view, I have:
        <%= render 'leads/form', lead: @lead %>

In the form partial I have:
    <%= simple_form_for Lead.new do |f| %>

      <%= f.input :email, :label => false, :placeholder => "your email address", input_html: { class: 'cform'  }   %>

    <%= f.button :submit, 'Go', class: 'cformbutton' %>

<% end %>

I'm trying to follow the advice in this post to get the form input field to show in the home index.
I cant find a way to make it work.
It works fine in the development environment but then when I push to heroku, I get an error that says:
FATAL -- : [a6e63810-1660-4e83-86d9-bfd6ae54b344] ActionView::Template::Error (PG::UndefinedTable: ERROR:  relation "leads" does not exist
2017-03-01T21:19:11.541780+00:00 app[web.1]: F, [2017-03-01T21:19:11.541735 #4] FATAL -- : [a6e63810-1660-4e83-86d9-bfd6ae54b344]
2017-03-01T21:19:11.541824+00:00 app[web.1]: LINE 8:                WHERE a.attrelid = '"leads"'::regclass

My lead.rb has no associations on it. I don't understand why there need to be any associations.
Can anyone see what I need to do in order to get this form to work in the production environment?
Answer: The error PG::UndefinedTable: ERROR:  relation "leads" does not exist clearly says that your leads table does not exists. That's because you most likely forgot to run migrations on Heroku.
To fix it run this command heroku run rake db:migrate --app your_application_name. Or drop --app part if you have only one application.
Please be aware that Heroku does not automatically run your migrations when you deploy new code. You need to do it manually every time you add new migrations. If you want to automate this, there are many options. I think the easiest will be to add this buildpack https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks.
Here are the steps if you want to go with this options:
heroku buildpacks:add https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks --app your_application_name
heroku config:set DEPLOY_TASKS='db:migrate' --app your_application_name


The coincidence of permutations

Question: Let $K$ be a field and let $V$ be a finite-dimensional $K$-vector space.
(a)Assume that $K$ is infinite. Show that $V$ is not the union of finitely many proper linear $K$-subspaces.
(b)Assume that $K$ is finite and $V$ is non-zero. Let $S$ be the set of affine hyperplanes of $V$. Let $g:V \longrightarrow \mathbb{R} $ be a function. The Radon transform $Rg:S \longrightarrow \mathbb{R} $ is defined by $(Rg)(\zeta)=\sum_{x \in \zeta}g(x)$ for $\zeta \in S$. Show that $Rg=0$ implies $g=0$.
(c)Let $v_1 , \cdots , v_n , w_1 , \cdots , w_n \in V$. Assume that for every $K$-linear map $f:V \longrightarrow K,(f(v_1) , \cdots , f(v_n))$ and $(f(w_1) , \cdots , f(w_n))$ coincide up to permutation of the indices. Deduce that $(v_1 , \cdots , v_n)$ and $(w_1 , \cdots , w_n)$ coincide up to permutation of the indices. Here we make no assumptions on $K$.
Assume (a) and (b) is proved.
For (c), in the infinite case, by subtacting $v_1$ to all vectors, this is equivalent to prove:
'If $w_1 , \cdots , w_n \in V$ are such that for each $f\in V^*$ one of the $f(w_j)=0$, then one of the $w_j=0$.'
Then we'll have $V^*=\cup_{i=1}^nAnn(w_i)$, and by (a) there exists a $j$ s.t. $Ann(w_j)=V^*$.
With this hint I tried to prove the finite case but this method is now no longer useful. I suppose this needs to use (b) but I don't know how to use it. How to solve the finite case of (c) ?
Comment: By "the finite case", you mean when $K$ is assumed to be a finite field?
Comment: Yes, after the infinite case was solved we need to solve the case when $K$ is a finite field.
Answer: For the case when $K$ is finite, we can prove by induction on $n$. If $n=1$ this is trivial. If $v_i= w_j$ for some $i, j$, then we can apply induction hypothesis to conclude the proof. So we may assume that $v_i\neq w_j$ for any $i, j$ and we are going to find a contradiction. Define $g:V\to \mathbb{R}$ by $g(v_i)=1$, $g(w_j)=-1$, and $g(x)=0$ for other $x\in V$.
Then for any affine hyperplane $\xi$,
$$Rg(\xi)=\#\{v_i\mid v_i\in \xi\}-\#\{w_j\mid w_j\in \xi\}.$$
Recall that $\xi$ is defined by $\{x\in V\mid f(x)=a\}$ for some $f\in V^\vee$ and $a\in K$.
Then the assumption implies that $Rg(\xi)=0$ for each $\xi.$
This implies that $g=0$, a contradiction.

The coincidence of permutations


Spring Security Custom Authentication - AuthenticationProvider vs UserDetailsService

Question: As far as I can understand when you want custom authentication in Spring Security you can either implement a custom AuthenticationProvider or custom UserDetailsService.
@Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            //.authenticationProvider(authProvider)  // option 1
            .userDetailsService(userDetailsService); // option 2

    }

In the AuthenticationProvider you can check the username and password and return Authentication with your custom object in it.
public Authentication authenticate(Authentication authentication){
        if (checkUsernameAndPassword(authentication)) {
            CustomUserDetails userDetails = new CustomUserDetails();
            //add whatever you want to the custom user details object
            return new UsernamePasswordAuthenticationToken(userDetails, password, grantedAuths);
        } else {
            throw new BadCredentialsException("Unable to auth against third party systems");
        }
    }

In the UserDetailsService you get only the username and when you return the custom UserDeatails, the framework performs a check on the password.
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        CustomUserDetails user = new CustomUserDetails();
        //add whatever you want to the custom user details object
        return user;
    }

Looks like both can produce similar results. So the question is what is the difference? When to user one vs the other?
Comment: Thank you for this question. It clarified the architecture for me just because you asked it.
Comment: @AdamEdison-MusicEducator I'm glad it helped.
Answer: The answer is inside your question.
when you are using a different authentication system, and the password is not provided in your own database/data model, you have to use the AuthenticationProvider. for example, I've worked in a project that the customer had a centralized authentication system (CAS), so my system had no idea about the password, I had to implement the AuthenticationProvider and send the given password to the CAS, and act according to its answer.
But in another system, I was storing the password in my database, so all I had to do was implementing the UserDetailsService and check if the user exists in my database or not, spring-security had to do the rest.
Comment: In my project I am creating microservice for checking authentication. That is using UserDetails service and already deployed in AWS. And I am creating another service which calls this authentication service which already deployed in aws, In this case which service I can choose? can I choose authenticationProvider ? . And also what is the difference between authenticationProvider and authenticationManager?
Comment: @Jacob  authenticationManager delegates the auth task to its list of autentication providers,each provider performs its own authentication method,and on this basis returns the authentication or throws exception
Answer: From spring security documention,
https://docs.spring.io/spring-security/site/docs/5.0.0.RC1/reference/htmlsingle/#overall-architecture

There is often some confusion about UserDetailsService. It is purely a DAO for user data and performs no other function other than to supply that data to other components within the framework. In particular, it does not authenticate the user, which is done by the AuthenticationManager. In many cases it makes more sense to implement AuthenticationProvider directly if you require a custom authentication process.

AuthenticationProvider and UserDetailsService have different purpose.
AuthenticationProvider authenticates(compares) the user(request) provided username and password against system User(This can be any system like DB which maintains list of registered users)
It is the responsiblity of UserDetailsService Implementation to get the System User Details that match with user provided Username. Here it just gets the users that have same username and does not tell the application whether authentication is successful or failed.
Example :
Spring provides the following as a default setup to authenticate a user details against database

AuthenticationProvider - DaoAuthenticationProvider
 which extends AbstractUserDetailsAuthenticationProvider that calls the authenticate method by passing on username, Authentication object

UserDetailsService - JdbcDaoImpl

Flow of authentication

DaoAuthenticationProvider responsibility is to authenticate the username and password obtained from request with Database user.
To get the corresponding Database User, it asks UserDetailsService Implementataion JdbcDaoImpl to get a UserDetail object from database with name same as request username.Here JdbcDaoImpl just fetches the UserDetails object from system.It will either send back the user found in DB or send an exception that user is not found.
If user details are found in DB, DaoAuthenticationProvider then proceeds with checking on request user password with DB found user password else fails the authentication.
DaoAuthenticationProvider will respond whether the user is authenticated or not based on JdbcDaoImpl response.

Have a look here to understand it better:
AuthenticationProvider - DaoAuthenticationProvider
  extends AbstractUserDetailsAuthenticationProvider
UserDetailsService - JdbcDaoImpl
UserDetails - User
Comment: i thought by default it uses InMemoryUserDetailsManager (which is also an implementation of UserDetailsService), not JdbcDaoImpl ?
Answer: Those two are related but is intentionally separated by Spring Security. If the enterprise has multiple systems, UserDetailsService will provide the specific user's information held by your particular system even though the authentication MIGHT be carried out by totally another system. In a simple system, they can be combined. For example, a database call will verify the username/password and retrieve all that user's email, ID and etc.
According to Spring Security Reference:
http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#getting-started
There is often some confusion about UserDetailsService. It is purely a DAO for user data and performs no other function other than to supply that data to other components within the framework. In particular, it does not authenticate the user, which is done by the AuthenticationManager. In many cases it makes more sense to implement AuthenticationProvider directly if you require a custom authentication process.


Servlet Initialization parameters using annotation

Question: I am trying to learn Servlet annotations and came across this snippet
@WebServlet(urlPatterns="/MyPattern", initParams={@WebInitParam(name="ccc", value="333")})

This makes sense to me. However, I don't understand why it is not like this
@WebServlet(urlPatterns="/MyPattern", initParams={(name="ccc", value="333"), (name="abc", value="1")})

So, the question is why we need to put @WebInitParam annotation when we already declared the attribute as initParams. It seems redundant to me, or am I missing something?
Answer: The alternative you suggest would not even compile.
When you look at the JLS, it states this:

It is a compile-time error if the return type of a method declared in
  an annotation type is not one of the following: a primitive type,
  String, Class, any parameterized invocation of Class, an enum type
  (§8.9), an annotation type, or an array type (§10) whose element type
  is one of the preceding types.

So in order to group name and value together, which represent the initialization parameter the only option is to use annotation (@WebInitParam in this case) with corresponding values set as its parameters.
Answer: As with most questions about language design choices we can only speculate here. I think some reasons for this are:

Keeping the language simple.
It is kind of redundant, but the syntax for annotations can be reused and does not require new language constructs. This makes it easier to parse and to read. Sure, It's longer, but it's also more explicit to write the annotation's name.
Don't restrict possible future language enhancements.
The proposed syntax would not work if annotations would support inheritance. I don't know if that's even a planned feature but it would not be possible to implement straightforward it if the type can be omitted.

In many cases an array of annotations seems like a workaround anyway. It can be avoided in Java 8, where you can add multiple annotations of the same type:
@WebServlet(urlPatterns="/MyPattern")
@WebInitParam(name="ccc", value="333")
@WebInitParam(name="abc", value="1")

(I don't know if the servlet api actually supports this yet though)


What's so special about unipotent groups

Question: Why are they so important?
I see them appear in Lie theory, algebraic geometry, etc.
Can somebody elaborate?
For example, can someone explain why they are such natural groups to consider in algebraic geometry?
Answer: They are solvable groups and there representation theory is by far easier than the general representation theory (Mackey-Clifford-etc.)
In principle, it is sufficient to classify the representations of unipotent groups and reductive groups, in order to classify all the representation in full.
Every lie or algebraic group is a semi direct product of a reductive one and a unipotent one.
Also the reductive subgroups of a reductive group determine to a large extent the representation theory via parabolic induction. This involves necessary understanding the parabolic subgroups, hence the unipotent subgroups.
Comment: So the algebraic group $\mathrm{GL}_n$ can be written as the semi-product of which reductive and which unipotent one? (This must have something to do with some well-known matrix decomposition.)
Comment: @Harry: $GL_n$ is reductive (its only normal subgroups are central).
Comment: A nontrivial (but still easy) example of the decomposition is given by the Borel subgroup (maximal closed solvable subgroup) of a reductive linear group; in $GL_n$, we can write $B = UT$ where $B$ is the (solvable) subgroup of upper-triangular matrices, $U$ is the subgroup of strictly-upper-triangular matrices (i.e. 1's on the diagonal; this is unipotent and normal in $B$) and $T$ is a maximal torus of $G$ (diagonal matrices), which is reductive.


Display a menu on all pages except a specific page type

Question: In my site, I have a menu that should be displayed on all pages, except on pages of a certain type.
In "admin -> structure -> blocks" I can set the visibility parameters of this menu to "pages of a certain type", and check all page types except the one I dont want. This has 2 problems :
1) whenever I add a new page type, I need to update this menu visibility (minor problem)
2) the menu will not be visible except on pages. For example, on the contact page, there is no menu.
What should be the right way to do that ?
Answer: I suggest using the context module for better and more advanced handling of blocks.
Alternatively, you could use pathauto to create a unique url for that content type (like mysite.com/type/content ) and set the block to show up everywhere, excluding 'type/*'. If you do this, make sure to rebuild your urls.
Answer: If you use aliases for the content type using 'pathauto' module you can select types by their path.  Then you would set the block as follows:
Select the "configure" option for the specific block
select the pages tab
select the radio button "All pages except those listed".
List the pages to omit similar to this:
<code>contentType1/*
contenttype2/* ( one type per line)
</code>


uploading videos in Djnago rest framework

Question: I have created an post api which will upload images and videos for blogs. I can handle multiple images, not a problem there. But I have to send a video as well from the frontend. I have used Filefield for video. I am no expert in apis but what I think is, since the code I have written is synchronous, everything that I have written will be be done at the same time. Now the issue is, if a user wants to upload a very large video for eg 200-500 mb then, the post api call response will be very long. Is there a way I can save the blog post first, return the response and then start uploading the video in the database of live server.
My models:
class Blogposts(models.Model):

    blog_title = models.CharField(max_length=100blank=True)

    video  = models.FileField(upload_to="media",
                                     null= True)

I tried to use django signals, but then again django signals are also synchronous.Everything I stated above might be wrong as well, I am not sure. But how to approach this?? Or should I use django celery??
Comment: Yes. Just create two separate requests. Create the post, get its id back. Then upload the video, using the id to attach it to the correct post.
Comment: Do a separate request for the video?
Comment: I dont know what is the best approach...my thinking is created a blog post first without the video and later start uploading the video asychronously.
Comment: hmn,...what about the time taken to upload the video on the form of the frontend itself??
Comment: Well ideally you would not use a plain form but some kind of JS to perform a [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) in the background.
Comment: sorry i am new to this...but does that mean, it will first take time to upload in the frontend form and then after uploading, it again takes time to upload it to db at the prod server??? after post api call.
Comment: You normally do not store files in a database server to begin with. The exact details of how you deal with files depends on your overall deployment. That's something you have to design. There is more to a fully working website than the application tier :)
Answer: DRF already have partial updates. Use that. Meaning, you make a post request for only the non file fields. Make the video field blank true null true.
Once its done successfully, you get back the id of your post in response. Then make a patch request to upload the file.
Your javascript algo can look roughly like this
const handleFormSubmit => (data) {
  file = data.video_file;
  delete data.video_file;
  axios.post('/blogs/', data).then(response => {
    updateUiShowCreatedBlogWithVideoUploading();

    id = response.id;
    fileUploadRequestData = {
      file: file
    }
    axios.patch('/blogs/${id}/', file).then(response => {
      updateUiShowCreatedBlogWithVideoUploaded();
    });
  });
}

But in general, for large files I would directly upload it to an object store (like S3) from the browser and not bother django process. Once it's done, attache the object like in the patch. In this case your model will just a URL field and not FileField.
If that's too much, at-least use django-storages and let the library handle uploading to S3 from your backend.
Comment: Any object store is fine. DigitalOcean Spaces have the same API as S3. But I haven't tried the browser to storage with Spaces. Plus videos are a bit too complex in general and better to deal with it separately. Uploading is just one part. then transcoding and serving. If you serve it directly from your VM, you will soon hit your transfer limits.
Comment: Not at all. It just solves your async problem - makes the user not wait for all 200MB upload to finish. For the rest it depends on your requirements actually. If you are the only one uploading videos - don't worry much and FileField is just fine with `django-storages` setup (DO Spaces with CDN enabled solves the serving problem). And you are responsible for making bandwidth / user device friendly videos locally. Otherwise checkout https://aws.amazon.com/elastictranscoder/ . I made something many years ago as well https://transcode.io/ but never managed to sell it :p
Comment: s3 is used if we used aws as a server for backend? what if we use digital ocean or something like that @Amrit??
Comment: So as you said "DRF already have partial updates. Use that. Meaning, you make a post request for only the non file fields. Make the video field blank true null true.

Once its done successfully, you get back the id of your post in response. Then make a patch request to upload the file." by doing this, will it solve problems like you said in serving etc?? also do i make a put request or patch??
Comment: @Reactoo I see you haven't marked this as answer yet. Let me know if you have any more queries :)


how to set number separated by comma in crystal report

Question: How to set number in crystal report separated by commas.
There is a provision for thousand separator. But actually i need the number in the form like the below example.
Eg : 12,34,556.00
not like 1,23,456.00
Comment: What is your exact requirement
Comment: need to show to the figure in the form of ##,##,##,###.##
Comment: thousand separator gives the number in the form like ###,###,###.##
Answer: What you probably want is Indian currency format i.e Lakhs and not Millions.
This is based on your culture information.
You can use a formula to format each field.  Right-click on one of your numeric fields, select "Format Field" and select the Common tab.  Click on the formula (X+2) button to the right of "Display String" near the bottom and enter a formula like:
<code>CStr (CurrentFieldValue, "#,##,##,##,##,###.00")
</code>
Give this a try and see if it displays correctly.
Comment: Great. For next time, please make sure you post a question that can be easily understood by everyone.


Drupal method to retrieve all existing roles and all users of a specified role

Question: I wish to retrieve the list of all users that belong to a particular role.
The query is to know:

if there's any drupal method that provides list of all available roles created under Admin >> User Management (admin/user/roles) similar to retrieving all node types using node_get_types method.
if there's a drupal method to get list of all users who belong to a particular role.

Though I could get this done by querying the role table and using the result with user_roles table, I felt that there might be some in-built method in drupal which must enable me to achieve the desired result which i might've missed out searching.
I'm aware that it's not ideal to check user permission related stuff with roles and rather user permissions instead from this post.
It'd be great if I could get a definite answer for my above mentioned queries.
Answer: You can use the user_roles() function to get a list of the roles in the system. You can even pass in specific conditions, for example if you want to get only the roles that contain a certain permission.
As far as getting a list of users by role, there is no such function, unfortunately; you'll have to write a small helper function yourself. I usually use something like this:
function users_by_role($role_id) {
  $sql = 'SELECT uid FROM {users_roles} WHERE rid = %d';
  $q = db_query($sql, $role_id);
  $uids = array();
  while ($row = db_fetch_array($q)) {
    $uids[] = $row['uid'];
  }

  return $uids;
}

That'll give you the user ids for a given role, after that you can use user_load() to get the user object for each, or simply add a join to the above query and include the data you need directly.
Comment: `db_result()` returns a single value, but it can be used in a loop; I tried with `$q = db_query('SELECT uid FROM {users_roles}'); while ($uid = db_result($q)) $uids[] = $uid;` and it worked. The fact the function returns the value of a single field doesn't mean it doesn't return the value for different rows.
Comment: @kiamlaluno The query's getting multiple results...all of the user ids that are related to a specific role. I think it's very unlikely that there will only be one user per role :)
Comment: As the query is getting a single field, `db_result()` can be probably be used.
Comment: @kiamlaluno I didn't know you could do that but it does make sense, thanks for the tip :)
Answer: user_roles() returns an array of roles. There is no function that returns all users of a particular role.
Answer: You can use Views to query a list of users, and filter them by Roles.
Or you can specify an Argument of User: Roles on the view. Then use user_roles() in a custom module to grab a list of all roles. Loop through the roles array, and inside the loop use views_embed_view('my_users_view', 'display_id', $roleid) passing the role id to this function as a third argument.
Comment: Thanks for the response, but my query was to know if there was a Drupal method for this without me doing anything other than calling a pre-defined function.


Starting Cassandra from Jenkins

Question: I'm trying to start a Cassandra instance (0.8.10) from Jenkins (latest version, 1.463).
Inside a "free-style project" job, I have a "Execute shell" build step, where I have tried a couple of approaches:
.../tools/apache-cassandra-0.8.10/bin/cassandra -f

and
.../tools/apache-cassandra-0.8.10/bin/cassandra

The first approach starts Cassandra ok, but Jenkins doesn't exit the build and keeps on building. If I stop the build, the Cassandra process dies as well.
The second approach fails because the Cassandra project dies as soon as the build finishes.
I have also tried:
.../tools/apache-cassandra-0.8.10/bin/cassandra -f &

that is kind of lame, and doesn't work anyway.
Any ideas on how to start Cassandra from Jenkins?
Answer: Try using nohup with &. Also pipe stdout and stderr to a file or /dev/null:
nohup .../tools/apache-cassandra-0.8.10/bin/cassandra -f > /dev/null 2>/dev/null &

Comment: Actually, I have tried that, it doesn't work. It seems that there is a problem in jenkins with spawned processes: https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller
Comment: if I use "export BUILD_ID=dontKillMe" it kinda works. The build fails but the Cassandra process is started
Comment: So you did solve your problem, didn't you? Please provide it as an answer, then. Also, maybe what you really need is [monitoring an external job](https://wiki.jenkins-ci.org/display/JENKINS/Monitoring+external+jobs).
Comment: not really, as the build is marked as failed, even though the process is actually started
Comment: Can you then please update your question with that info and also include build console output?

Starting Cassandra from Jenkins


What is the connection (if any) and difference between logistic regression and survival analysis?

Question: What is the connection (if any) and difference between logistic regression and survival analysis?
If we build two models one using logistic regression to predict if the instances will survive or not. Another is using survival analysis. Can we compare two model's coefficients?
Comment: In discrete survival analysis, you can  use logistic regression to predict survival in one period given survived up to previous period. Then survival iupnto time n is product of survival at time i for I≤n
Answer: They are different categories of things - survival analysis is the analysis of data where time to a given event is the dependent variable. The given event may include death, failure of a machine, a criminal's time to (re)offending or becoming ill, for example. It uses a number of techniques to analyse this data, including certain generalised linear models, including sometimes for the purpose of analysing whether specific variables influence the probability of an event, logistic regression.
Overall, survival analysis encompasses many techniques and methods to achieve different subordinate objectives, including tools for exploratory data analysis, distribution fitting and methods for designing experiments. Hence, I don't think you can meaningfully say 'build a model using survival analysis' rather than, perhaps 'build a model using Weibull regression/ Cox regression', as examples of tools closely associated with survival analysis.
Logistic regression, on the other, is a regression technique for analysing binary data. Hence, it is a single tool (but still a very powerful tool useful in many contexts) rather than an overall category of analysis. It could possibly be described as one of the central tools of categorical data analysis, where categorical data analysis belongs to the same category as survival analysis.
Comment: Disagree they are fundamentally different things. One thing you get from survival analysis is the hazard rate, and from the hazard rate you can get the cumulative hazard, and from the cumulative hazard you can get the cumulative incidence function, and every point along that function is basically what you would get out of a logistic regression that estimates the chance the event will happen within that amount of time.
Answer: They have different dependent variables (for logistic 1/0 and for survival time to event).
So the short answer is no - you cannot compare the coefficients.


numpy ndarray inside array using index

Question: I have a numpy.ndarray - a
and a[0] gives me:
array([0., 0., 0., ..., 0., 0., 0.])
also a[0:1] gives:
array([[0., 0., 0., ..., 0., 0., 0.]])
How can I make a[0] to the format array([[0., 0., 0., ..., 0., 0., 0.]]) ie; in the format of array inside an array, like we do [list[0]]
Comment: How about `a[[0]]`?
Comment: what exactly is wrong with `a[0:1]`?
Comment: @Divakar That works.!
Comment: @narn I wish to the result of `a[0]` to be shown in the format how it displays `a[0:1]`. @Divakar 's solution worked.
Comment: But Divakar's snippet doesn't perform `a[0]`. If you were willing to use an expression that isn't `a[0]`, you could have just used `a[0:1]`. (It'd be faster, too, producing a view instead of a copy.)
Answer: In case you want to reshape the array, so that every element of the first axis (every row) is an array of shape (1,m), instead of just (m,) you could do the following:
a = a[:,np.newaxis,:]

This way the (n,m) array is transformed into shape (n,1,m) and getting the first row with a[0] returns:
array([[0., 0., 0., ..., 0., 0., 0.]])

numpy ndarray inside array using index


Convergent sequence of irrational numbers that has a rational limit.

Question: Is it possible to have a convergent sequence whose terms are all irrational but whose limit is rational?
Comment: Have you considered $\frac{\pi}n$
Comment: Yes because irrational numbers are dense in reals.
Comment: Take the sequence of partial sums of any of the series from here http://math.stackexchange.com/questions/1647409/can-an-infinite-sum-of-irrational-numbers-be-rational
Answer: $$\bigg\{a_{n} =  \frac{\sqrt{2}}{n} \bigg\}$$
Answer: I will give a more interesting answer (I think OP wants something like that):
$$\sqrt{2+\sqrt{2+\sqrt{2+\sqrt{2+\cdots}}}}=2$$
Generally
$$\sqrt{a+b\sqrt{a+b\sqrt{a+b\sqrt{a+\cdots}}}}=\frac{1}{2}(b+\sqrt{b^2+4a})$$
It's not hard to find such numbers that $\sqrt{b^2+4a}$ is rational.
Also:
$$\sqrt{2}^{\sqrt{2}^{\sqrt{2}^...}}=2$$
Also, using Euler's continued fraction theorem we can have something like this:
$$1=\cfrac{\pi^2/9}{2-\pi^2/9+\cfrac{2\pi^2/9}{12-\pi^2/9+\cfrac{12\pi^2/9}{30-\pi^2/9+\cfrac{30\pi^2/9}{56-\pi^2/9+\cdots}}}}$$

Actually, I can do even better. Let $\phi$ be the golden ratio, then we have:
$$1=\frac{1}{\phi^2}+\frac{1}{\phi^3}+\frac{1}{\phi^4}+\frac{1}{\phi^5}+\cdots=\sum^{\infty}_{k=2}\frac{1}{\phi^k}$$
But we don't want $e$ to feel left out, so here is another one:
$$1=\cfrac{e}{e+\frac{1}{e}-\cfrac{1}{e+\frac{1}{e}-\cfrac{1}{e+\frac{1}{e}-\cdots}}}$$

Another good one. Using the following:
$$2=e^{\ln 2}=e^{1-\frac{1}{2}+\frac{1}{3}-\frac{1}{4}+\dots}$$
We obtain an infinite product, converging to $2$:
$$\prod_{k=1}^{\infty} \frac{\sqrt[2k-1]{e}}{\sqrt[2k]{e}} =\frac{e \sqrt[3]{e} \sqrt[5]{e} \sqrt[7]{e} \cdots}{\sqrt{e} \sqrt[4]{e} \sqrt[6]{e} \sqrt[8]{e} \cdots}=2$$
Comment: Woah, why is the $\phi$ one true?
Comment: @Sky, use the identity $\phi^{-2}+\phi^{-1}=1$, then multiply it by $\phi^{-1}$ several times to get $\phi^{-3}+\phi^{-2}=\phi^{-1}$, $\phi^{-4}+\phi^{-3}=\phi^{-2}$, etc. and replace the terms in the first indentity. You can also use the closed form of geometric sum to prove this result
Answer: If $q$ is any rational number at all and $n$ is a positive integer then $q+\frac 1 n \sqrt 2$ is irrational (it's a simple algebra exercise to prove that), and $\lim\limits_{n\to\infty}\left(q + \frac 1 n \sqrt 2\right) = q$.
Answer: Here is another example: $$a_n=\frac{1}{n\pi}$$
Answer: Let $q$ be a rational number, $\alpha$ be an irrational number, and $a_n$ be a sequence of integers where $a_n \to \infty$. Then the sequence $$b_n = q + \frac {\alpha} {a_n}$$ satisfies the property you asked.


Image Styles Generate

Question: I am running a Drupal 7 site that does a call to an external service using Feeds Module, creates nodes from that coming JSON.
The Feeds Module grabs an image, download it, and generate 4 different image styles. Usually in Drupal, you upload the image that goes through Imagecache, and boom the image is re-sized, why because after node_save you go to node_view that grabs a function which does the real resizing.
In my case, i do not view the nodes, so the images does not get generated unless i run a whole view/page i created with all images so they get generated.
I tried to curl and request the page on a time interval for the page to get generated but it seems you have to really view the page.  What would a solution for this problem be ?
Would doing a batch operation to just view the nodes using node_view help ? What else ?
Note that on each Feeds request the site creates around 50 nodes X 4 images = 200 image.
Help is much appreciated.
Comment: The resizing is done when the URL is requested for the cached image, not when the node is viewed. Just request the URL for the image and it will be generated by Imagecache
Comment: Yes that is right, i can run a page that has all images and i am good to go. But i am using these images in an external platform, requesting the images is not the best way to do it. I mean i cannot run the page every 1 hour to get my images. Any other solution for requesting images ?
Comment: I think you're missing the point of the image caching functionality in Drupal...you request the **URL for the image file** directly, not the node page. It's the request for the **image URL** that causes Drupal to either serve an already created image from cache, or create a new one if none exists. Viewing a node page causes this as a side effect because your browser requests the image URL to display it in your page. Viewing the node page has absolutely nothing to do with the images being generated other than what I've already mentioned. Hope that clears things up
Answer: A derivative image is generated on the fly when served over HTTP. When the derivative image is requested in HTTP, if the file doesn't exists, the HTTP request is handled by Drupal. Drupal will use the image_style_deliver() page callback to generate the derivative image and deliver it. Once the image exists, Drupal will not handle the request and the file is directly delivered.
If you need to access to files externally without using HTTP to access them, you can use image_style_create_derivative($style, $source, $destination) to create the derivatives images. You will need to provide the destination path for the derivative image, which you can get with the image_style_path($style_name, $uri) function.
The following (untested) code should ensure that the derivatives for the image file identified by $fid are generated for the styles whose names are in the $styles array.
$original = file_load($fid);
foreach ($styles as $style_name) {
  $derivative_uri = image_style_path($style_name, $original->uri);
  image_style_create_derivative($style_name, $original->uri, $derivative_uri);
}

Comment: Yes, when a style or its effects are edited, image_style_flush will be called to delete all the derivative files. After file deletion, hook_image_style_flush implementations are invoked and may be used to re-generate the derivative files if needed. Since this would be a long operations, I suggest using the Queue API to delay the operations and split it in smaller chunks of work.
Comment: That is a great explanation, thank you!
So what will happen if you change an image style, say change the width from 100 to 80 pixels for the "thumbnail style"? Will Drupal delete all the images in the "thumbnail" directory?

Image Styles Generate


Velocity field for uniform rotation

Question: Let be $${\bf F}=-y\hat{\bf i}+x\hat{\bf j}$$
In this video how did the professor figure out above field has angular velocity of $1$?
I understand $(-y,x)$ rotates the entire plane counter clockwise by $90$ degrees. So this vector field seems to rotate in counter clock wise direction at uniform speed. But how to work the actual angular velocity?
Answer: The vector field corresponds to the system of differential equations
$$
dx/dt = -y
,\qquad
dy/dt = x
,
$$
which has the general solution
$$
x(t) = A \cos(t-\varphi_0),
\qquad
y(t) = A \sin(t-\varphi_0)
.
$$
From these formulas it follows that every solution curve (except the equilibrium point at the origin itself) is a circle centered at the origin, and that the period to go around such a circle is $2\pi$ (regardless of the radius).
You can also see this by expressing the differential equations in polar coordinates $x=r \cos \varphi$, $y=r \sin \varphi$,
which after some computation gives
$$
dr/dt = 0
,\qquad
d\varphi/dt = 1
.
$$

Velocity field for uniform rotation


Complex rates of change?

Question: For the function $f(x)=e^{ix}$ defined over $\mathbb{C}$, if its derivative is $ie^{ix}$ does that mean rates of change at various points are complex numbers? And what does that suggest about the nature of the function?
This was just a thought I came up with. It doesn't make much sense to think about directly but I was thinking it might mean something. Like for example the rate of change at $0 \implies i$.
Answer: Yes it does.  It goes back to the definition of a derivative.
$$f'(z)=\lim_{h \to 0}\frac {f(z+h)-f(z)}h$$
When you work in the complex plane $h$ is allowed to be complex.  It still means that if you change the input by $h$ the function value changes by $hf'(z)$.  In your example, if $f(z)=e^{iz}, f'(z)=ie^{iz}$ we have $f(0)=1, f(h)=e^{ih}$.  For $h$ small, $e^{ih}\approx 1+ih$ so the change in the function value, $ih$ is $i$ times the change in input, $h$.
Comment: Thanks. That makes sense
Answer: Rather than "rate of change" think of it as "velocity".  In two dimensions, velocity is a vector.  Here, that vector is considered to be a complex number.

Complex rates of change?


How to insert a large record into Orient DB using pyOrient?

Question: I have an orientDB database with a vertex type 'node'.
The node vertex has a string property called 'data'.
I am trying to insert roughly 5 to 10mb into the node's data property, however, the database seems to become non responsive when doing so.
I am using pyOrient as a driver, and the data is a pandas data frame which I have serialised to message pack format.
Is there a way to insert properties with large properties? Is the limitation at the database end, at the pyOrient end, or something else altogether?
Answer: Per the Binary Data page in the Orient DB docs, it is possible to save the binary as a file in the file system, and then simply keep a reference to the file within the database. This has worked relatively well for me.


Exercise 1.1.4, Weibel

Question: I'm new to homological algebra, and I'm trying to learn it using Weibel's An introduction to homological algebra. I'm currently having trouble with exercise 1.1.4:

Show that $\{\operatorname{Hom}_R(A, C_n)\}$ forms a chain complex of abelian groups for every $R$-module $A$ and every $R$-module chain complex $C$. Taking $A=Z_n$ show that if $H_n(\operatorname{Hom}_R(Z_n,C))=0$, then $H_n(C)=0$. Is the converse true?

I think the first part is OK, using that if the chain complex looks like:
$$C:~\ldots\rightarrow C_n\overset{d_n}{\rightarrow}C_{n-1}\rightarrow\ldots$$
Then we get a new chain complex:
$$C^*:~\ldots\rightarrow\operatorname{Hom}(A,C_n)\overset{d^*_n}{\rightarrow}\operatorname{Hom}(A,C_{n-1})\rightarrow\ldots$$
Where $d^*(f_n)=d_n\circ f_n$ for a $f_n\in\operatorname{Hom}(A,C_n)$. This is a chain complex since $d^*_{n-1}\circ d^*_n(f_n)=(d_{n-1}\circ d_n)(f_n)=0$.
I'm not so sure about the second part. Assuming $Z_n=\operatorname{ker}(d_n)$, and $\frac{\operatorname{ker}(d^*_n)}{\operatorname{im}(d^*_{n+1})}=0$.
I need to show that $\operatorname{ker}(d_n)\subseteq \operatorname{im}(d_{n+1})$. Let $z\in\ker(d_n)$, then $\operatorname{id_{Z_n}}\in\operatorname{Hom}(Z_n,C_n)$ since $Z_n\subseteq C_n$. $\operatorname{id_{Z_n}}\in\ker(d^*_n)=\operatorname{im}(d^*_{n+1})$, $z=\operatorname{id_{Z_n}}(z)=d_{n+1}\circ f_{n+1}(z)$ for some $f_{n+1}\in \operatorname{Hom}(Z_n,C_n)$, so $$\ker(d_n)\subseteq\operatorname{im}(d_{n+1})$$ So $H_n(C)=0$. Is this reasoning correct?
I'm also having trouble with the converse:

$\operatorname{im}(d^*_{n+1})\subseteq\ker(d^*_n)$ is OK
Let $f_n\in\ker(d^*_n)$, then $d_n\circ f_n(z)=0$ and so $f_n(z)\in\ker(d_n)=\operatorname{im}(d_{n+1})$. Therefore $f_n(z)=d_{n+1}(y)$ for some $y\in C_{n+1}$. I think I'm done if I can find a morphism $$g:Z_n\rightarrow C_{n+1}$$ such that $g(z)=y$, but I'm having trouble arguing when this is possible (if it is).

Any help is much appreciated!
(Edit: $\operatorname{id}_{Z_n}\in\operatorname{ker}(d^*_n)$ is an arrow, and not an element)

Are there any criteria for when the diagram above commutes? For instance, by hypothesis $Z_n\cong B_n=\operatorname{im}(d_{n+1})$, so do I need $g$ to be surjective which would mean $g$ is the inverse of $d_{n+1}$?
Comment: Notice that $id_{Z_n}(z)$ is an element of $C_{n}$, not of $ker(d_n^*)$ (the latter is a set of arrows, not of elements). The hypothesis $H_n(Hom(Z_n,C_n))=0$ actually tells you that every arrow $f:Z_n\rightarrow C_n$ such that $d_n f=0$ can be seen as a composition $d_{n+1}g$ for some other arrow $g:Z_n\rightarrow C_{n+1}$: now you only need to take the right $f$ and you're done
Comment: @TheMadcapLaughs in fact, the first object is fixed.
Comment: Oh wow, I've been trying to solve this exercise for over 30 minutes now, and I couldn't because I didn't think and hence thought $Z_n = \mathbb{Z}/(n\mathbb{Z})$  lmao
Comment: @JoBe I thought the same but immediately checked the notation in exercise 1.1.1 which uses $\mathbb Z$.
Comment: For $A = \mathbb{Z}$ converse will hold as Hom$(\mathbb{Z}, C_n)$ is same as $C_n$ in that case.
Answer: Elements of $\ker(d_n^*)$ are arrows. Let $i_n:Z_n\rightarrow C_n$ be the canonical inclusion. Then $i_n\in Hom(Z_n,C_n)$ and
$$d^*_n(i_n)=d_n\circ i_n=0.$$
Hence there exists $u\in Hom(Z_n,C_{n+1})$ such that $i_n=d_{n+1}\circ u$ and therefore
$$Z_n\subseteq B_n=im(d_{n+1})$$
Note that this argument is valid to any exact category.
The reciproque is false: consider the complex $0\rightarrow 2\mathbb{Z}\rightarrow\mathbb{Z}\rightarrow\mathbb{Z}_2\rightarrow0$ and take $Z_n=\mathbb{Z}_2$.
Comment: Thank you for the counterexample! So it is not true because I would get a new complex: $0\rightarrow 0\rightarrow 0\rightarrow \mathbb{Z}_2\rightarrow 0$, and $\frac{\operatorname{ker}(\mathbb{Z_2}\rightarrow 0)}{\operatorname{im}(0\rightarrow\mathbb{Z}_2)}\cong\mathbb{Z}_2\neq 0$?

I was also looking for when the converse holds (I've updated the question).
Comment: @cansomeonehelpmeout, $H_n(C_\bullet) = 0 \ \Rightarrow  \ H_n(\operatorname{Hom}(Z_n,C_\bullet)) = 0 $ implies, that any endomorphism $f : Z_n \to Z_n$ factorizes through $C_{n+1}$, i.e. there is a map $g : Z_n \to C_{n+1}$ such that $d_{n+1} \circ g =f$. In particular, $d_{n+1}$ has a right inverse (this is a necessry condition). Having $H_n(C_\bullet) = 0$ you can always construct a (set) function $g : Z_n \to C_{n+1}$ such that $d_{n+1} \circ g =f$. Simply let $g(x) = y$, where $d_{n+1} (y) = f(x)$. Problems start when you want to show that $g$ is an $R$-module.

Exercise 1.1.4, Weibel


How to set OverflowStrategy in Spring Webflux?

Question: Is it possible to set an overflow strategy in Webflux, like one would do for any other Sink? See https://projectreactor.io/docs/core/release/api/reactor/core/publisher/FluxSink.OverflowStrategy.html
The default behaviour appears to be:

FluxSink.OverflowStrategy BUFFER Buffer all
  signals if the downstream can't keep up. Warning! This does unbounded
  buffering and may lead to OutOfMemoryError.

I was looking for a way to change this to DROP or ERROR: (see https://projectreactor.io/docs/core/release/api/reactor/core/publisher/FluxSink.OverflowStrategy.html)
Comment: I guess you need this operator https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#onBackpressureBuffer-int-reactor.core.publisher.BufferOverflowStrategy-
Comment: WebFlow? dont really understand what you mean
Comment: typo sorry, WebFlux
Comment: @YauhenBalykin that could be applied to the service calls inside the controller method. I was wondering if there's a way to set the behaviour at container level?
Comment: How are you creating the sink, the default, for example, is `ignore` when using a flux processor.
Comment: did you achieve the setting at container level? how did you do it?
Answer: If you are using Flux.create,  to emit values, we can pass FluxSink.OverflowStrategy.DROP as an additional parameter.
If you use EmitterProcessor /Any processor, the sink method accepts the OverflowStrategy.
    EmitterProcessor<Object> emitterProcessor = EmitterProcessor.create();
    FluxSink<Object> sink = emitterProcessor.sink(FluxSink.OverflowStrategy.DROP);

    // you should use sink to publish values
    sink.next(obj);

Comment: I'm talking of webflux, how to setup the strategy in the container


aws-secretsmanager-caching-python :: Is cache shared among different docker containers running on the same EC2 instance?

Question: I bumped to this interesting feature of AWS Secret caching:
https://docs.aws.amazon.com/secretsmanager/latest/userguide/use-client-side-caching.html#use-client-side-caching-components
https://github.com/aws/aws-secretsmanager-caching-python
https://aws.amazon.com/blogs/security/improve-availability-and-latency-of-applications-by-using-aws-secret-managers-python-client-side-caching-library/
I have a question though:
I am running several different docker containers on the same AWS EC2 instance retrieving AWS Secrets. After implementing aws-secretsmanager-caching-python will containers share the same cache or each container will have it's own cache?
Thank you
Answer: containers are independent of the eachother unless there is some common space share among them to be used as cache.


É permitido a um compilador omitir um membro referência em uma classe?

Question: Considere a seguinte classe:
struct Type {
    int a, b, c;
    int& ref;
    Type(int m) : a(m), b(m), c(m), ref(a) { }
    Type(const Type& ot) : a(ot.a), b(ot.b), c(ot.c), ref(a) { }
} obj;

Aqui temos que o sizeof(Type) é 24. No entanto obj.ref pode sempre e em qualquer situação/contexto ser substituído por obj.a, fazendo com que a referência possa ser resolvida em tempo de compilação e tornando inútil guardar os 8 bytes da referência no objeto (e os 4 de padding). Idealmente o sizeof(Type) pode ser 12 (apenas os três ints).
Um compilador pode executar essa otimização enquanto que seguindo estritamente as regras do standard? Por que? Existe alguma situação em que essa otimização seria incorreta?
Demonstre com um exemplo que produza comportamento diferente com/sem a otimização.
Comment: Acredito que esse tipo de otimização não é permitida, pois mexe no layout da struct/classe. Mas tenho que pesquisar pra dar uma resposta com certeza.
Comment: Penso o mesmo. **Mas**, não consigo achar nenhum caso em que isso vire um problema ou uma incompatibilidade. Nota: Nenhum dos compiladores que testei fazem isso (obviamente).
Answer: Em primeiro lugar, o standard afirma o seguinte na sessão 8.3.2, paragrafo 4:

It is unspecified whether or not a reference requires storage.

Então um compilador é livre para omitir a alocação de uma referência sempre que isso não alterar o comportamento do programa. O tamanho do objeto pode ser 12.
No caso da pergunta, a otimização será válida se e somente se o compilador puder provar que obj.ref sempre resulta em obj.a, ou seja, qualquer construção de uma referência em obj.ref será feita de forma que esta "aponte" para obj.a.
Referências recebem um objeto na sua inicialização e não podem ser mudadas até a destruição. Se uma referência é membro, então tem sua vida igual a vida do objeto que a contém, logo o único lugar onde é legal inicializar uma referência é na inicialização do objeto, isso ocorre na lista de inicialização dos construtores.
Se o compilador pode ver todos os construtores na definição da classe e se todos eles inicializam a referência da mesma forma (para um membro do próprio objeto), então não existe como a referência em qualquer momento referenciar outro objeto. Logo a otimização é válida para o exemplo da pergunta.
Comment: Entretanto os compiladores não a implementam. Ou ela não é realmente válida, ou é muito custoso fazer a checagem de sua validade para valer a pena.
Comment: Ou criar uma referência dentro da classe para um membro dela mesma é algo que ninguém são faria e eles não tem por que implementar a otimização.
Answer: O seu caso já é era uma situação problemática:
#include <iostream>

struct Type {
    int a, b, c;
    int& ref;
    Type(int m) : a(m), b(m), c(m), ref(a) { }
};

int main() {
    Type a(42);
    Type b(a);

    std::cout << &a.ref << "  " << &b.ref << std::endl;
    std::cout << &a.a << "  " << &b.a << std::endl;
}

0xbfb1efa0  0xbfb1efa0
0xbfb1efa0  0xbfb1efb0

http://ideone.com/e65KKt
Como o compilador pode com inteligência suficiente se garantir que tal otimização faria com que o comportamento não mudasse (entre a implementação de referência via ponteiros ou por sua omissão) para este exemplo e outros casos possíveis?
Mais info sobre possíveis otimizações no uso de referências em: http://www.preney.ca/paul/archives/1051
Comment: Estava faltando um bom construtor de cópia. Assumindo que o compilador veja que ele existe (veja questão atualizada), o seu exemplo não é mais válido. Ainda assim, boa tentativa
Comment: @GuilhermeBernal meu argumento continuaria sendo o mesmo, foi só um exemplo baseado no seu código original. Você está requisitando que o compilador realize uma prova, para que a otimização possa então ser realizada.
Comment: E ainda há como fazer a referência apontar para outro lugar com esse meu construtor de cópia? Se não, então um compilador poderia provar a otimização válida.
Comment: @GuilhermeBernal não sei, ainda não parei pra pensar, mas outra coisa também seria verificar o padrão a respeito das regras de layout/etc para membros e ver se tem algo que elimine a necessidade dessa linha de raciocínio.
Answer: Uma subclasse de Type pode sobrescrever o construtor e inicializar ref de forma diferente. Isso invalidaria essa otimização.
Comment: sobrescrever um construtor? o que vejo possível como situação problema no caso dele é fazer uso de outros construtores implícitos, porém não é possível sobrescrever construtores base.
Comment: É verdade! Não é permitido inicializar membros de superclasses no construtor. `SubType(int m) : Type(m), ref(b) {}` não é permitido.
Comment: @C.E.Gesser: Mesmo se esses membros forem públicos, como no caso do exemplo do Guilherme?
Comment: @missingno é possível apenas a reinicialização de membros da base nos construtores derivados, ou seja, não funciona na lista de inicialização do construtor da classe derivada, somente no corpo, depois que já ocorreu a inicialização via construtor da base.
Answer: Eu vejo problema se duas unidades de compilação diferentes (dois arquivos .cpp) usassem essa struct (via definição em um arquivo .h, por exemplo), e em uma delas a compilação fosse com otimização e na outra sem otimizações. Seria impossível linkar os arquivos objeto gerados pelas duas unidades depois.
Desta forma, algo assim deveria ser uma otimização proibida, ou sempre obrigatória, já que se fosse opcional (e configurável, como outras otimizações) tornaria impossível o modelo de compilação separada da linkagem que vigora na linguagem.
Comment: Como a definição estaria no `.hpp`, o compilador poderia detectar ela em todos os arquivos. Sobre a ABI, ela sempre foi não *standarlizada* e conectar arquivos compilador com compiladores diferentes, ou versões diferentes, ou mesmo flags diferentes já pode dar problema. Então a meu ver nenhum dessas questões seria realmente um impessilho.


Is there any restriction on the movement of a runner for an injured striker before he plays the ball?

Question: What does umpire do in this situation, especially when wicket keeper is further back for fast bowler and runner for an injured striker is further forward then injured striker before playing the ball. Because it gives clear advantage to the runner.

There is currently no restriction in the Laws on the movement of a runner for an injured striker.

So what happens in this situation?
Answer: Before the change of law procedures of Law 42.18 Players' conduct would be followed in this situation.
---------------------------------------------------------------------------------------------------
This case is now covered by the new code of laws NEW CODE OF LAWS - OCTOBER 2017, thanks to @gout for looking into this and reminding me in comment.
Law 25.7 Restriction on the striker's runner states:

25.7.1 Once the ball comes into play, the runner for an injured batsman who is the striker
  shall have part of his/her person or bat grounded behind the popping crease until the
  ball reaches the striker or passes the popping crease, whichever is the sooner.
25.7.2 If the striker's end umpire considers the runner for an injured striker to be in breach
  of this restriction then, if the ball does not become dead for any other reason, he shall
  call and signal Dead ball as soon as the ball reaches the boundary or at the
  completion of the first run. However, he/she shall delay the call of Dead ball to allow
  the opportunity for a catch to be completed.

This law will now restrict the movement of runner for injured striker before he strikes the ball.
This new set of laws with few other that have been changed will take effect around the world from 1 October 2017.


How to compare two bit values in C?

Question: I've been dabbling around a bit with C and I find that being able to directly manipulate bits is fascinating and powerful (and dangerous I suppose). I was curious as to what the best way would be to compare different bits in C would be. For instance, the number 15 is represented in binary as:
00001111

And the number 13 is represented as:
00001101

How would you compare what bits are different without counting them? It would be easy to use shifts to determine that 15 contains 4 1s and 13 contains 3 1s, but how would you output the difference between the two (ex that the 2^1 spot is different between the two)? I just can't think of an easy way to do this. Any pointers would be much appreciated!
EDIT: I should have clarified that I know XOR is the right way to go about this problem, but I had an issue with implementation. I guess my issue was comparing one bit at a time (and not generating the difference per say). The solution I came up with is:
 void compare(int vector1, int vector2) {
     int count = 0;
     unsigned int xor = vector1 ^ vector2;

     while (count < bit_length) {
          if (xor % 2 == 1) { //would indicicate a difference
               printf("%d ", count);
          }
          xor >>= 1;
          count++;
      }
 }

Comment: http://stackoverflow.com/questions/1746613/bitwise-operation-and-usage/1746642#1746642
Comment: There's nothing magical or dangerous about manipulating bits -- they're just individual digits of a number.
Answer: Use bitwise operations:
c         = a         ^ b        ;
00000010b = 00001111b ^ 00001101b;

What ^, or XOR, does is:
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0

One way of thinking about it would be:

If the two operands (a and b) are different, the result is 1.
  If they are equal, the result is 0.

How to compare two bit values in C?


What are all the existing clefs?

Question: I am asking this question out of sheer curiosity. What are all the existing clefs? What is the clef's most used range?
Comment: Have you checked https://en.wikipedia.org/wiki/Clef? What extra information do you need exactly?
Comment: Why? Do you want it to be more difficult to play?
Comment: Don't do it.  There's not a musician in the world who will say "Cool! I always wanted to play in a new clef just for the fun of it!"
Answer: When you ask about "existing" clefs... well, they all still exist.  We may not be writing modern music that uses them, but it's fairly easy to find a 200 year old score that uses a now obsolete clef.  So these are the ones I know of - the ranges are the actual range of the lines, as any staff can be extended by ledger lines
G clefs
G (treble) clef: places G4 on the second staff line.  Range: E4-F5
G ottava clef: places G5 on the second staff line.  Range: E5-F6.  Originally written with '8' at the top of the clef - now that's assumed for piccolo.
G ottava bassa clef (also called tenor clef): places G3 on the second staff line.  Range: E3-F4.  Originally written with '8' at the bottom of the clef - now that's assumed for guitar
French violin clef: places G4 on the first staff line.  Range: G4-A5
F clefs
F (bass) clef: places F3 on the fourth staff line.  Range: G2-A3
F ottava bassa clef: places F2 on the fourth staff line.  Range: G1-A2.  Originally written with '8' at the bottom of the clef - now that's assumed for double bass
C clefs
Soprano clef: places C4 on the first staff line.  Range: C4-D5
Mezzo-soprano clef: places C4 on the second staff line.  Range: A3-B4
Alto clef: places C4 on the third staff line.  Range: F3-G4
Tenor clef: places C4 on the fourth staff line.  Range; D3-E4  (this is the more common tenor clef - the G clef version is now only used for guitar)
Baritone clef: places C4 on the fifth staff line.  Range: B2-C4
Comment: Heck, the OP might as well design a clef with 11 ledger lines, of which some are whole tone, some are half tone, and some are quarter tone.
Comment: Sure - or use a clefless system like Klavarscribo (https://en.wikipedia.org/wiki/Klavarskribo) or Equiton (http://musicnotation.org/wiki/notation-systems/equiton-music-notation-by-rodney-fawcett/).  Or even a staffless system like Parsons code (https://en.wikipedia.org/wiki/Parsons_code)
Answer: There are many clefs that you can use. There are clefs that are commonly used, like treble clef and bass clef, and others that are not so commonly used.
Here is a website for more detail on clefs and their ranges.
Clefs that you could use are:

Treble Clef (G Clef)
Bass Clef (F Clef)
Alto Clef
Tenor Clef
Neutral Clef (Percussion Clef)
Guitar TAB (Short for Tablature)

Of course, these clefs are only some of many others that you could use. Hope this helped.
Comment: @AduyummY Then the ranges should also be written out in the answer itself.
Comment: You did not specify their ranges..
Comment: @Xilpex The ranges are on the website, as well as the instruments that use the clefs.
Comment: @Xilpex the ranges are irrelevant - have you not heard of extended lines above/below the clef, or of terms like  "8va" ?

What are all the existing clefs?


Is any map $f:M\to N$ smooth?

Question: Let $M$ be a 0-dimensional smooth manifold and $N$ a smooth manifold with or without boundary, is any map $f:M\to N$ smooth?
Answer: Yes. A map $f:M\to N$ is smooth if and only if for all $p\in M$ there are smooth charts $(U,\varphi)$ containing $p$ and $(V,\psi)$ containing $f(p)$ such that $f(U)\subseteq V$ and the composition $\psi\circ f\circ\varphi^{-1}:\varphi(U)\to\psi(V)$ is smooth. In case $M$ is $0$-dimensional and $N$ is $n$-dimensional, we can take $U=\{p\}$, $\varphi:\{p\}\to \{0\}=\mathbb{R}^0:p\mapsto 0$, and any chart $(V,\psi)$ containing $f(p)$. This satisfies the requirement since the map $\psi\circ f\circ\varphi^{-1}:\{0\}=\mathbb{R}^0\to\psi(V)\subseteq\mathbb{R}^n$ is constant and hence smooth.
Answer: This is basically asking if every point of $N$ has a smooth coordinate neighborhood, which is true since $N$ is smooth.
Comment: I would agree that $f$ must be continuous, since any map out of a discrete space is continuous. Is it conventional to consider maps out of discrete spaces smooth as well?
Comment: Only if the target space is smooth. Consider the map $f$ that maps your point to a non-smooth point of $N$.

Is any map $f:M\to N$ smooth?


Free Response Question About Product Sales Function (Integration)

Question: In a workbook for our AP Calculus class, I found this question:
The Sales of a new product, after it has been on the market for $t$ years, is given by
$S(t)=Ce^{k/t}$
a) Find C and k if 7000 units are sold after one year and $\lim _{t\to\infty }S(t)=45,000$
b) Find the total number of units sold during the year t=5 and t=10.
Part (a) was easy to do when you apply the limits and set 7000 equal to S(t); the values of C and k are 45,000 and $ln(7/45)$ respectively;
Part b is the portion I am unable to answer. It tried plugging in those values and then adding them together but that is incorrect; also, my teacher said it had to do with integration but integrating the S(t) does not give you the units sold. It gives Unit multiplied by time when you find the accumulated value under the curve of S(t). I have not been able to figure it out. How would I go about figuring this out using some form of integration?
Answer: $S(x)$ is the total mumber of sold units  until $t=x$. Therefore the total number of sold units between $t=a$ and $t=b$ is $S(b)-S(a)$. Thus the total number of sold units between $t=5$ and $t=10$ is
$$S(10)-S(5)=45,000\cdot \left( \frac7{45} \right)^{\frac1{10}}-45,000\cdot \left( \frac7{45} \right)^{\frac1{5}}$$
Remark
$e^{\ln (7/45)\cdot 1/t}$ can be transformed to $$\left(e^{\ln (7/45)}\right)^{\frac1t}\Rightarrow \left( \frac7{45} \right)^{\frac1{t}}$$


Show that $|A+A|\geq (2n-1)$

Question:
Consider a set $A$ consisting of $n$ natural numbers $\{a_i\}_{i=1}^n$ such that $a_1<a_2 < \cdots <a_{n-1} < a_n$. Define the set $A+A$ such that it contains $a_i + a_j \ ; \ i \leq j$ as its elements.
Prove That $|A+A| \geq (2n-1)$. When does the equality hold?

I tried to list out the elements for general $n$, but it became too tedious.
Any help will be appreciated.
Thanks.
Comment: If my memory serves me right, the equality holds iff $A$ is an arithmetic progression.  This result probably generalizes for any arbitrary field, not just $\mathbb{R}$, where you have an ordering compatible with addition, which is required in Michael Burr's hint.
Comment: @Batominovski there is no actual multiplication in this result. A convenient general context are torsion-free abelian groups (which are precisely those abelain groups that admit a total order).
Comment: @Batominovski consider it as subset of its divisible hull, which is  $\mathbb{Q}^{(I)}$ for some cardinal, and order that, say, lexicographically. [Disclaimer: this works in "usual" foundations, I am not sure if there is some issue if you do not have "choice" or some such thing.]
Comment: I meant "any arbitrary field of characteristic zero," as this claim is most likely false for nonprime finite fields, even when $A$ is a small set.
Comment: @Batominovski I gauge that you know quite a lot about this problem. Can you please post a solution explaining the details?
Comment: @SubhadeepDey What is this CMI? I heard that for my last question as well. I've got this in a worksheet given to me by my teacher.
Comment: @SubhadeepDey My teacher gave me a "worksheet" not a book. I'll have to ask him where did he get the problem from.
Comment: @quid  How do you prove that every torsion-free abelian group admits a total order compatible with the addition?  (Although to prove your version, it does not require to deal with the order for the whole group, just that of a finitely generated subgroup, which is free of finite rank, whence equipped with such an order.)

By the way, the result can even be more generalized than that, if I remember correctly.  If $(G,\cdot)$ is a group such that every non-identity element does not have a finite order, then $|A\cdot B|\geq |A|+|B|-1$ for any nonempty finite subsets $A$ and $B$ of $G$.
Answer: Hint: Why are
$$
a_n+a_n,a_n+a_{n+1},\cdots,a_n+a_2,a_n+a_1,a_{n-1}+a_1,\cdots,a_2+a_1,a_1+a_1
$$
all distinct?
To see that this is the minimum, suppose that $A$ is an arithmetic sequence:
$$
A=\{a_1,a_1+r,a_1+2r,\cdots,a_1+(n-1)r\}.
$$
Then the list above is $\{2a_1,2a_1+r,\cdots,2a_1+2(n-1)r\}$.  Can you prove that every $a_i+a_j$ is in this list?
Comment: They are distinct, but can you show that this is the minimum?
Comment: Showing that $2n-1$ is the minimum is not the same as answering the question "When does the equality hold?"
Comment: @Batominovski Can you provide a complete solution? Thanks!
Comment: Not right now.  Typing this on an iPad is too much work.  But it is not too difficult.  Maybe, somebody will post the solution before I have access to better computers.  My first hint is: what should the value of $a_2+a_2$ be if the minimum is achieved?
Answer: We assume that $A$ and $B$ are finite nonempty subsets of an integral domain $D$ of characteristic $0$, whose field of fractions is $F$.  We claim that $|A+B|\geq |A|+|B|-1$, and that the equality holds iff $A$ and $B$ are arithmetic progressions with the same common difference.  (If $A$ and $B$ are arithmetic progressions with the same common difference, then it is easily seen that the equality occurs.)
First, note that $\mathbb{Q}$ is a subfield of $F$, and the smallest subfield $K$ of $F$ containing $A$ and $B$ is a field extension of $\mathbb{Q}$ with finite transcendental degree.  Hence, $K$ can be embedded into $\mathbb{C}$.  Therefore, it suffices to prove the claim when $D=F=\mathbb{C}$.
Now, we equip $\mathbb{C}$ with the total ordering $<$ by demanding that, for $x,x',y,y'\in\mathbb{R}$, $x+\text{i}y<x'+\text{i}y'$ iff (1) $x<x'$ or (2) $x=x'$ and $y<y'$.  This ordering is compatible with addition, namely, for $z_1,z_2,z\in\mathbb{C}$, $z_1<z_2$ implies $z_1+z<z_2+z$.
Write $A=\left\{a_1,a_2,\ldots,a_m\right\}$ and $B=\left\{b_1,b_2,\ldots,b_n\right\}$ such that $a_1<a_2<\ldots<a_m$ and $b_1<b_2<\ldots<b_n$ (making $m=|A|$ and $n=|B|$).  Without loss of generality, assume that $m\leq n$.  Then, $$a_1+b_1<a_1+b_2<\ldots<a_1+b_n<a_2+b_n<\ldots<a_m+b_n\,.\tag{*}$$
This proves that $A+B$ has at least $m+n-1=|A|+|B|-1$ elements.
Suppose that the equality $|A+B|=|A|+|B|-1$ holds.  Note that
$$a_1+b_1<a_2+b_1<\ldots<a_m+b_1<a_m+b_2<\ldots<a_m+b_n\tag{**}$$
also contains $m+n-1$ elements of $A+B$.  We conclude that $a_1+b_i=a_i+b_1$ for all $i=1,2,\ldots,m$.  Thus, $b_i=a_i+t$, where $t:=b_1-a_1$ for $i=1,2,\ldots,m$. Since $$a_1+b_1<a_1+b_2<a_2+b_2<a_3+b_2<\ldots<a_m+b_2<a_m+b_3<\ldots<a_m+b_n$$
also contains $2+(m-1)+(n-2)=m+n-1$ elements of $A+B$, we have that $a_2+b_2=a_1+b_3$, $a_3+b_2=a_1+b_4$, $\ldots$, $a_{m-1}+b_2=a_1+b_m$.  Thus, $$a_2-a_1=b_3-b_2=(a_3+t)-(a_2+t)=a_3-a_2\,,$$
$$a_3-a_1=b_4-b_2=(a_4+t)-(a_2+t)=a_4-a_2\,,$$
$$\ldots\,,$$
$$a_{m-1}-a_1=b_m-b_2=(a_m+t)-(a_2+t)=a_m-a_2\,.$$
This proves that $a_2-a_1=a_3-a_2=\ldots=a_m-a_{m-1}=:d$.  That is, $A$ is an arithmetic progression with common difference $d$.
From the relationship between $a_i$ and $b_i$ with $i=1,2,\ldots,m$, we conclude that $\left\{b_1,b_2,\ldots,b_m\right\}$ is a subset of $B$ which is an arithmetic progression with common difference $d$.  Now, from (*) and (**), $a_1+b_{m+1}=a_m+b_2$, $a_1+b_{m+2}=a_m+b_3$, $\ldots$, $a_1+b_n=a_m+b_{n+1-m}$.  This implies that $$b_{m+1}=b_2+(a_m-a_1)=b_m+d\,,$$ $$b_{m+2}=b_3+(a_m-a_1)=b_{m+1}+d\,,$$ $$\ldots\,,$$ $$b_n=b_{n+1-m}-(a_m-a_1)=b_{n-1}+d\,.$$  Thus, $B$ is an arithmetic progression with common difference $d$ as well.
Note that, if $D$ is the field $\mathbb{F}_p$ for a prime natural number $p$, then we have a similar inequality, known as the Cauchy-Davenport Inequality: $|A+B|\geq \min\big\{p,|A|+|B|-1\big\}$.  This last inequality is also true in general for an integral domain $D$ of characteristic $p$.
Answer: if $A=\{ 1,...,n\}$, then $A+A=\{2,...,2n\}$. A simple computation gives that $|A|=n$ and $|A+A|=2n-1$.
Following the hint of Michael Burr, $a_n+A\cap a_1+A=\{ a_1+a_n \}$. Therefore, using the classical formula $card(A\cup B)=card(A)+card(B)-card(A\cap B)$, we have that $card(A+A)\geqslant card(\{ a_1,a_n\} + A)=card(a_1+A\cup a_n+A) = 2n-1$.
Comment: Can you please explain the notation used in the first line?
Comment: Do you mean $card$ ? It is the number of element in a set. With your notations, $card(A)$ is $|A|$. $card$ might be a french notation. Also, $[1;n]\cap \mathbb{N}$ is equal to $\{1,...,n\}$.
Comment: I know $card$, what do you mean by "if $A=[1;n]\cap \mathbb{N}$, then $A+A=[2;2n]\cap\mathbb{N}$" ?
Comment: I am using your notations here. $A+A=\{ a_i+a_j;1 \leqslant i,j\leqslant n\}=\{ i+j, 1\leqslant i,j\leqslant n\}$
Comment: Note that this proves that $2n-1$ is the minimum.
Comment: Can you edit the first line so that it is simple to read (using my notation)? Thanks.
Comment: You have just computed the cardinality for a special case when the elements are consecutive integers.

Show that $|A+A|\geq (2n-1)$


FirebaseAuth parou de autenticar o usuário

Question: No aplicativo, antes do usuário entrar, ele precisar logar com email e senha. Caso não tenha ele pode criar uma conta.
O problema aconteceu ontem a noite, os usuários já cadastrados não conseguem logar mais e não dá nenhum erro. Porém se eu criar uma conta nova, nessa tela eu programei que assim que for feito o cadastro já logar no app.
O problema parece ser a tela de login. No meu lopcat aparece isso
D/FirebaseAuth: Notifying id token listeners about user ( 7zWrS3nX2lW815oOMzwoxyFRbzc2 ).
D/FirebaseApp: Notifying auth state listeners.
D/FirebaseApp: Notified 0 auth state listeners.

Se eu colocar senha errada ou email errado ele não está mais verificando também, sendo que dias atrás estava funcionando normalmente.
Minha tela de login
    public class TeladeLoginActivity extends AppCompatActivity {

    private EditText edt_Email;
    private EditText edt_Senha;
    private TextView txt_CriarConta;
    private Button btn_Logar;
    private ProgressBar progressBarLogin;

    ValidaEmail validaEmail;
    private FirebaseAuth autenticacao;
    VerificaInternet verificaInternet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_telade_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().hide();

        edt_Email = (EditText) findViewById(R.id.edt_TelaLogin_Email);
        edt_Senha = (EditText) findViewById(R.id.edt_TelaLogin_Password);
        txt_CriarConta = (TextView) findViewById(R.id.txt_CriarUmaConta);
        btn_Logar = (Button) findViewById(R.id.btn_LogarApp);
        progressBarLogin = (ProgressBar) findViewById(R.id.progressBarLogin);

        verificaInternet = new VerificaInternet(this);
        validaEmail = new ValidaEmail();

        btn_Logar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (edt_Email.getText().toString().isEmpty() ||
                        edt_Senha.getText().toString().isEmpty()) {
                    Toast.makeText(TeladeLoginActivity.this, "Campos não podem ficar vazios",
                            Toast.LENGTH_SHORT).show();

                }else if (!validaEmail.validar(edt_Email.getText().toString())){
                    edt_Senha.setError("Verifique o email digitado");

                }else {

                    String emailUsuario = edt_Email.getText().toString().trim();
                    String senhaUsuario = edt_Senha.getText().toString().trim();
                    criarUser(emailUsuario, senhaUsuario);
                }

            }
        });

        txt_CriarConta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent cadastrarUsuario = new Intent(TeladeLoginActivity.this, CadastrarContaActivity.class);
                startActivity(cadastrarUsuario);

            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        autenticacao = AutenticacaoLogin.getFirebaseAuth();
    }

    private void criarUser(String emailUsuario, final String senhaUsuario) {
       // progressBarLogin.setVisibility(View.VISIBLE);
        autenticacao.signInWithEmailAndPassword(
                emailUsuario, senhaUsuario
        ).addOnCompleteListener(TeladeLoginActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (verificaInternet.existeConexao()){

                }else if (task.isSuccessful()) {

                    progressBarLogin.setVisibility(View.INVISIBLE);
                    Intent abrirApp = new Intent(TeladeLoginActivity.this, MainActivity.class);
                    startActivity(abrirApp);

                } else {
                    progressBarLogin.setVisibility(View.INVISIBLE);
                    String erroExcecao = "";

                    try {
                        throw task.getException();

                    } catch (FirebaseAuthInvalidCredentialsException e) {
                        erroExcecao = "Email inválido";

                    } catch (FirebaseAuthUserCollisionException e) {

                        erroExcecao = "Esse e-mail já está em uso no App.";

                    } catch (Exception e) {

                        if (senhaUsuario.length() < 6) {
                            edt_Senha.setText("");//Limpa o campo de texto para o usuário digitar uma nova senha mais forte
                            edt_Senha.requestFocus();//Seta o focus no campo de texto
                            Toast.makeText(TeladeLoginActivity.this, "Senha incorreta", Toast.LENGTH_LONG).show();
                        }
                        erroExcecao = "Erro ao logar no aplicativo.";
                        e.printStackTrace();
                    }

                    Toast.makeText(TeladeLoginActivity.this, "Erro: " + erroExcecao, Toast.LENGTH_LONG).show();

                }
            }
        });
    }
}

Answer: Notei agora que na activity de cadastrar usuário, não tem um teste de verificação se o dispositivo está conectado na internet. Já na tela de Login tem essa verificação. O que fiz foi remover o mesmo e acrescentar no final do último catch.
if (task.isSuccessful()) {

                progressBarLogin.setVisibility(View.INVISIBLE);
                Intent abrirApp = new Intent(TeladeLoginActivity.this, MainActivity.class);
                startActivity(abrirApp);

            } else {
                progressBarLogin.setVisibility(View.INVISIBLE);
                String erroExcecao = "";

                try {
                    throw task.getException();

                } catch (FirebaseAuthInvalidCredentialsException e) {
                    erroExcecao = "Email ou senha inválidos";

                } catch (FirebaseAuthUserCollisionException e) {

                    erroExcecao = "Esse e-mail já está em uso no App.";

                } catch (FirebaseAuthEmailException e) {
                    erroExcecao = "Erro na autenticacao do email";

                } catch (Exception e) {

                    if (senhaUsuario.length() < 6) {
                        edt_Senha.setText("");//Limpa o campo de texto para o usuário digitar uma nova senha mais forte
                        edt_Senha.requestFocus();//Seta o focus no campo de texto
                        Toast.makeText(TeladeLoginActivity.this, "Senha incorreta", Toast.LENGTH_LONG).show();
                    }
                    erroExcecao = "Erro ao logar no aplicativo. Verifique se está conectado a internet";
                    e.printStackTrace();
                }

                Toast.makeText(TeladeLoginActivity.this, erroExcecao, Toast.LENGTH_LONG).show();

            }
        }
    });
}


Importação de XLS para PgAdmin

Question: Estou tentando importar uma planilha com as informações de uma tabela do banco de dados, porém, quando tento fazer a importação, da a seguinte mensagem:

field 'md5' must have a value

Porém, existe valores neste campo. Não sei mais o que fazer a respeito.
Comment: Vi uma orientação sobre importar arquivo *.csv será que você consegue converter estes dados para *.csv para tentar importar? Segue o link: https://stackoverflow.com/questions/19400173/how-should-i-import-data-from-csv-into-a-postgres-table-using-pgadmin-3
Comment: Consegui thainá. obrigado. ;-)
Comment: Yey! \o/ Vou colocar como resposta, por ser uma solução possível. Sucesso aí!
Answer: Solução paliativa: Transformar o arquivo *.xls em arquivo *.csv e importá-lo.
Você deve criar sua tabela primeiro e, em seguida, você pode importar dados com facilidade, clicando com o botão direito do mouse no nome da tabela e em seguida em "Importar".
Link com a orientação completa: https://stackoverflow.com/questions/19400173/how-should-i-import-data-from-csv-into-a-postgres-table-using-pgadmin-3

Importação de XLS para PgAdmin


Adicionar propriedade em relação N-N

Question: Como adicionar propriedades (colunas) numa relação N-N usando EF?
Por exemplo, eu tenho a classe Produto:
public class Produto
{
    [Key]
    public int ProdutoID { get; set; }
    public string Descricao { get; set; }
    public decimal Valor { get; set; }

    public virtual ICollection<Venda> Vendas { get; set; }

    public Produto()
    {
        this.Vendas = new List<Venda>();
    }
}

E tenho a classe Venda:
public class Venda
{
    [Key]
    public int VendaID { get; set; }
    public string Descricao { get; set; }
    public decimal Total { get; set; }

    public virtual ICollection<Produto> Produtos { get; set; }

    public Venda()
    {
        this.Produtos = new List<Produto>();
    }
}

Estou usando Code First e por isso o EF cria automaticamente uma tabela VendaProdutos, o problema é que essa tabela deveria ter mais campos, como Quantidade, etc.
Existe alguma forma que eu possa fazer isso sem fugir do Code First ou terei que procurar outra forma de fazer o mapeamento das minhas classes (Fluent API por exemplo)?
Comment: Você terá que criar uma nova classe que ficará entre o seu relacionamento pra poder ter uma propriedade nova.
Comment: Relações `NxM` são feitas de forma a ter uma tabela intermediária que recebe uma relação `1xN` de cada uma das outras tabelas envolvidas. Nesse caso em específico, a tabela intermediária (`ProdutoVenda`) deve ter uma chave composta pelo par das chaves das duas tabelas externas. E uma vez que esta intermediária for receber uma entrada, deve-se ter certeza que a quantidade vendida não excede o estoque...
Comment: Então eu teria que trocar essas relações N-N para relações 1-N né. Não tinha pensado nisso.
Comment: Exatamente, no caso teria uma classe `ProdutoVenda` no meio.
Answer: Creio que a única solução seja criar uma classe para o relacionamento.
No seu caso, ficaria da seguinte forma:
Classe Produto
public class Produto
{
    [Key]
    public int ProdutoID { get; set; }
    public string Descricao { get; set; }
    public decimal Valor { get; set; }

    public virtual ICollection<ProdutoVenda> ProdutosVenda { get; set; }

}

Classe Venda
    public class Venda
    {
        [Key]
        public int VendaID { get; set; }
        public string Descricao { get; set; }
        public decimal Total { get; set; }

        public virtual ICollection<ProdutoVenda> ProdutosVenda { get; set; }

    }

Classe ProdutoVenda
public class ProdutoVenda
{
    public int ProdutoId { get; set; }
    public int VendaID { get; set; }

    public virtual Produto Produto { get; set; }
    public virtual Venda Venda { get; set; }

    public int Quantidade { get; set; }
}

Comment: Aqueles construtores em `Produto` e `Venda` não são mais necessários. Você pode removê-los da resposta. Os outros elementos `virtual` o Entity Framework se encarrega de preencher.
Comment: @cigano, valeu! Tinha esquecido desse detalhe quando copiei ;)

Adicionar propriedade em relação N-N


Mann Whitney U and ROC AUC relationship

Question: I've been learning about the relationship between Mann-Whitney U.
Supposedly, the area under the ROC curve should be
$\text{AUC} = \frac{U}{n_0 n_1}$, where $U$ is the Mann-Whitney statistic, $n_0$ is the number of negative examples, and $n_1$ is the number of positive examples.
I attempted to test this using python's scipy and scikitlearn library and found some unaccounted discrepancies.
Unfortunately, I can't share the data, but here's the code and output.
U = mannwhitneyu(preds['score'], preds['truth'])[0]
vc = preds['truth'].value_counts()
n0n1 = vc.loc[0] * vc.loc[1]

print('U: %d' % U)
print('n0n1: %d' % n0n1)
print('U/n0n1: %0.3f' % (U/n0n1))
print('AUC: %0.3f' % roc_auc_score(preds['truth'], preds['score']))

output:
U: 26899093155
n0n1: 40496604804
U/n0n1: 0.664
AUC: 0.674

However, when I using the implementation described in the above link:
def calc_U(y_true, y_score):
    n1 = np.sum(y_true==1)
    n0 = len(y_score)-n1

    ## Calculate the rank for each observation
    # Get the order: The index of the score at each rank from 0 to n
    order = np.argsort(y_score)
    # Get the rank: The rank of each score at the indices from 0 to n
    rank = np.argsort(order)
    # Python starts at 0, but statistical ranks at 1, so add 1 to every rank
    rank += 1

    # If the rank for target observations is higher than expected for a random model,
    # then a possible reason could be that our model ranks target observations higher
    U1 = np.sum(rank[y_true == 1]) - n1*(n1+1)/2
    U0 = np.sum(rank[y_true == 0]) - n0*(n0+1)/2

    # Formula for the relation between AUC and the U statistic
    AUC1 = U1/ (n1*n0)
    AUC0 = U0/ (n1*n0)

    return U1, AUC1, U0, AUC0

gives me the correct equality $\text{AUC} = \frac{U}{n_0 n_1}$.
I've attempted applying the solution described here, but this is not resolving my issue.  I'm wondering if it could have anything to do with these particular implementations with the functions.
Comment: not sure if the open issues on Mann-Whitney address the issue you are having. It is difficult to say without data for a reproducible example. Can you provide mocked or simulated data which illustrate? Also you may find it helpful to check the open issues on the scipy github repo to see if the open issues are impacting you (i'd guess they do but can't be sure without an MRE). Github link: https://github.com/scipy/scipy/issues?q=is%3Aissue+is%3Aopen+mann+whitney+
Answer: I believe Lukas Roberts is correct that this is an open issue in scipy. The current implementation reverses the definitions of U1 and U2.
The problem is in these lines:
u1 = n1*n2 + (n1*(n1+1))/2.0 - np.sum(rankx, axis=0)  # calc U for x
u2 = n1*n2 - u1  # remainder is U for y

When we place the definition of u1 in u2, we see that the formula for u2 used here is in fact the formula for u1 from Wikipedia:
u2 = n1*n2 - n1*n2 + (n1*(n1+1))/2.0 - np.sum(rankx)

There is an complete revision in the works, but the pull request seems to be stuck for now.
Comment: Sounds great.  Thanks for the explanation!
Answer: Please check the case where it has the tied value.
=======================================
Assign numeric ranks to all the observations (put the observations from both groups to one set), beginning with 1 for the smallest value. Where there are groups of tied values, assign a rank equal to the midpoint of unadjusted rankings. E.g., the ranks of (3, 5, 5, 5, 5, 8) are (1, 3.5, 3.5, 3.5, 3.5, 6)
(the unadjusted rank would be (1, 2, 3, 4, 5, 6)).
from https://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U_test#Calculations

Mann Whitney U and ROC AUC relationship


Printing on a local printer from SSH remote session

Question: I have the following problem. My normal work files are on a remote machine that I can SSH to. I want to be able to print from remote machine on a local printer without having to print to a file, and then scp file to the local machine.
What I need is a script or a program that can be invoked on the remote machine that accepts a file as an argument (or reads input from stdin, it does not matter), and somehow executes lpr on the local machine.
Constraints:
1) The local machine does not run SSH server. Thus, I cannot have a script that scp's the file back to local machine, and remotely executes lpr.
2) There is no mail server on the local machine, so I cannot send the file via e-mail, and have it filtered locally.
Both machines run Linux with normal assortment of programs, and I can run my own scripts, and compile programs. Any ideas?
Answer: Assuming that your local linux server runs a print server, you could use ssh port forwarding to make it work...

On your local machine, you need a print server that's configure to accept remote printing requests using the lpr protocol.
When you connect to the remote server, add this to your ssh command line: -R 515:localhost:515
This will cause ssh to listen to the tcp port 515 on the remote server and forward all connections to the local machine through the ssh tunnel. Once there, connections will be forwarded to port 515 of localhost which is your print server.
Configure a print queue on the remote machine that points to localhost:515.

If the remote server already runs a print server, I believe it's possible the change the ports to prevent a conflict.
Comment: The main problem with this is that I am not an administrator on the remote machine, and cannot configure printer queues.
Comment: @Boris: this solution doesn't require configuring a printer queue on the remote machine. If both sides are using cups, use the `lpoptions` command to set the default server to `localhost:12345` where 12345 is the port you forward back to your local machine. Other print servers may require a little more setup (up to installing your own `lpr` binary in `~/bin`).
Comment: It seems that CUPS requires forwarding of port 631, rather than port 515.
Answer: I found the program that I needed: bcvi utilizes reverse port forwarding to allow execution of various commands on local computer. It is not well-documented, and in particular makes severe assumption about the installation, and in my case, the default changes it made to shell initialization file conflicted with handling of TERM variable elsewhere, but the source code is self-explanatory, and easy to modify.
Answer: Printing via VT100 control codes, you can shove PostScript or other binary files via these commands back to your local printer, your client may vary.
Some terminals support local printing:
Print Screen        <ESC>[i
Print the current screen.
Print Line      <ESC>[1i
Print the current line.
Stop Print Log      <ESC>[4i
Disable log.
Start Print Log     <ESC>[5i
Start log; all received text is echoed to a printer.

http://www.termsys.demon.co.uk/vtansi.htm#printing


Typoscript 9.5 render FILES Object like Contentelement

Question: i get via CONTENT an Element from the page and want to render the image from this like all other images in the page.
i have installed sms for responsive images but when i render this image via typoscript an normal  will be outputtet.
how must i change my typoscript to render like in the partial file Media/Rendering/Image?
                            20 = FILES
                            20{
                                references {
                                    table = tt_content
                                    fieldName = image
                                    uid.data = field:uid
                                }
                                begin = 0
                                maxItems = 1
                                renderObj = IMAGE
                                renderObj {
                                    file.import.data = file:current:publicUrl
                                    file.width = 1920c
                                    file.treatIdAsReference = 1
                                    stdWrap.outerWrap = |
                                }
                            }

thank you very much!
Answer: As the example you mention is rendered in Fluid, I'd suggest to use Fluid as well. The ViewHelper for responsive images is used in Fluid.
Try to get away from rendering via TypoScript and use Fluid templates.