Show content whose "team" field matches the user "team" field

Question: The users on my site all have a field named "team" and my content types also have the same field.
I want to create a views page that shows all nodes where that "team" field equals the same field on the user node.
Example:
USERS:
user1, team: team1
user2, team: team1
user3, team: team2

NODES:
node1, team: team2
node2, team: team1
node3, team: team1

When I'm logged in as user 3, I want my view to show node 1.
I know that I have to work with the contextual filters and context but I'm unable to get the output I need.
Comment: Both the OG Groups module, and the Group module can do this.
Comment: what type is the team field? is that taxonomy term?
Comment: Sorry, yes, it's a taxonomy term.
Comment: @Jaypan both these modules can be a solution but what I need is so simple that I would prefer to not install modules for that.
Answer: The easiest way to set this up is through the Content: Has taxonomy term ID Contextual filter.

In Content Views listing your nodes add that Contextual filter and set it up:
When the filter value is NOT available
Provide default value
Taxonomy term ID from URL
Load default filter from node page, that's good for related taxonomy blocks
Filter to items that share any term
When the filter value IS available or a default is provided
Specify validation criteria
Taxonomy term
[your vocabulary]
Term ID
Adjust the other option as necessary.
Now if you enter some taxonomy term ID into the Views Preview field, it will be picked up by the Contextual filter and show only nodes that have been tagged with that term.

Create a block Display and place your block in a region shown on entities that have the taxonomy term ID chosen. The block will show all nodes that share the term.

In your case if you place that block on User Profile pages, it will show the nodes related through the same Taxonomy term.

If you want to show that Block anywhere on the site, and have it take the Context of the currently logged in user you have to approach it a bit differently.
There may be more ways on how to achieve this, here's one:

In the original Views where you show a list of nodes add a Relationship for the Taxonomy term field (Field: Team).
Then add another Relationship that is reverse, going from Taxonomy term to the field on Users (Taxonomy term: User using …). This is where the term matching is done, the second Relationship will use the first one. This introduces the User info into your content Views.
Then add the Contextual filter for User ID (User Uid), it will use the second Relationship. Set up the Contextual to use the logged in user context:
When the filter value is NOT available
Provide default value
User ID from logged in user
When the filter value IS available or a default is provided
Specify validation criteria
User ID
Single ID
Place the Block anywhere on the site and it will only show nodes related to the user that is currently logged in, even if you place it on user profiles and view other users profiles!
Comment: Thanks prkos. Tried the second part of your answer but I get 0 results. From what I understand the view is of type content, not user right?
Comment: Yes, it should be Content type View if you want to show/list nodes. The user is just the context for which nodes to show. It's impossible to figure out what might be wrong. Go step by step and test each change that it does what it should. Add User field using the second Relatinship to test that it matches correctly. Only then add the Contextual filter, you can test that Contextual in a new Test views without Relationships.
Comment: Solved. I think I was missing something and I don't know what.
Comment: Yes, sometimes IT problems just work the second time you try :) I'm glad it's working now :)


How to get pgrep to display full process info

Question: Is there any way to get pgrep to give me all the info about each process that ps does?  I know I can pipe ps through grep but that's a lot of typing and it also gives me the grep process itself which I don't want.
Answer: pgrep's output options are pretty limited. You will almost certainly need to send it back through ps to get the important information out. You could automate this by using a bash function in your ~/.bashrc.
function ppgrep() { pgrep "$@" | xargs --no-run-if-empty ps fp; }

Then call the command with.
ppgrep <pattern>

Comment: Thanks!  I modified it to: `function ppgrep() { pgrep "$@" | xargs ps fp 2> /dev/null; }`  Otherwise, if no processes match your search, it dumps a whole `ps` usage megilla.
Comment: If you want to avoid the ps usage page, GNU xargs has an option, `-r` that will only execute the command if it has received a list.
Comment: More concise way is `ps fp $(pgrep -d, "$@")`
Comment: On OS X, the `ps` needs a hyphen for the flags: `function ppgrep() { pgrep "$@" | xargs ps -fp 2> /dev/null; }`
Answer: Combine pgrep with ps using xargs!
pgrep <your pgrep-criteria> | xargs ps <your ps options> -p

For example try
pgrep -u user | xargs ps -f -p

to get a full process list of user. Option -u user limits pgrep to the user given (as a number or name) while the ps options -f -p request a full format listing for the selected PID.
It's nice that you keep the first line with the column names. grep always drops the column names.
Comment: This should be the accepted answer, because it uses unix pipes in a proper way, taking a list of PIDs from one tool and feeding back into another (if it seems like hackery, it's not - this technique can be used in LOTS of UNIX tools, like email grep tools. The Bash function ppgrep() is an unnecessary dependency, and avoids confronting the learning opportunity presented here.)
Comment: @ScottPrive but, the OP said "I know I can pipe ps through grep but that's a lot of typing" so probably that is the reason why this is not the accepted answer.
Comment: @alerio-bozzolan Yes, the OP said "grep", and `"pgrep"` is a different application,  different installer package, different authors. pgrep is less typing, because it is specialized to only work with processes (it's not general-purpose like grep).
Answer: The following only gives you PID + full command-line.  For "all the info ps does", see other answers...
Most linuxes use procps-ng.  Since 3.3.4 (released in 2012), pgrep -a (--list-full) shows the full command line.
Note: By default pgrep only matches the pattern you give against the executable name.
If you want to match against the full command line (as grepping ps does), add the -f (--full) option.
In older versions (including the original procps project), -l option showed info but it's behavior varied:

pgrep -fl matched the pattern against full command line and showed the full command line.
pgrep -l alone matched only executable name and showed only executable name.
If you don't want full match, you couldn't see the full command line :-(
[https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=526355#15]

Not sure what code *BSD use but their man page documents the old -fl behavior.
Unfortunately you can't even use -fl portably - in recent procps-ng, -f (--list-name) always prints only the executable name.
Answer: Linux grep
For the GNU version of pgrep long + fuzzy output is achieved with -af and the string must be case-sensitive (i.e. there is no option for case-insensitivity).
$ pgrep -af apache

OUTPUT:
    1748 /usr/sbin/apache2 -k start

Man page:
   -a, --list-full
       List  the  full  command line as well as the process ID.  (pgrep only.)

   -f, --full
       The pattern is normally only matched against the process name.
       When -f is set, the full command  line is used.

MacOS/BSD grep
On MacOS/BSD -l (long output) in combination with -f (match against full argument lists) will display the complete command (-i adds case-insensitivity):
$ /usr/bin/pgrep -fil ssh

OUTPUT:
    33770 ssh: email@example.com [mux] t

man page:
 -l          Long output.  For pgrep, print the
             process name in addition to the
             process ID for each matching
             process.  If used in conjunction
             with -f, print the process ID and
             the full argument list for each
             matching process.  For pkill, dis-
             play the kill command used for
             each process killed.

❗NOTE: GNU grep can be installed on MacOS with brew install pgrep — this will expose the GNU flavour under /usr/local/bin/pgrep and depending on your $PATH config might break code which relies on the BSD syntax of pgrep.
Answer: Use the -v option to grep - it returns everything BUT the requested pattern.
ps -ef | grep <process> | grep -v grep

Comment: This was the best answer for me. It shows you the full command without being truncated as it happens with `pgrep -u user | xargs ps -f -p`
Answer: I know it is an old topic. However, It was useful to me, and I would like to share what I did that worked. It is pretty straightforward for beginner (like me) and may help others:
[root@2a8ad900f55e tmp]# pgrep -f '(^|/)db2fmcd'|xargs ps |awk '{print $5}'
COMMAND
/opt/ibm/db2/V11.5/bin/db2fmcd

Note that the column "CMD" was also showed, but the question of this thread was to show the command, and not only the command. Anyway, I am trying to remove that column and will update my post with the command too.
Note:
The arguments "--no-headers and -o command" isolated the output only to the command. And I could remove the awk print.

[root@2a8ad900f55e tmp]# pgrep -f '(^|/)db2fmcd'|xargs ps -o command --no-headers
/opt/ibm/db2/V11.5/bin/db2fmcd
[root@2a8ad900f55e tmp]#

Answer: I don't think there is, the most information you can get is the name and process id by using the -l option to pgrep.
ps supports all sorts of formatting options, so I would just make an alias for what you want to save the typing.  A simple way to exclude the grep process from the output us to include an additional pipe to grep -v grep to exclude any grep processes.
Answer: In order to eliminate the grep process, you can use brackets as part of your pattern:
ps -ef | grep '[t]ty'

You can do this with ps and pgrep:
ps -fp $(pgrep -d, tty)

Answer: This will help you I guess:
ps auxww
Comment: Can you please elaborate.  More info would improve this answer


Zero divisors in a ring $Q^2$

Question: I was asked to show that the ring $\langle\mathbb{Q^2},\oplus,\otimes _m \rangle$, where $m \in \mathbb{N}$ and $(a,b)\oplus(c,d)=(a+c,b+d)$, and $(a,b)\otimes _m (c,d)= (ad+mbc, ac+ bd)$ contains zero divisors $\Leftrightarrow$ when $m$ equals to the square of a rational number.
As I understood I need to show that $ab = 0$ or $ ba=0,\; $  for some $a\in \mathbb{Q^2}$ ,where $b \in \mathbb{Q^2} \setminus \{0\}$
This is what I have done by far. Concluding $m=-(\frac{a}{b})^2$
$m=-(\frac{a}{b})^2$." />

Should I operate with the modulus? or just with the coordinates according under the operations allowed in this ring?.
I want to know what I am supposed to do with the
$(a,b)\oplus(c,d)=(a+c,b+d)$ information. I got confused with the
vector $\mathbb{Q^2}$ information and vector:
Comment: That should be $(a,b) \otimes (c,d) = (ac + mbd, ad + bc)$ otherwise the multiplication isn't associative (or commutative, or have an identity).
Comment: Thanks! I understood.@TrevorGunn Could you comment my solution? may be something to add?
Comment: @deEgea Well the entirety of your work is based on an incorrectly given definition, so in any case none of it is "correct".
Comment: @BenGrossmann Understand, could you suggest me how to start or give me some hints. Could you tell me more precesily what is the correct definition?
Answer: Hint: Your ring is isomorphic to ${\mathbf Q}[x]/(x^2-m)$, which has no zero divisors if and only if $x^2-m$ is irreducible over $\mathbf Q$.

Zero divisors in a ring $Q^2$


problem in install pyearth in jupyter (python)

Question: I try to install pyearth package by pip in jupyter notebook
but it's not work
( my python = 3.7.0 )
I need to install this package important
I found this code to install
git clone git://github.com/scikit-learn-contrib/py-earth.git
cd py-earth
sudo python setup.py install

but I don't where I put it to install the package :( !
I try to copy the first line and put it in jupyter notebook but this error appear :

'git' is not recognized as an internal or external command, operable
program or batch file.

Can any one help me step by step how can I install the package?
Comment: can any one help me :( ??
Comment: Is it the one being referenced [here](https://github.com/scikit-learn-contrib/py-earth#installation)? If that's the one, then run in a cell in you notebook, `%conda install -c conda-forge sklearn-contrib-py-earth`. That's based on [here](https://anaconda.org/conda-forge/sklearn-contrib-py-earth), which looks similar to what your code references. If you aren't using Anaconda/`conda` yet , see https://www.anaconda.com/products/distribution for getting the Anaconda distribution for your machine. Or see if there is a way to add git to your windows machine?
Answer: maybe you need to add the following paths to PATH:
C:\Program Files\Git\bin\
C:\Program Files\Git\cmd\
And check that these paths are correct – you may have Git installed on a different drive, or under Program Files (x86). Correct the paths if necessary.
setting PATH on Windows 10:

Search for "environment variable" in taskbar search
Select "Edit the system environment variables".

Klik "Environment Variables" button at the bottom.

Double-click the "Path" entry under "System variables".

With the "New" button in the PATH editor,
add C:\Program Files\Git\bin\ and C:\Program Files\Git\cmd\ to the end of the list.

Close, reload and re-open your console

I hope this will help you :)


xDebug not working using docker, vscode and WSL 2

Question: I am not sure what the issue is, it just doesn't work.
The routing seems to work, I have a server name in my nginx conf file. e.g. test.com. that works.
My project is in the root of ubuntu and not in the mount folders.
I am not sure what else to try.
xdebug.ini
[XDebug]
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so

xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9002
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_host=soapboxtest.com
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/usr/local/etc/php/xdebug.log

launch.json
{
  "name": "Listen for XDebug",
  "type": "php",
  "request": "launch",
  "port": 9002,
  "log": true,
  "externalConsole": false,
  "pathMappings": {
    "/var/www": "${workspaceRoot}"
  },
  "ignore": [
    "**/vendor/**/*.php"
    ]
  },

Request cookies
"XDEBUG_SESSION" => "VSCODE"

Dockerfile
FROM php:fpm-alpine3.11
...
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
...

Answer: This happened to me also and found that XDEBUG is not looking for the Docker Daemon Host. Got it fixed by adding:
"hostname": "0.0.0.0"

As part of the general options of the launch.json on VS Code.

Full example:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "XDebug",
            "type": "php",
            "request": "launch",
            "hostname": "0.0.0.0",
            "port": 9000,
            "pathMappings": {
                "/var/www/html/": "${workspaceFolder}"
            },
            "xdebugSettings": {
                "max_children": 999,
                "max_data": -1,
            }
        }
    ]
}

Comment: Awesome, thank you. It works. I found a work around in the meantime, you can set the remote host to the linux machine ip address, problem is it isn't static.
Comment: Alternatively, You can set `"hostname": "localhost"`
Answer: After had a colleague helping me to configure Xdebug to listen out of WSL Remote environment from VSCode, I´ve looked up for an option that is opened when you click on the WSL Remote icon on the lower left side of the IDE:
Remote-WSL: Show Log
It opens a WSL terminal, which shows (between several infos) the IP Address that WSL is using. Adding this WSL IP to the xdebug.remote_host value, the only thing left to get Xdebug running correctly from your Docker container, inside your WSL2 environment (from WSL Remote VSCode extension), is adjusting your application path inside the launch.json file.
For that, clicking with the right-button mouse at the launch.json file tab, search and click on the "Copy path" option, and then paste it inside the pathMappings setting, like this sample (don´t forget to adjust the path removing the info relative to launch.json itself, it's for your application path!):
"pathMappings": {
      "/application": "paste here the path you've copied"
 }

Try it, for me worked like a charm. And if it work for you too, vote for my answer!
Answer: I have spent many days "ripping my hair".. I use docker.

add rule into firewall to allow traffic (taken/quoted from: https://github.com/microsoft/WSL/issues/4171#issuecomment-559961027
Go there for screenshots if this isn't clear enough)

In windows Start menu, type Firewall and choose Advanced settings, then Inbound Rules
select custom rule.
Click Next until you get to Protocol and Ports in the left menu
Select ICMPv4 as protocol type.
Select Next until you end up in the Name section, enter any name and click Finish.

If you now try pinging your host ip from wsl2, it should work as
expected.
For every other connection between your wsl2 and your host, you have
to allow the inbound rules for private and public networks or if they
don't exists manually create the rules for the corresponding UDP/TCP
port, but be aware that this might impact your security, if you use
your computer in public networks.

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9900,
            "pathMappings": {
                "/linux/path/in/docker":"${workspaceFolder}",
            }
        },
    ]
}

Dockerfile

RUN echo "zend_extension=/usr/lib/php/20190902/xdebug.so\n \
         xdebug.remote_enable=on\n \
         xdebug.remote_autostart=on \n \
         xdebug.remote_connect_back=on\n \
#         xdebug.remote_host=$HOST_IP\n \
#         xdebug.remote_log_level=9\n \
#         xdebug.remote_log=/var/www/mywebdir.com/xdebug.log \n \
         xdebug.remote_port=9900" > /etc/php/7.4/mods-available/xdebug.ini;

EDIT:
After a while I started having problems again. Try these:

uncomment the logging lines above in dockerfile

try hard coding your windows ip address like 192.168.0.x
(uncomment xdebug.remote_host=... and comment out xdebug.remote_autostart=on)
My xdebug appeared to be connected but closed connection straight after because it had to be my windows ip. My understanding is that its the VSCode that responds to xdebug requests which runs in windows so the debugging packets need to get through out of WSL2.

try logging level 10. If you see that xdebug can't find a match for the breakpoints, check your webroot mapping in docker and pathMappings in vscode/launch.json

Check that you do have a breakpoint set (stupid but yeah)


Problem with Automapper 8.0 in a .NET WCF Service

Question: I have just upgraded Automapper in my WCF Service project to the newest version 8.0.0. I get following error when calling the service via WCF Test client:

System.InvalidOperationException: 'Mapper already initialized. You must call Initialize once per application domain/process.'

With the old Automapper version 6.0.2 it's working and no exception is thrown. I initialzed the Mapper with a seperate Service Behaviour class.
Service Class EdiPartners.cs:
[AutomapServiceBehavior]
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode =
InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class EdiPartners : IPartners
{
    [FaultContract(typeof(ExceptionObject))]
    public Supplier GetPartnerData(string SupplierNumber)
    {
       // Code
    }
}

Service Behaviour Class AutomapServiceBehavior:
    public sealed class AutomapServiceBehavior : Attribute, IServiceBehavior
    {
    #region IServiceBehavior Members

    public void AddBindingParameters(ServiceDescription serviceDescription,
    ServiceHostBase serviceHostBase,
    Collection<ServiceEndpoint> endpoints, BindingParameterCollection
    bindingParameters)
    {
        AutomapBootstrap.InitializeMap();
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    #endregion
}

Static Automapper Initializing Class AutomapBootstrap:
   public class AutomapBootstrap
{
    public static void InitializeMap()
    {
        Mapper.Initialize(config =>
        {
            config.CreateMap<Supplier, IEdiPartnerData>();
            config.CreateMap<IEdiPartnerData, Supplier>();
            config.CreateMap<IEdiPartner, EdiPartner>();

        });
    }
}

Maybe you guys can help me. I don't understand why it is working with the old but not with the new version. Is there a better way for initializing Automapper in a WCF Service?
Thx in advance,
Sandro
Comment: By the way, `ConcurrencyMode.Multiple` makes no sense when `InstanceContextMode =
InstanceContextMode.PerCall`.  It's not possible to share the server
Comment: Not sure it is related to your issue, but you could change your redundant Supplier-IEdiPartnerData mappings (back and forth) and instead use `config.CreateMap().ReverseMap()`.
Comment: @MickyD see https://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and#Instance%20mode%20=%20per%20Call%20and%20Concurrency%20=%20Multiple
Comment: @Edword thx, did not know this method
Comment: Because the old version didn't throw. But your code was broken nevertheless.
Comment: @LucianBargaoanu and what exactly is broken in my code? Can you help with?
Comment: The exception tells you exactly what's wrong.
Comment: I'm not sure that codeproject proves anything.  A gap of 2 seconds between calls is zero evidence for concurrency. The _"different threads"_ is also just an indication of random thread assignment from the thread pool. Like I said, it's a pointless configuration for the mode you are in.  _[Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus](https://www.amazon.com/Programming-WCF-Services-Mastering-AppFabric/dp/0596805489)_
Comment: I need a solution what to adjust in my code, no more and no less!
Answer: Since your service is InstanceContextMode.PerCall a new instance will be created per call.  In addition, because you have decorated your service with [AutomapServiceBehavior] it is going to initialise not once per AppDomain (like the error says) but per instance.
So change your code to:
public sealed class AutomapServiceBehavior : Attribute, IServiceBehavior
{
    static bool _initialised;

#region IServiceBehavior Members
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
        if (_initialised)  // already init'd per this AppDomain
            return;

        AutomapBootstrap.InitializeMap();
        _initialised = true;
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }
#endregion
}

Useful Service Behaviours or Custom Service Host?
By the way, AutomapServiceBehavior is probably not a good example of writing service behaviours because if you intend to decorate multiple services with it (possibly with different mappings) in the same assembly it won't work.  AutoMapper likes to be initialised only once at application startup so if it were going to be initialised anywhere I would put it in a custom ServiceHost and do away with the behaviour.
Comment: @Karamafrooz AutoMapper will work correctly if it is initialsed exactly once per AppDomain.  Otherwise you will encounter _"Mapper already initialized. You must call Initialize once per application domain/process"_
Comment: thx for the answer you saved my day. I've implemented your suggestion with a service host factory. I will post it seperatly.
Comment: I am really confused here. If we check _initialised as you suggested, then the initialization will happen only once per Application domain since the static _initialised is an application state variable. So why won't the Automapper work properly? Also if I want to use the Custom Service Host as you suggested like the answer Sandro has posted, will I need to create a seperate Factory for each service class?
Answer: The solution is to implement a custom service host factory class:
public class EdiPartnersFactory : ServiceHostFactory
{
   protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
   {
    // Initialize Automapper
    AutomapBootstrap.InitializeMap();

    var host = new ServiceHost(serviceType, baseAddresses);

    return host;
    }
}

And I changed the web.config for the service contract and the binding:
    <system.serviceModel>
  <services>
    <service name="Company.Sap.EdiPartners"
             behaviorConfiguration="MEXServiceTypeBehavior">
      <endpoint address=""
                binding="basicHttpBinding"
                contract="Company.Sap.IPartners" />
      <endpoint address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="MEXServiceTypeBehavior">
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

Change the .svc file:
<%@ ServiceHost Language="C#" Debug="true" Service="Company.Sap.EdiPartners" Factory="Company.Sap.EdiPartnersFactory" CodeBehind="EdiPartners.svc.cs" %>

So no use of a service binding in the service class (looks much more cleaner):
public class EdiPartners : IPartners
{
    [FaultContract(typeof(ExceptionObject))]
    public Supplier GetPartnerData(string SupplierNumber)
    {
       // Code
    }
}

Comment: Don't forget to mention who suggested it


Adjusting contrast of Image from Byte Array of the image

Question: Can I change the contrast of an image from the byte stream of the Image? I have done   necessary to do copying the image and I need to add the change contrast part to the code.
Any idea how to do this? Or is it even possible?
<code>package make.image.bw;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class MakeImageBWActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);                  // set screen view
        String imageInSD      =     Environment.getExternalStorageDirectory().getAbsolutePath() +"/earthglobe.jpg";
        RandomAccessFile file =     null;
        try {
            file = new RandomAccessFile(imageInSD, "r");
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        File myFile               =     new File(Environment.getExternalStorageDirectory(), "latestearth.jpg");
        byte[] buffer             =     new byte[1024];
        try {
            FileOutputStream out  =     new FileOutputStream(myFile);
            while(file.read(buffer)!=-1){
                out.write(buffer,0,1024);
            }
             out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.d("tag", "finished");
        finish();
        }
}
</code>
Thanking You in advance for your valuable suggestions.
Answer: You should try to look at that Google IO 2012 session : Doing More With Less: Being a Good Android Citizen
There is a good demo of image manipulation with RenderScript at 22min30.
The code of the presentation can be found here


ColumnConstraints.setPercentWidth() not working when using array

Question: I'm using GridPane for my JavaFX layout, and I always use width percentages  for ColumnConstraints, but in one specific case it seems it does not work: when I set the width percentage of a ColumnConstraints inside an array.
My code:
ColumnConstraints[] myColumns = Factory.createColumns(3);
myColumns[0].setPercentWidth(35);
myColumns[1].setPercentWidth(12);
myColumns[2].setPercentWidth(53);
devicePane.getColumnConstraints().setAll(myColumns);

The array comes from my utility method for column creation using equally divided sizes:
public static final ColumnConstraints[] createColumns(int count) {

    ColumnConstraints[] columns = new ColumnConstraints[count];

    ColumnConstraints column = new ColumnConstraints(1, 10, Double.MAX_VALUE);
    column.setPercentWidth(100/count);
    column.setFillWidth(true);
    column.setHgrow(Priority.ALWAYS);
    column.setHalignment(HPos.CENTER);

    for(int i = 0; i < count; i++) {

        columns[i] = column;

    }

    return columns;

}

Result:
Columns with old width percentage
But the expected is:
Columns with right width percentage
Answer: You're just creating a single ColumnConstraints object. This makes it impossible to assign different values to the percentWidth property. You need to create seperate objects for this purpose:
public static final ColumnConstraints[] createColumns(int count) {
    ColumnConstraints[] columns = new ColumnConstraints[count];

    for(int i = 0; i < count; i++) {
        ColumnConstraints column = new ColumnConstraints(1, 10, Double.MAX_VALUE);
        column.setPercentWidth(100/count); // do we still need this?
        column.setFillWidth(true);
        column.setHgrow(Priority.ALWAYS);
        column.setHalignment(HPos.CENTER);
        columns[i] = column;
    }

    return columns;
}

Comment: So even if I assign the object to different positions in the array and I set the attributes of one of them, are all of them affected?
Comment: `ColumnConstraints` objects can be used multiple times without any issue. There's one "set of property data" per object though. If you use the same object multiple times, all those columns will be set up with the same properties. If you want to assign different properties to each column, you need one "set of data" ***per column***, i.e. each column needs a seperate instance of `ColumnConstraints`. Otherwise all column properties for all columns end up with the values last assigned to "one of" them.


Not able to display all Buttons on JFrame

Question: Below is my code. I am not able to add all the 6 buttons. Only Button1 - 3 or Button4-6 are getting displayed at a time.
Kindly let me know where I am going wrong.
// This class contains the main method and launches the Main screen
import javax.swing.*;
import java.awt.*;

public class LearningHome{
    public static void main(String[] args){
        JFrame mainFrame = new JFrame("Welcome to the Learning! ");

        try {

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(800, 800);
        mainFrame.setVisible(true); // Without this property the frame will not be visible

        FlowLayout mainLayout = new FlowLayout();
        JPanel mainPanel = new JPanel();

        mainPanel.setLayout(mainLayout);

        mainPanel.add(new JButton(" Button 1 "));
        mainPanel.add(new JButton(" Button 2 "));
        mainPanel.add(new JButton(" Button 3 "));

        JPanel subPanel = new JPanel();

        subPanel.setLayout(mainLayout);

        subPanel.add(new JButton(" Button 4 "));
        subPanel.add(new JButton(" Button 5 "));
        subPanel.add(new JButton(" Button 6 "));

        mainFrame.add(mainPanel, mainLayout.LEFT);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.add(subPanel, mainLayout.RIGHT);
    }
}

Answer: You did not mention the exact layout you seek, and there is a wide number of way to go about arranging components, but to address your specific comment

I am not able to add all the 6 buttons. Only Button1 - 3 or Button4-6 are getting displayed at a time

Add all elements to the JFrame before it becomes visible (eg. move the mainFrame.setVisible(true) after you add components to mainFrame. This way the LayoutManager can arrange the components as needed
Consider calling mainFrame.pack(); prior to calling setVisible (see What does .pack() do? )
The default LayoutManager for the content pane of a JFrame is BorderLayout (default for a JPanel is FlowLayout - so no need to explicitly set the Layout as such)...if you wish to add the two panels so they layout in a line, consider using an appropriate combination of BorderLayout parameters.

For example:
mainFrame.add(mainPanel, BorderLayout.WEST);
mainFrame.add(mainPanel, BorderLayout.EAST);
mainFrame.pack();//call these methods after adding components
mainFrame.setVisible(true);

You can alternatively stack them into two lines using the appropriate BorderLayout parameters. For example:
mainFrame.add(mainPanel, BorderLayout.CENTER);
mainFrame.add(mainPanel, BorderLayout.SOUTH);

Comment: PACK was a good option. But with that my page is getting shrink, which I dont need. Apart from that I had implemented everything. Your explanation was awesome. Thank you so much.


How can I block users from registering with disposable email addresses?

Question: I have a site where users send messages to one another using the Private Message module, and they are notified of messages by email.
On this site, there are a variety of spammers, scammers, and abusive users who attempt to register multiple accounts using disposable email addresses.
I know it's impossible to block every possible disposable email address domain, but is there anything in the Drupal world that can help me?

I considered white-listing valid domains, but my user base is too diverse for this.
The only project I could find on drupal.org has been abandoned.
There are third-party APIs such as this one.

At the moment, I'm blocking them manually using this code:
/**
 * Custom validation function from the Domain Registration module (1.0).
 *
 * Checks if the domain in the email address is on a list of blocked domains.
 * @param $form
 * @param $form_state
 */

function MYMODULE_domain_registration_user_register_validate(&$form, &$form_state) {
  $default_message = t('You are not allowed to register for an account on this site.  Contact support for more information.');
  $mail = explode('@', $form_state['values']['mail']);
  $exact_domains = array(
    'ad.drupaler.org',
    'drupaler.org',
  );
  $partial_domains = array(
    'pp.ua',
    'guerrilamail',
  );
  if (isset($mail[1]) && !empty($exact_domains)) {
    if (in_array($mail[1], $exact_domains) || MYMODULE_contactinfo_check_banned_words($mail[1], $partial_domains)) {
      form_set_error('account', variable_get('domain_registration_message', $default_message));
    }
  }
}

Comment: You should use a [ReCaptcha](https://www.drupal.org/project/recaptcha) in you registration form, if you aren't already, to get rid off all the robot/spider/automated spam registrations.
Comment: @NoSssweat Alas, in my case, the problem is manually registered spam.
Answer: Like you mentioned it is impossible to block all disposable domains.
If you want to end the madness, the most effective solution would be: Upon registration you send a confirmation link or code to a mobile phone number
Take a look at:

SMS Framework module
VoIP Drupal module

Using Unique field you could make sure that the phone number can only be used once.
Checkout: How to send SMS messages (Drupal Tutorial)
Answer: Your solution is good ( but I think sms verification is better solution),any way I improved your solution with integrate it with block-disposable-emai,have simple JSON BASE APIs
   /**
 * Custom validation function from the Domain Registration module (1.0).
 *
 * Checks if the domain in the email address is on a list of blocked domains.
 * @param $form
 * @param $form_state
 */

function MYMODULE_domain_registration_user_register_validate(&$form, &$form_state) {
  $default_message = t('You are not allowed to register for an account on this site.  Contact support for more information.');
  $mail = explode('@', $form_state['values']['mail']);
  $exact_domains = array(
    'ad.drupaler.org',
    'drupaler.org',
  );
  $partial_domains = array(
    'pp.ua',
    'guerrilamail',
  );

  $key     = '12345abcapikey'; //please get your own valid api key
  $request = 'http://check.block-disposable-email.com/api/json/'.$key.'/'.$mail[1];
  if (isset($mail[1])) {
  $response = file_get_contents($request);
  $dea = json_decode($response);
  if ($dea->request_status == 'success'){
    if ($dea->domain_status == 'block' && $dea->domain_type == 'dea_provider' && $dea->domain_is_handpicked == 1){
    // deny registration ...
              form_set_error('account', variable_get('domain_registration_message', $default_message));

    }

  }else{
    form_set_error('account', "try again");
   }
  }
  }

Answer: I'm facing the same problems so I create a Free API service that recognize disposable and temporary email address: Block Temporary Email
Email Endpoint:
GET https://firstname.lastname@example.com
JSON response:
{
  "status": 200,
  "domain": "simplemail.top",
  "temporary": true,
  "dns": true,
}

This API is 100% free and have no limits.
This site provides a free and updated TXT list of disposable email providers too.
Disclaimer: I made this API


How to translate the user profile page in commerce kickstart

Question: Hello,
I'm using commerce kicksart in my website, I use french as source language instead of english, I translated many words in the user profile page, but I can't translate or edit some titles:

-HELLO
-PRIMARY SHIPPING ADDRESS
-PRIMARY BILLING ADDRESS
-RECENT ORDERS

I didn't find them in the translate interface page admin/config/regional/translate/translate, also I didn't find any view or block to edit them.
How I can translate or edit them without touch the tpl file?
Thanks
Comment: I think I already fixed this issue, I'm trying to remember how I did, so I'll put it here as comment.and sure I'll try your solution too
Comment: it's just because the language problem I don't share my research results, it's diffuclt to explain to experienced people. another thing I'm working in many projects -so busy-, I'm seeking to find time to spend it here in stackexchange.
Answer: The solution described in issue #1937982 has been used to solve an issue similar to yours. That issue includes a .PO file to handle at least these strings:

Primary billing address
Primary shipping address

If that would work in your case also, you can probably use the same solution by adding 2 similar lines for these 2 other strings (you may have to verify if it is case sensitive or not):

HELLO
RECENT ORDERS


How to fix Crawled (403)

Question: I am using python 3 and scrapy. I am fetching in scrapy shell with this code:
url = "https://www.urban.com.au/projects/melbourne-square-93-119-kavanagh-street-southbank"
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"

}

fet = scrapy.Request(url, headers=headers)
fetch(fet)

and it's showing DEBUG: Crawled (403)
Please share any idea with return 200 response in scrapy shell.
Answer: 403 error - because website showing a captcha.
If resolve the captcha and extract cookie it will be work.
Example with requests for debug:
import requests

headers = {
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
    'cookie': 'your cookie',
}

response = requests.get('https://www.urban.com.au/projects/melbourne-square-93-119-kavanagh-street-southbank', headers=headers)

Comment: How do you obtain the cookie, I am curious about it.
Answer: If you open it in your browser it's showing fill captcha to proceed. So for high traffic from a computer, it will ask for extra authentication.
Hence you're seeing 403
Answer: headers = {
    'authority': 'www.urban.com.au',
    'cache-control': 'max-age=0',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'dnt': '1',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'sec-fetch-site': 'none',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9',

}

Request('https://www.urban.com.au/projects/melbourne-square-93-119-kavanagh-street-southbank', headers=headers)

You need to mimic exact same header as a real browser does
Comment: No luck! still 403

How to fix Crawled (403)


How to detect mouseclick outside user control?

Question: I have modified a drop down control, and replaced combobox dropdown with datagridview. I want to close datagridview, but if I clicked outside datagridview it may be other control or blank form.
I have tried control leave event but it is only working when I clicked on other textbox or focus-able control.
Comment: UI Controls expose the `Capture` property (no need to PInvoke). When set to `true`, mouse events are notified even when generated outside a Control's bounds. You can subscribe to, for example, the the DGV's `VisibleChanged` event and set `dataGridView1.Capture = dataGridView1.Visible`. If `.Visible = true` add a handler to `MouseLeave`, which, when triggered, unsubscribes itself and sets `.Capture = true;`. In the `MouseDown` event, you can have something like: `if (!dataGridView1.ClientRectangle.Contains(e.Location)) { dataGridView1.Visible = false; }`. It will also release the Capture.
Comment: If you set `.Visible = true` to show your DGV, then in the `VisibleChanged` event. Use a typed event handler for the `MouseLeave` event , don't use a Lambda and don't subscribe to it in the Designer. So, in `VisibleChanged` you can have: `dataGridView1.Capture = dataGridView1.Visible; if (dataGridView1.Visible) { dataGridView1.MouseLeave += dataGridView1_MouseLeave; }` and in the `MouseLeave` handler: `dataGridView1.MouseLeave -= dataGridView1_MouseLeave; dataGridView1.Capture = true;`.
Comment: You can also handle the `Leave` event, if you want to hide the DGV as soon as a User tabs out of it. Same logic: `private void dataGridView1_Leave(object sender, EventArgs e) { dataGridView1.MouseLeave -= dataGridView1_MouseLeave; dataGridView1.Visible = false; }`. You can subscribe to this event in the Designer.
Comment: Look up the Win32 concept of `Capture` (using `SetCapture` and `ReleaseCapture`).
Comment: @Jimi I did not get " If .Visible = true add a handler to MouseLeave, which, when triggered, unsubscribes itself and sets .Capture = true;" in which event I have to use it
Comment: thank you so much @Jimi mouse-click captured successfully. cheers :)
Answer: You will need a Low-Level Mouse Hook to catch Mouse clicks outside of you app.
Here is a library that will help you: https://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library or https://github.com/charygao/MouseKeyboardLibrary (They are the same)
Example Code (make sure you include the GlobalHook.cs and MouseHook.cs):
using MouseKeyboardLibrary;

//....
//...

MouseHook mHook = new MouseHook();

public void Form1_Show()
{
    mHook.MouseDown += new MouseEventHandler(SomeMethod);
    mHook.Start();
}

public void SomeMethod(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Left)
    {
        //do something
    }
}

Comment: This works in .Net Framework 4.5, because it uses Win32 API Methods
Comment: When i tried this i also had trouble at first. If you want extra help you can contact me
Comment: And make sure you have System.Windows.Forms added as assembly


How many tennis balls are in use at the same time?

Question: How many tennis balls does a player have to choose from at a time?
I know every 7 games they play with new balls, but I do not know how many balls are in use at the same time.
Is the rule the same for ATP/WTA matches and Grand Slam matches?
Answer: New balls are requested by the chair umpire after the first 7 games of the match, and then every 9 games thereafter. It is done this way since the players warming up with the first set of new balls is considered to amount to the normal wear and tear of 2 games.
When the chair umpire calls for new balls, the ball kids (if present) will collect the balls and put them back into the empty cans they came from and (usually) put those cans back into a case (cardboard box) sitting beneath or to the side of the chair umpires chair. If no ball kids are present, lines persons will usually perform this task. These "used" balls typically become practice balls that players can request to borrow for practice after they are brought back in from the courts after that days matches are completed.
To answer the question - each round of new balls typically consists of two cans of three balls, so six balls in total. I don't know why six balls - but that has been the number for a few decades now.
In watching matches, you might see some players be particular about each ball kid holding an even number of balls - so they will ask for a ball from one kid just to hit it over to the others so they don't have to later worry about which ball kid they can ask for balls from after the point is over.
If one of the six balls gets taken out of play (either hit out of the court or over a fence, into the seats and not thrown back, or gets broken during play, etc.) it is not usually replaced and the players will just play with one less ball until the next round of new balls.
Edit: To answer the edited question: yes, these same rules are followed by all ATP/WTA and Grand Slam matches. The ATP/WTA adopted their practices from the Grand Slams.


How to make an "Add more logins" similar to Stackoverflow?

Question: I am using OpenID Selector module to integrate OpenID logins to my website; the same service used by Stackoverflow for login/registration. However, how can I add an option to "Add more logins" like Stackoverflow? I am open to suggestions also.
The idea is to prevent duplicate users. I am using D7.
See attached image.
Also kind of tweaks are welcome.
Comment: If there is no working modules, i am open to suggestions, and if i get a perfect logic; i pledge you will get a D7 module hosted in drupal.org for sure.
Comment: Complete one day of inactivity, i am disappointed. Its a rare thing in Stackoverflow. !!
Comment: I know there is Janrain module that integrates multiple login, but I have no idea how or even if they deal with duplicates at all.
Comment: @NikhilM are you putting a sandbox project for this or what? I'll be happy to review it :-)
Comment: @indrock, Unfortunately my client dropped the concept. I am planning to make something similar but not recently. I am pretty busy.
Answer: So the default assumption most Drupal modules make with authentication is that they will replace the current login setup with their own.
This approach is described well in http://drupal.org/node/114774#dist-auth and can be seen in how the current openid module provided in Drupal core works.
If I understand your question correctly you want to provide a way for people to authenticate from multiple different sources and have those enabled sources show up under their user profile. I don't think there is a module that does this yet, as most of the auth modules operate in their own little bubble as described above.
After a quick look at OpenID Selector you aren't going to get a lot of mileage from there, its mainly implementing and configuring the jquery library. You are going to need to write this from scratch.
The main pieces of your module will be:

A database table that maintains the logins associated to a given
user.
Some kind of form_alter scheme for the login page to show
all of the available login options the site owner has specified
(this will replace what the individual modules currently do)
An admin page for the site owner to select from available options.

Each one of those login options will probably be provided by a separate contrib module, so you might have to alter your logic for the specifics you find in the implementation from those modules.
To me its an ambitious undertaking for a configurable module that you plan to release, but if you go the quick and dirty highly customized / hard coded route for a single site it shouldn't take you too long.
Other places to look for inspiration on how auth modules operate in Drupal and possible sources you might want to include are FBOauth and Twitter.
Comment: Thanks for such a detailed answer. I need it anyway. So lets do it myself at least it will help me.
Answer: Does the HybridAuth Social Sign-on not fit your requirements?

This module integrates HybridAuth library into Drupal and allows your users to login and register using Google, Facebook, Twitter, Yahoo, MySpace, Windows Live, LinkedIn, Foursquare, AOL, OpenID and more.
  Additional providers include: Github, Gowalla, LastFM, Vimeo, Disqus, Instagram, Viadeo, Identica, Tumblr, Goodreads, QQ, Sina, Murmur, Pixnet, Plurk, Skyrock, 500px, Vkontakte, Mail.ru, Yandex, Odnoklassniki.

I have not used it myself, but reading the description and screenshots seems to suggest it does what you need.
Update: Looking at the Usage statistics it seems like the module got picked up only recently and might not have been usable when you posted this question.
Comment: If this module works good... which seems to be the case... you save me about 20 or 40 hours of work jaja thanks!


Get the CCK field value using dbquery

Question: I have a content type called user registration under this one cck field called regid. i would like to fetch the entered values of this particular field through programmatically probably by using dbquery() functions and print it. Anyone guide me how to achieve it.
Comment: possible duplicate of [Programmatically get field values](http://drupal.stackexchange.com/questions/27293/programmatically-get-field-values) - i know it's not about db_query, but you wrote *probably*. And field_get_items is much better way to do it.
Answer: Here is a little example, how to make requests in Drupal 6. You may change table name and field name and it should work for you.
<code>$query = db_query('SELECT n.nid, regid.regid_FIELD_VALUE
   FROM {node} AS n
   JOIN {regid_FEILD_TABLE_NAME} AS regid ON regid.vid = n.vid
   WHERE n.type = %s AND n.status = %d, $your_node_type, 1);

$result = array();
while($node_data = db_fetch_object($query)) {
  $result[] = array(
    'nid' => $node_data->nid,
    'regid' => $node_data->regid_FIELD_VALUE,
  );
}
</code>
Comment: Can you please guide me how to done the same thing in drupal 7.
Comment: http://drupal.stackexchange.com/questions/12515/entityfieldquery-vs-db-select

Get the CCK field value using dbquery


Putting any fields of referenced entity in lightbox frame

Question: I want output a field as a link to an overlay that has fields of a referenced entity in it.
I can output the actual link just fine as long as it doesn't have to be an overlay.
Content type A is called 'performance', content type B is 'performer' (to which is being referenced).
So, for now, in my view there are only two fields: 'Performer name'(of content type B also the title) and 'title' (of content type A). 'Performer' is arranged above 'title' and display set to none. 'Title' has 'ouput field as link' checked and has the token in the URL that is the title of 'Performer'.
So this makes the link that is the title in the view go to the node of the performer that is referenced in the 'Performance' node.
I know that in the 'rewrite results' fieldset, I can fill in 'lightbox2' in the 'rel' attribute of my link. When I do this though, the lightbox shows, but is empty. No image, no values of any field, no nothing.
Answer: This doens't necessarily have to be done using lightbox. It ended up being a lot easier using a jQuery plugin like Simple Modal
I made extra fields in views and gave them a class the jQuery could latch on to using
$('hideMe').hide;

and using jQuery's click(); method to go and say (this).modal
A different question here at Drupal Answers went into the same issue but it was more about how to write the jQuery code so definitely more of a jQuery question. Anyway the question with the exact code can be found here.
Answer: The rel attribute should be "lightframe". It is used for HTML elements you want to open in lightbox.


Fixing Linefeed breaks

Question: I have a pipe delimited text file that comes on a daily basis with line breaks as a result of an online feed interruption.
Every line in the file starts with FORM_, so i normally open the file in notepad++, use the find and replace for either ^[^FORM] or ([^\S \r\n]*)\n([^\S\r\n]*) and manually fix the mistake.
Due to limitation in technology where I work, I am writing a vb script, that will end up as an executable, to among other things fix this issue.
I have looked on here for some possible answers and below is what I tried:
Reading the file one line at a time, then using regex to replace either of the above search criteria:
objFixFormFile = objFSO.OpenTextFile(FileLocation & filename, 1)
objWriteFormFile = objFSO.CreateTextFile(FileLocation & NewFileName, True)
Do while not objFixFormFile.AtEndOfStream
    If strReader = "" THEN
        strReader = objFixFormFile.ReadLine
    Else
        strReader = strReader & ";" & objFixFormFile.ReadLine
    End if
Loop
ObjFixFormFile.Close
strCleansedLine = RegEx.Replace(strReader, ("[^\S \r\n]*)\n([^\S\r\n]*)", "")
strCleansedLine = RegEx.Replace(strReader, ";", "\n")
objWriteFormFile.Writeline(strWriter)

That didn't work. When I try to do the .ReadAll version of the above code out of 125,000 records I only end up with 59,000 records.
Can you please advise what is the best way of doing this, as it needs to run on a daily basis?
Comment: Can you add some failing samples and the part of the code where the RegEx is instantiated?
Comment: First: In the two `Replace` at the bottom you use `strReader` as input to both - shouldn't the second one have `strCleansedLine`? Secondly the same goes for the `WriteLine` statement - shouldn't that have the result of the second `Replace` (also `strCleansedLine`)?
Answer: Only write the linebreak for the previous line if the current line starts with FROM_:
Do Until objFixFormFile.AtEndOfStream
    line = objFixFormFile.ReadLine
    If Left(line, 5) = "FROM_" And objFixFormFile.Line > 2 Then
        objWriteFormFile.WriteLine
    End If
    objWriteFormFile.Write line
Loop
objWriteFormFile.WriteLine

objFixFormFile.Close
objWriteFormFile.Close

Fixing Linefeed breaks


What is the space, on which a random variable is defined?

Question: Well, I have some simple, maybe silly question about random variables, but there is something that I can not undestand when we define them. Suppese that, we have some random variable $X$, that is defined in a standard probability space $(\Omega,\mathcal{F}_s,\mathbb{P})$, where $\mathbb{P}:\mathcal{F}_s\rightarrow [0,1]$. I struggle to undestand which is the space , that this random variable is defined. Specifically, the random variable $X$ is a function $X:\Omega\rightarrow R$, where R is some arbitrary space and probabyly the real line.
Can we claim that $X\in L^2(\Omega,\mathcal{F}_s,\mathbb{P})$ or this is wrong? How can we know indeed where this random variable belongs to?
Maybe my whole skeptic is wrong, so forgive me in advance, but I am a begginer, who wants to understand, this mathematical theory!
Comment: It is $X:\Omega\rightarrow\mathbb{R}$.
Comment: @Michael You are right!
Answer: Here's a few of the basic definitions.
Given your setup, a random variable is a function $X : \Omega \to \mathbb R$, that is, it is a function whose domain is $\Omega$.
But not just any old function from $\Omega$ to $\mathbb R$ is allowed as a random variable. There is a requirement, namely that for every open interval $(a,b) \subset \mathbb R$, the set $X^{-1}(a,b) = \{\omega \in \Omega \mid X(\omega) \in (a,b)\}$ is an element of the $\sigma$-algebra $\mathcal F_s$. In other words, $X^{-1}(a,b)$ is required to be an event.
Because of this requirement, we can define the expectation of a random variable $X$ as an integral, namely
$$E(X) = \int_\Omega X \, d \mathbb P
$$
Now as I have stated this so far, that integral might be infinite. So, one might wish to impose one more requirement on the definition of a random variable, namely that $E(X)$ is finite. This would be equivalent to requiring that $X \in L^1(\Omega, \mathcal F_s, \mathbb P)$.
Comment: It looks like you are thinking about a [stochastic process](https://en.wikipedia.org/wiki/Stochastic_process#Definitions), which is a deeper concept that a random variable. But that's another question.
Comment: That sounds about right.
Comment: thank you for your answear, it is very helpful, but I have something in mind that I want to make it clear a little further. Usually, I think that we define random variables $X(\omega,t)$ where we say that if we fix time, then it is a finction of the sample space $\Omega$, we can have in mind a Brownian motion, or if we fix it as function of time $t$, where as we know $t\in [0+\infty)$. Is this completely different from my question in the beginning?
Comment: yes you are right, I just figured out the same think! Well in such a case  stochastic process is some $X\in L^2(\Omega,\mathcal{F}_s,\mathbb{P})$
Comment: so to understand the concept in depth, if a function is a random variable and it has a finiete integral then, we can say that it holds $X\in L^1(\Omega,\mathcal{F}_s,\mathbb{P})$. In addition to this, if the random variable is a square integrable function, then it holds that it also has a finite variance and as a consequence $X\in  L^2(\Omega,\mathcal{F}_s,\mathbb{P})$ or $\mathbb{V}ar(X)+\int_{A}(X-\mathbb{E}(X))f(X)dX<\infty$, $A\in\Omega$. Am I right?


i<=: Expression is not complete; more tokens expected. ksh

Question: i get this error: i<=: Expression is not complete; more tokens expected. This is the code:
vr=`$line`
set sep_mx=`echo $vr | awk '{
    n=split($0,x,"@#;")
    print n
}'`

echo $sep_mx
i=1 && while ((i<=$sep_mx))
do
    echo $vr | awk -v er=$i '{
         n=split($0,x,"@#;")
         print x[er]
    }'
    ((i+=1))
done

Anyone can help me? Thanks
Comment: `while [ $i<=$sep_mx ]` maybe?
Comment: Mmh yeah but now i have new error `=: Cannot find or open the file.`
Comment: Execute the script with ksh -xv to track down the error.
Comment: And I think you want `$i <= $sep_mx`
Comment: nope..same error also with `$i <= $sep_mx`
Comment: For integer comparision in `[...]` (or `[[...]]`), use `-le`. The `<` is interpreted as a redirection operator.
Comment: Are you sure `sep_mx` has a value?
Answer: ok , got that thing working.
Logic : code that would take a string , break it into words based on a defined delimiter and put each as a array element.
To make things a bit more simple i removed the other two delim option (# and ;) specified in your question.
working code :
Kaizen ~/so_test $ cat zawk1.sh
echo "entter a line : " ;
read line ;

vr="$line";

      sep_mx=`echo $vr| awk '{ n=split($0,x,"@"); print n }'` ;
      echo $sep_mx ;  ## here the number of substring produced is calc.

      while [ $i -le $sep_mx ]
      do
          ## would print individual substring one at a time as the counter increases.
          echo $vr | awk -v er=$i '{ n=split($0,x,"@"); print x[er] }' ;
          i=$(( $i + 1 ));
      done

output :
 Kaizen ~/so_test $ ./zawk1.sh
 entter a line :
 hello@world@how@are@you
 5
 hello
 world
 how
 are
 you

hope this helps !!
Comment: helps me a lot thanks. Another one thing, if i would extract for example only the first line? In this case "hello".
Comment: ` echo "hello@world@how@are@you" | awk '{ n=split($0,x,"@"); print x[1] }' `
Answer: To answer the original question, the error message already
explains where the error occurs:
In i<= in i<=: Expression is not complete; more tokens expected. is the result of the expansion of the contents of ((i<=$sep_mx)).
This would happen if $sep_mx contained an empty string, which in turn, would be the consequence of
vr=`$line`

resulting in a vr that does not contain the delimiter string you include in the split() function in the awk line.
Note that awk split()'s third argument is a regular expression.  If you want to split on any of the characters in "@#;", you should turn it into a character set by enclosing it in square brackets.
Compare:
$ echo "@fee#fie@foe" | awk '{ print split($0,x,"[@#;]") }'
4

With:
$ echo "@fee#fie@foe" | awk '{ print split($0,x,"@#;") }'
1


Utility of the Frisch-Waugh theorem

Question: I am supposed to teach the Frish Waugh theorem in econometrics, which I have not studied.
I have understood the maths behind it and I hope the idea too "the coefficient you get for a particular coefficient from a multiple linear model is equal to the coefficient of the simple regression model if you "eliminate" the influence of the other regressors".
So the theoretical idea is kind of cool. (If I totally misunderstood I do welcome a correction)
But does it have some classical/practical usages ?
EDIT : I have accepted an answer, but am still willing to have new ones that bring other examples/applications.
Comment: An obvious one would be [added variable plots](http://stats.stackexchange.com/q/125561/22228)?
Comment: I have exploited this relationship in many posts, primarily for conceptual purposes and to provide interesting examples of regression phenomena.  See, *inter alia*, http://stats.stackexchange.com/a/46508, http://stats.stackexchange.com/a/113207, and http://stats.stackexchange.com/a/71257.
Comment: Dougherty's *Introduction to Econometrics* mentions another example of using the Frisch-Waugh-Lovell theorem. In the early days of econometric analysis of time series, it was quite common in models where variables had deterministic time trends to detrend them all before regressing. But by FWL, you get the same coefficients simply by including a time trend as a regressor, and moreover this gives the "correct" standard errors, since it acknowledges that 1 df has thereby been consumed.
Comment: Dougherty does warn against the procedure, so in that respect it's not a great example, even though it's an instructive one. Economic variables often seem to be difference-stationary rather than trend-stationary, so this kind of attempted detrending doesn't work and can result in spurious regressions.
Comment: @Silverfish: FWL is a purely algebraic technique, so the issue of whether extracting a deterministic trend is "right" given the underlying DGP is no doubt important, but imho unrelated to FWL, so in that sense your example is a perfectly valid one for OPs question about the two ways to obtain point estimates.
Comment: @ChristophHanck Thanks - yes I agree. I think Dougherty's point was that this used to be a really  practically important application of FWL and is now somewhat less so. I was thinking of adding it as an answer but I wonder if it essentially duplicates yours (albeit somehow "in reverse"!).
Answer: Consider the fixed effects panel data model, also known as Least Squares Dummy Variables (LSDV) model.
$b_{LSDV}$ can be computed by directly applying OLS to the model $$y=X\beta+D\alpha+\epsilon,$$
where $D$ is a $NT\times N$ matrix of dummies and $\alpha$ represent the individual-specific fixed effects.
Another way to compute $b_{LSDV}$ is to apply the so called within transformation to the usual model in order to obtain a demeaned version of it, i.e. $$M_{[D]}y=M_{[D]}X\beta+M_{[D]}\epsilon.$$
Here, $M_{[D]}=I-D(D'D)^{-1}D'$, the residual maker matrix of a regression on $D$.
By the Frisch-Waugh-Lovell theorem, the two are equivalent, as FWL says that you can compute a subset of regression coefficients of a regression (here, $\hat\beta$) by

regressing $y$ on the other regressors (here, $D$), saving the residuals (here, the time-demeaned $y$ or $M_{[D]}y$, because regression on a constant just demeans the variables), then
regressing the $X$ on $D$ and saving the residuals $M_{[D]}X$, and
regressing the residuals onto each other, $M_{[D]}y$ on $M_{[D]}X$.

The second version is much more widely used, because typical panel data sets may have thousands of panel units $N$, so that the first approach would require you to run a regression with thousands of regressors, which is not a good idea numerically even nowadays with fast computers, as computing the inverse of $(D :X)'(D: X)$ would be very expensive, whereas time-demeaning $y$ and $X$ is of little cost.
Comment: Thanks a lot, this is the kind of answer I was looking for, even though it is a bit advanced for me to actually use it. So your answer is fine with me, but I would be happy if I have other ones, am I supposed to accept yours ?
Comment: If it helped it would be appropriate to do so. But accepting will reduce your chances of getting better answers, so you may consider to wait before accepting this one. A bounty would further increase your chances of getting more answers - given that there are not enough users on CV who regularly answer questions given the amount of questions, even a single answer may lead other active users to conclude that the questions has been dealt with. (I did post a somewhat simpler answer below.)
Answer: Here is a simplified version of my first answer, which I believe is less practically relevant, but possibly easier to "sell" for classroom use.
The regressions $$y_i = \beta_1 + \sum_{j=2}^K\beta_jx_{ij} + \epsilon_i$$ and $$y_i-\bar{y} = \sum^K_{j=2}\beta_j(x_{ij} - \bar{x}_j) + \tilde{\epsilon}_i$$ yield identical $\widehat{\beta}_j$, $j=2,\ldots,K$.
This can be seen as follows: take $\mathbf{x}_1=\mathbf{1}:=(1,\ldots,1)'$ and hence
$$
M_\mathbf{1}=I-\mathbf{1}(\mathbf{1}'\mathbf{1})^{-1}\mathbf{1}'=I-\frac{\mathbf{1}\mathbf{1}'}{n},
$$
so that
$$M_{\mathbf{1}}\mathbf{x}_j=\mathbf{x}_j-\mathbf{1} n^{-1}\mathbf{1}'\mathbf{x}_j=\mathbf{x}_j-\mathbf{1}\bar{x}_j=:\mathbf{x}_j-\bar{\mathbf{x}}_j.
$$
Hence, the residuals of a regression of variables on a constant, $M_{\mathbf{1}}\mathbf{x}_j$, are just the demeaned variables (the same logic of course applies to $y_i$).
Answer: Here is another, more indirect, but I believe interesting one, namely the connection between different approaches to computing the partial autocorrelation coefficient of a stationary time series.
Definition 1
Consider the projection
\begin{equation}
\hat{Y}_{t}-\mu=\alpha^{(m)}_1(Y_{t-1}-\mu)+\alpha^{(m)}_2(Y_{t-2}-\mu)+\ldots+\alpha^{(m)}_m(Y_{t-m}-\mu)
\end{equation}
The $m$th partial autocorrelation equals $\alpha^{(m)}_m$.
It thus gives the influence of the $m$th lag on $Y_t$ \emph{after controlling for} $Y_{t-1},\ldots,Y_{t-m+1}$. Contrast this with $\rho_m$, that gives the `raw' correlation of $Y_t$ and $Y_{t-m}$.
How do we find the $\alpha^{(m)}_j$? Recall that a fundamental property of a regression of $Z_t$ on regressors $X_t$ is that the coefficients are such that regressors and residuals are uncorrelated. In a population regression this condition is then stated in terms of population correlations. Then:
\begin{equation}
E[X_t(Z_t-X_t^\top\mathbf{\alpha}^{(m)})]=0
\end{equation}
Solving for $\mathbf{\alpha}^{(m)}$ we find the linear projection coefficients
\begin{equation}
\mathbf{\alpha}^{(m)}=[E(X_tX_t^\top)]^{-1}E[X_tZ_t]
\end{equation}
Applying this formula to $Z_t=Y_t-\mu$ and $$X_t=[(Y_{t-1}-\mu),(Y_{t-2}-\mu),\ldots,(Y_{t-m}-\mu)]^\top$$ we have
$$
E(X_tX_t^\top)=\left(\begin{array}{cccc}
                          \gamma_{0} & \gamma_{1}&\cdots& \gamma_{m-1}\\
                         \gamma_{1}& \gamma_{0} & \cdots &\gamma_{m-2}\\
                         \vdots & \vdots & \ddots &\vdots\\
                         \gamma_{m-1}&\gamma_{m-2} & \cdots & \gamma_{0}\\
                       \end{array}
                     \right)
$$
Also,
$$
E(X_tZ_t)=\left(
                              \begin{array}{c}
                                \gamma_1 \\
                                \vdots \\
                                \gamma_m \\
                              \end{array}
                            \right)
$$
Hence,
\begin{equation}
\mathbf{\alpha}^{(m)}=\left(\begin{array}{cccc}
                          \gamma_{0} & \gamma_{1}&\cdots& \gamma_{m-1}\\
                         \gamma_{1}& \gamma_{0} & \cdots &\gamma_{m-2}\\
                         \vdots & \vdots & \ddots &\vdots\\
                         \gamma_{m-1}&\gamma_{m-2} & \cdots & \gamma_{0}\\
                       \end{array}
                     \right)^{-1}\left(
                              \begin{array}{c}
                                \gamma_1 \\
                                \vdots \\
                                \gamma_m \\
                              \end{array}
                            \right)\end{equation}
The $m$th partial correlation then is the last element of the vector $\mathbf{\alpha}^{(m)}$.
So, we sort of run a multiple regression and find one coefficient of interest while controlling for the others.
Definition 2
The $m$th partial correlation is the correlation of the prediction error of $Y_{t+m}$ predicted with $Y_{t-1},\ldots,Y_{t-m+1}$ with the prediction error of $Y_{t}$ predicted with $Y_{t-1},\ldots,Y_{t-m+1}$.
So, we sort of first control for the intermediate lags and then compute the correlation of the residuals.

Utility of the Frisch-Waugh theorem


Two formulas for complex inner product: Which formula is true?

Question: I am a bit confused regarding the correct formula for the complex inner product of two complex vectors. The textbook I learnt from defines the complex inner product of the vectors: $u$ and $v$ as follows: $$\left ( cu,v \right )=\bar{c}\left ( u,v \right )$$ and $$\left ( u,cv \right )=c\left ( u,v \right )$$ where $c$ is a scalar.
However, in a different textbook, I found the following convention: $$\left ( cu,v \right )=c\left ( u,v \right )$$  and $$\left ( u,cv \right )=\bar{c}\left ( u,v \right )$$
Which one of the above formulas is the one I should use?
Also, for the complex dot product: In some textbooks, I found: $$u\cdot v=\bar{u_{1}}v_{1}+\bar{u_{2}}v_{2}+\cdots+\bar{u_{n}}v_{n}.$$
In other textbooks, I found the following: $$u\cdot v= u_{1}\bar{v_{1}}+u_{2}\bar{v_{2}}+\cdots+u_{n}\bar{v_{n}}$$

Which one I should as the definition of the complex dot product? Also, which one is equivalent to $u^{*}v$?
Comment: Either one works; it depends on your context. Mathematicians prefer the second one (linear in the first coordinate, conjugate linear in the second). I believe physicists prefer the former (linear in the second coordinate, conjugate linear in the first coordinate). So long as you adapt formulas appropriately, they give you the same results.
Comment: If you write your vectors as columns, $u=(u_1,\ldots,u_n)^t$ and $v = (v_1,\ldots,v_n)^t$, then $u^* = (\overline{u_1},\ldots,\overline{u_n})$, so $u^*v$ is given by your first definition of "complex dot product."
Answer: Unfortunately, there are two different conventions floating around out there. Either one is correct, but you need to be very careful that you define everything which depends on the inner product in a manner consistent with the particular definition of inner product you are using. For example, if you use your first definition of inner product then $$u\cdot v=\sum_{i=1}^n\langle u_ie_i,v_ie_i\rangle = \sum_{i=1}^n \bar{u_i}\cdot v_i$$
where $e_1,\ldots,e_n$ is the standard basis, while if you use the second then $$u\cdot v=\sum_{i=1}^n\langle u_ie_i,v_ie_i\rangle = \sum_{i=1}^n u_i\cdot \bar{v_i}$$
and similar modifications have to be made to other definitions and theorems. The moral: be sure to check what definition your professor and textbook use (hope that they are the same!) and if you every become a brilliant mathematician and invent some new math, insist on just one convention.
Answer: "True" is not the right word to use here. The two conventions are interchangeable, since given any inner product of the first kind defining $(u, v)_{\text{new}} = (v, u)_{\text{old}}$ gives an inner product of the second kind. So it doesn't matter which one you use, really, as long as you are consistent.
Nevertheless, I would like to argue that the convention where the inner product is conjugate-linear in the first variable is the "better" one to use. One bit of evidence comes from the fact that $\dim \text{Hom}(V, W)$ naturally appears in the representation theory of finite groups, and it gives an inner product on characters which is conjugate-linear in the first variable. The discussion in the beginning of Baez's Higher-Dimensional Algebra II: 2-Hilbert Spaces is relevant.


How to send email in drupal

Question: I have drupal site in that ,i want to send a email to charities when donor post a donation..,and i want to send a email notification to charities,so how i send email using drupal7
$message = array(
  'to' => '"'. addslashes(mime_header_encode('new donation')) .'" <email@example.com>';
  'subject' => t('Example subject'),
  'body' => "Hi user! a donor added a new donaion! So please     check the site for more infomation!
Thanks,
",
  'headers' => array(
          'From' => 'firstname.lastname@example.com',
          'MIME-Version' => '1.0',
          'Content-Type' => 'text/html;charset=utf-8',),
);

drupal_mail_send($message);

Comment: "when the donor posts a donation" can you explain more about it. The donation is a node or your customized data in your own table.
Comment: Ok i'll write down the answer for you now.
Comment: actulty donation is a node and alos a custom content type,but i have develop that part i want to send the email and i want kown,
Comment: Have you solved your problems yet ?
Answer: First you must enable the following modules:
1. Mailsystem module  http://drupal.org/project/mailsystem
2. Mimemail module http://drupal.org/project/mimemail (it contains the mail actions and sending mail function) or you can use htmlmail module  http://drupal.org/project/htmlmail
3. Rules module http://drupal.org/project/rules
After that you will do some steps:
1. Create new rule in admin/config/workflow/rules/reaction/add and select the "React on event" is "After saving new content".
2. Add a condition. Choose Node (Content is of type) for condition and choose your custom content type in the "Content types" list box.
3. Add another condition.  Choose User(User has roles()). Choose data selector is "node:author" and choose the role of donor.
4. Add the action. Choose System (Send HTML email). You'll change the To, CC, subject, content here.
5. Try to test that.
Be sure that you can send email using your system. If you use the htmlmail module. It will have the send test email in its setting.
Answer: You may use Rules module to send email automatically.
The rules module allows site administrators to define conditionally
executed actions based on occurring events (known as reactive or ECA rules).

How to send email in drupal


commerce_line_item VS field_data_commerce_line_items

Question: Right now, I am touching the database for drupal 7. I searched about the difference between "commerce_line_item" table and "field_data_commerce_line_items" table. But no luck.
Does anyone know the difference between them? Also, is there any reference that I can follow to check the table explanation?
I really appreciate anyone's help.
UPDATE
Based on everyone's help and my own opinion, I drew a simple diagram for these two tables:
Answer: My answer concerns the structural and not the semantic difference between the two.
The table field_data_commerce_line_items corresponds to a standard drupal field. On the other hand commerce_line_item is a table that is managed by one of the modules in the commerce module family.
The table field_config_instance describes the relationships between drupal fields (which are actually tables such as field_data_commerce_line_items) and higher level entity types (i.e. node) and bundles (i.e. content type) that reference them.
Answer: commerce_line_item is the base table of entity with same name. The entity is defined from the sub module commerce_line_item in Commerce project. You can find the definition from its code repository
commerce_line_item.module
/**
 * Implements hook_entity_info().
 */
function commerce_line_item_entity_info() {
  $return = array(
    'commerce_line_item' => array(
      'label' => t('Commerce Line item'),
      'controller class' => 'CommerceLineItemEntityController',
      'base table' => 'commerce_line_item',
      'fieldable' => TRUE,
      'entity keys' => array(
        'id' => 'line_item_id',
        'bundle' => 'type',
        'label' => 'line_item_id', // TODO: Update to use a custom callback.
      ),
      ...

commerce_line_item.install
function commerce_line_item_schema() {
  $schema = array();

  $schema['commerce_line_item'] = array(
    'description' => 'The base table for line items.',
    'fields' => array(
      'line_item_id' => array( ... ),
      'order_id' => array( ... ),
      'type' => array( ... ),
      'line_item_label' => array( ... ),
      'quantity' => array( ... ),
      'created' => array( ... ),
      'changed' => array( ... ),
      'data' => array( ... ),
    ),

    ...

field_data_commerce_line_items is a field created by commerce_order, which is another sub module in Commerce module during installation. You can find this behaviour here
commerce_order.module
/**
 * Implements hook_modules_enabled().
 */
function commerce_order_modules_enabled($modules) {
  commerce_order_configure_order_fields($modules);

  ...
}

/**
 * Ensures the line item field is present on the default order bundle.
 */
function commerce_order_configure_order_type($type = 'commerce_order') {
  // Look for or add a line item reference field to the order type.
  $field_name = 'commerce_line_items';
  commerce_activate_field($field_name);
  field_cache_clear();

  $field = field_info_field($field_name);
  $instance = field_info_instance('commerce_order', $field_name, $type);

  if (empty($field)) {
    $field = array(
      'field_name' => $field_name,
      'type' => 'commerce_line_item_reference',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'entity_types' => array('commerce_order'),
      'translatable' => FALSE,
      'locked' => TRUE,
    );
    $field = field_create_field($field);
  }

  ...

Please note that the field type here is commerce_line_item_reference, which is defined in commerce_line_item module as well.
commerce_line_item.install
/**
 * Implements hook_field_schema().
 */
function commerce_line_item_field_schema($field) {
  if ($field['type'] == 'commerce_line_item_reference') {
    return array(
      'columns' => array(
        'line_item_id' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => FALSE,
        ),
      ),
      'indexes' => array(
        'line_item_id' => array('line_item_id'),
      ),
      'foreign keys' => array(
        'line_item_id' => array(
          'table' => 'commerce_line_item',
          'columns' => array('line_item_id' => 'line_item_id'),
        ),
      ),
    );
  }
}

According to the schema defined above, commerce_line_item_reference acts exactly similar as a entity reference, which helps each order can be connected to a number of line items.
Comment: I know it's a long time ago topic. But right now, I am a little confused that which table will actually store orders' information?
Comment: @jmu What kind of information you want from order?
Comment: I think I got it. I want some detailed information. commerce_line_item is the table I need to look at.
Comment: @jmu cool. have a Merry Christmas!


Baseline hazard function is the hazard function obtained when all covariates are set to zero

Question: I am trying to learn Cox proportional hazard model but I have hit a wall with the basehaz function.
Lets suppose for example I have some data that I want to use but there is a column such as BMI. The BMI has 3 levels: Underweight, normal weight and fat. How could one of these levels be set to zero?
In addition lets suppose I have a continuous variable such as IQ. Does that mean the model wants to take the base for the IQ to be 0?
Comment: Would it work to use -1, 0 and 1?
Comment: the baseline hazard can be defined at the _mean_ values (i.e. not strictly zero), but your covariates in the equation for the log-partial-hazards will be centered now (i.e. x - mean(x)). Related: See the "centered" param in https://www.rdocumentation.org/packages/survival/versions/2.44-1.1/topics/basehaz.
Comment: Actually, and this is important, the `basehaz` by default a: returns the *cumulative* hazard function and b: *centers* all covariates (regardless of whether they were dummy encoded). The main response is right: if you set `centered=F` as an option to `basehaz`, then you will get the BL hazard function for the referent group. It is also related to the survival by $\exp(-\Lambda(t))$
Answer: 1) There are different ways to include categorical variables into the analysis. The most popular and default in most packages is so called
dummy coding where the first category is the "reference" category and the other are code 1 if the observation is from the category and 0 otherwise. Below is an example with R where "a" is the reference category and the columns x1b and x1c would enter the analysis. The coefficients for these variables would than indicate differences compared to category "a".
# create example data set
df <- data.frame(x1 = sample(letters[1:3], 6, replace = TRUE))
df
#>   x1
#> 1  b
#> 2  c
#> 3  a
#> 4  b
#> 5  a
#> 6  c
# dummy coding
cbind(df, model.matrix(~x1, df)[,-1])
#>   x1 x1b x1c
#> 1  b   1   0
#> 2  c   0   1
#> 3  a   0   0
#> 4  b   1   0
#> 5  a   0   0
#> 6  c   0   1

2) For the baseline hazard continuous variables have to be set to 0 as you suspect, which is why the baseline hazard often does not have a useful interpretation and not the focus of the analysis. What is done sometimes is to center the continuous variable. That way the baseline hazard corresponds to the mean value.
Created on 2019-06-08 by the reprex package (v0.3.0)
Comment: Note that the details can depend on the software package used for the survival analysis. For example, SAS uses the last named level of a categorical variable as the reference, unlike R; see the end of [this answer](https://stats.stackexchange.com/a/26540/28500) for example. Also if you use the `basehaz()` function in R at default settings the result is provided for a situation with all predictors at their average values across the dataset. This makes no sense for categorical predictors, as the [authors themselves note](https://cran.r-project.org/web/packages/survival/survival.pdf).


Decide if the next infinite series are convergent or divergent.

Question: I need help with this problem:
Decide if the next infinite series are convergent or divergent. The tools that you need to use are the Leibniz Theorem, the Comparison Test, Quotient Test and the Integral Test.

$\sum_{n=1}^\infty {{\sin\ (n\theta)}\over{n^2}}$
$\sum_{n=1}^\infty (-1)^n {{\log\ n}\over{n}}$

I don't know how to start. Can you please help me and explain me how to decide when to use one of the tests? Thanks.
Answer: $1$. Note that, Every absolutely convergent series is convergent.
Now, $\displaystyle\sum_{n=1}^\infty |\frac{\sin(n\theta)}{n^2}|\le\sum_{n=1}^\infty \frac{1}{n^2}=\frac{\pi^2}{6}$. So, the series $\sum_{n=1}^\infty\frac{\sin (n\theta)}{n}$ is absolutely convergent and hence convergent.
$2$.For the second one we use alternating series test. Set $a_n=(-1)^n\frac{\log n}{n}$ then, $|a_n|=\frac{\log n}{n}$ decreases monotonically as $n>\log n$ for all $n>1$ with $\displaystyle\lim_{n\to \infty}a_n=0$. Hence the series $\sum_{n=1}^\infty (-1)^n\frac{\log n}{n}$ converges.
EDIT: Alternating series test, Absolute convergence, value of $\sum_{n=1}^\infty \frac{1}{n^2}$
Comment: So $|sin(n\theta)|$ is always $\leq 1$?  And then the inequality will remain when we divide by $n^2$, right?
Comment: How did you found that $\displaystyle\sum_{n=1}^\infty |\frac{\sin(n\theta)}{n^2}|\le\sum_{n=1}^\infty \frac{1}{n^2}$? Why did you choose $\sum_{n=1}^\infty \frac{1}{n^2}$? Sorry I find this topic pretty difficult.
Comment: Oh, I see. Recall that $|\sin x|\le 1$ for all $x\in\mathbb{R}$ so each term of the $n$-th partial sum of the above series i.e. $\displaystyle\sum_{n=1}^N |\frac{\sin(n\theta)}{n^2}|$ is less than $\sum_{n=1}^N \frac{1}{n^2}$ for all $N=1,2,...$
Comment: Or, simply $|\frac{\sin(n\theta)}{n^2}|\le\frac{1}{n^2}$ and take finite sum both sides then take the sum to infinite sum.
Comment: absolutely correct. and it is true for all n and for all theta.
Comment: Okay, I see it know. For the second one, why is $\lim_{n\to \infty}a_n=0$? I understand that $|a_n|=\frac{\log n}{n}$ and taking the limit tends to 0, since as you said $n>\log n$ for all n.
Comment: We know that $x\le |x|$ for all real $x$. Now $a_n \le |a_n|\implies \lim_{n\to \infty}a_n \le \lim_{n\to \infty} |a_n|=0$ so the limit on the R.H.S is zero.
Comment: Ok, thanks! Just one more question, how can I see which test method should I use?
Comment: I had asked this question to my teacher long ago he told me to spend more time with series, and it works! I don't have any prominent answer to this question. If you find my answer suitable kindly upvote the answer and accept it.
Comment: Sure. One question, does the second one change if instead of $\log n$ it is $\ln n$?
Comment: In general in calculus: $\log x = \log _{10} x$ and $\ln x = \log_e x$
Comment: Yes, I know, but I wrote it as $\log x$ because my teacher uses $\log x$ as $\ln x$, not as $\log_{10} x$, does it change the way the problem is solved?
Comment: No. It doesn't change the procedure. If someone does not specify the base of the log and he writes $\log$ in calculus we, in general, mean $\ln$. If you still want to proceed, the substitution may help you $\log_{10} x = \frac{\ln x }{\ln 10}$


How to play a piece "cantabile"?

Question: When the performance direction is "cantabile" one must supposedly play it in a song-like manner, but how do I implement it in my playing? Is it just the nature of the piece that will do the work or do I have to add something more to my playing?
Also, technically speaking how can an instrument imitate human voice?
Since I'm learning the violin, what should I do in that context? Do I change something about my bowstrokes? Add a lot of vibrato? Play tempo rubato?
Also, can I do anything the music sheet hasn't directed me to do? For example playing legato is indeed an important component of cantabile playing but I can't slur all the notes, only the ones that are specified by the slur marks. So, simply playing legato (which is already covered by the score) should not be the answer, unless the nature of the piece makes it cantabile and the musician need not add anything to it (which I don't think is the case)
Answer: Well, it is a vague instruction so there is a lot of freedom in interpreting it.
For me, it means making use of the full bow length, being in charge of the expression, developing particularly the long notes (which means that you don't just start them and then some time later they end and nothing happens in between) with an organic quality of dynamic, bow pressure and speed and also an oragnically unfolding decent amount of vibrato (being able to control both its speed and depth rather than just having one kind of involuntary tremor at one's disposal helps).  There might also be a slight amount of rubato where the melodic stresses tend to coincide with some slight prolongation of notes.
And so forth and so on.  Basically the execution is tied to the musicality of the phrases.
Continuous controlled instruments like wind and bellow instruments and bowed instruments have more means at their disposal than percussive instruments like a piano.  So it's also instructive to see what a good piano player does when confronted with "cantabile": most of that will still provide some inspiration to the bigger toolbox of the continuous-control instruments.
Comment: "where the melodic stresses tend to coincide with some slight prolongation of notes." You totally lost me there. Could you please elaborate on that?
Answer: http://www.wisegeek.com/what-does-cantabile-mean.htm says:.

..in order to have a singing quality, the performer needed to execute
  the music with expression, flexibility and an overall sense of
  naturalness. The idea was to convey as much humanness and drama
  through the line as possible, even if the music was not for the voice.

You need to play Legatto and with expression.
Both WiseGeek and http://en.wikipedia.org/wiki/Cantabile says so.
Performance Practice: A Dictionary-Guide for Musicians: A Dictionary-Guide says that it is of moderately slow tempo.
Adams' new musical dictionary of fifteen thousand technical words, phrases says:

... smooth, elegant and replete with feeling.

That sounds like legato to me.
edit:
... but Digging deeper it looks like this answer is be less than accurate... Even though it is supported elsewhere. see comments.
Comment: Well if wikipedia says so who am I to say otherwise.
Comment: Wikipedia says portato is "..,known as articulated legato or slurred staccato"
Comment: @NeilMeyer You might be right after all... the he Harvard Dictionary of Music says that a loure or portato is similar to cantable "...Several notes seperated slightly within a slur." I would consider restoring your answer.
Comment: @amalgamate - if you have found your answer to be wrong, you should delete it. Also, the word is spelled "legato".
Comment: @jjmusicnotes: I'm giving NeilMeyer or who ever it was that posted and deleted the answer that appears to be correct, a chance to restore their correct answer. A kind of courtesy, if you will. Also, there appears an inconsistency in the use of the term, where my answer may be correct sometimes but just not the *most* correct or *most* accurate.

How to play a piece "cantabile"?


New Guitar Sound Check

Question: I have just brought a new Acoustic Guitar. I found that String sounds metallic is this normal ?
Will my Guitar Sound improves gradually i.e Playing become smooth  ?
Currently sliding the finger or changes chord in guitar also produces a metallic sound
Comment: New strings generally do sound more 'jangky' than those that have been 'played in'. Other strings are available - flatwound, etc.
Answer: That's very likely due to different strings than on your reference guitar, that you may have in mind. Factors like material, coating, winding technique, gauges all influence the sound. Maybe experiment with different string sets to find the sound you like.
New strings sound more crisp than older ones. With some materials this effect is more pronounced.
Answer: The natural sound of a particular manufacture and type of strings often needs to be matched up with a particular manufacture and type of guitar to achieve the most pleasing sound. This process gets complicated by the personal preference of the sound produced by matching these things up. Everybody has his own preference, but you should understand that a wooden body on a guitar will have a very difficult time producing a metallic sound without the use of a resonator, so I personally would suspect the problem to be with the strings being used.

New Guitar Sound Check


Programmatically build module permission list

Question: I'm defining a few new roles in the install hook of my module and one of them is an "admin light" role, which will need some, but not all of the admin account permissions.
I need to pragmatically build a list of available permissions from a pre-defined list of modules (User, Node, Toolbar, etc) and assign them to the role. Without having to hard code all of the permission strings.
In Drupal 7 I could do something like:
$modules = array( 'user', 'node', 'toolbar' );
foreach( $modules as $module) {
 $permission_hook = $module . '_permisson';
 if( module_exists( $module ) && function_exists( $permission_hook ) ) {
  user_role_grant_permissions( $rid, array_keys( $permission_hook() ) );
 }
}

I'm struggling to find an solution as neat for Drupal 8 seeing as hook_permssion has been removed in favor of $module.permission.yml. Is there something I'm missing?
EDIT: Final Solution:
This is the final implementation, just in case anyone else need to do this:
  // Define our new roles
  $roles = array(
    array(
      'id' => 'mymodule_client',
      'label' => 'Client',
      'mymodule_permissions' => array(
        'view project status reports',
      ),
    ),
    array(
      'id' => 'mymodule_agent',
      'label' => 'Agent',
      'mymodule_permissions' => array(
        'view projects',
        'view project status reports',
      ),
    ),
    array(
      'id' => 'mymodule_agent_manager',
      'label' => 'Agent Manager',
      'mymodule_permissions' => array(
        'view projects',
        'administer projects',
        'view project status reports',
      ),
    ),
    array(
      'id' => 'mymodule_employee',
      'label' => 'Employee',
      'mymodule_permissions' => array(
        'view projects',
        'administer projects',
        'view project status reports',
        'view project financial reports',
      ),
      // Assign all permissions for the listed modules to this role
      'provider_permissions' => array(
        'user', 'node', 'toolbar'
      ),
    ),
  );

  // At the default level all roles should have the same permissions as authenticated users
  $permissions = Role::load( Role::AUTHENTICATED_ID )->getPermissions();
  // Get permissions for all modules and add them to an array keyed by the module
  $provider_permissions = array();
  foreach( \Drupal::service('user.permissions')->getPermissions() as $key => $permission ) {
    $provider_permissions[ $permission['provider'] ][] = $key;
  }
  // Lets loop our new permissions and create them
  foreach( $roles as $role_values ) {
    // Merge our modules bespoke permissions with the authenticated role permissions
    $permissions = array_merge( $permissions, $role_values['mymodule_permissions'] );
    // Remove our module permissions before we create the role
    unset( $role_values['mymodule_permissions'] );
    // If the role needs access to other module permissions merge them in
    if( isset( $role_values['provider_permissions'] ) && is_array( $role_values['provider_permissions'] ) ) {
      foreach( $role_values['provider_permissions'] as $module ) {
        if( array_key_exists( $module, $provider_permissions ) ) {
          $permissions = array_merge( $permissions, $provider_permissions[ $module ] );
        }
      }
      // Remove the module_permissions before we build the role
      unset( $role_values['provider_permissions'] );
    }
    // Create the role
    $role = Role::create( $role_values );
    // Assign all the required permissions
    foreach( $permissions as $permission ) {
      $role->grantPermission( $permission );
    }
    // Save the role
    $role->save();
  }

This code uses the authenticated role as a base, then adds our module permissions (defined per role), and then adds any other modules permissions to roles that require them.
Answer: You can list all available permissions with PermissionHandler:
$permissions = \Drupal::service('user.permissions')->getPermissions();

You'll find the module name in the provider property of the permissions:
$permissions_by_provider = [];
foreach ($permissions as $key => $permission) {
  $permissions_by_provider[$permission['provider']][] = $key;
}

Comment: That's what I was after. Seems a shame that you can't pass the provider as an argument to filter against. Thanks very much for your help.


Should SQL Server 2008 only be run on Server 2008 R2 (64 bit)?

Question: Today I was told that SQL Server 2008 is supposed to be run on Server 2008 R2 (64 bit). Is this true? And if so, can anyone explain why?
Our development instances are running on 32-bit systems. I don't have any issue with migrating to 64-bit, but are there specific reasons why this is a preferred method?
Comment: Do you have > 4Gb of RAM? If so, then there's no good reason why you should be using x86 (to flip the question).
Comment: I think "supposed to be" really translates into "this is definitely Microsoft's direction".  Things are finally headed to 64-bit.  No reason to hold back.
Answer: With x64 you get access to more RAM and CPU power than with an x86 OS and SQL Server version.  You can use Server 2008 x64 as well.
Answer: Whoever told you that is mistaken. SQL Server 2008 was released before Server 2008 R2, so how could it be intended to only run on Server 2008 R2?
I think the issue that is being confused is that Microsoft strongly recommends the x64 version of SQL Server. They are pushing everyone to their x64 platform, so if you choose to install the x86 version of software, you may find yourself without an upgrade path in the future. This has nothing to do with Windows Server 2008 R2 other than the fact that it happens to only come in an x64 flavor.
Comment: Well... the upgrade path would be to move to x64. The databases are completely interchangable between x86 and x64 platforms (not sure about IA64)
Comment: @Farseeker - Right, if you wish to upgrade the version of SQL in-place (not that I recommend that) and you are on an x86 OS, you are out of luck.
Comment: well yeah that's true. And you can't actually do an in-place upgrade from x86 to x64 so I guess in the strict sense of the word upgrade you're correct.
Comment: @Farseeker - Not that I would ever recommend it, but I've been consulting for some smaller shops on the side lately where resources are limited and some people actually have to do that kind of thing. Gross, I know.


Find all the directional derivative of the function $f(x, y)=y^{2} e^{4 x}$

Question: I would like to find all the directional derivative of the function $f(x, y)=y^{2} e^{4 x}$ in $P=(0,-1)$.
I know that the definition of directional derivative, fixing a direction $\vec{u}=(a,b)$,  is:
$$D_{\vec{u}} f\left(x_{0}, y_{0}\right)=\lim _{h \rightarrow 0} \frac{f\left(x_{0}+h a, y_{0}+h b\right)-f\left(x_{0}, y_{0}\right)}{h}$$
Equally, it can be expressed as follows:
$$D_{\vec{u}} f(x, y)=\nabla f(x,y)^T\vec{u}=\frac{\partial f}{\partial x} a+\frac{\partial f}{\partial y} b=\left(\frac{\partial f}{\partial x}, \frac{\partial f}{\partial y}\right) \cdot(a, b)$$
Now, I've calculated the partial derivatives of $f(x,y)$ which are: $f_x=4y^2e^{4x}, f_y=2ye^{4x}$.
Now, my textbook's result is: $D_{\vec{u}}f(P)=(cos\theta, sin\theta)\cdot (4, -2)$. How does it get there?
Comment: Your second formulation of a directional derivative is only valid when $f$ is differentiable at $(a,b)$, which, fortunately, is the case here.
Answer: The vector $\vec{u}$ is a unit vector.  $\vec{u}=(\cos \theta, \sin \theta).$
If you evaluate $(f_x,f_y)$ at $P$, you get $(4,-2)$.