Using principal components analysis vs correspondence analysis

Question: I am analyzing a data set concerning intertidal communities. The data are percent cover (of seaweed, barnacles, mussels, etc) in quadrats. I am used to thinking about correspondence analysis (CA) in terms of species counts, and principle component analysis (PCA) as something more useful for linear environmental (not species) trends. I haven't really had any luck figuring out if PCA or CA would be a better fit for percent cover (can't find any papers), and I'm not even sure how something that is capped up to 100% would be distributed?
I am familiar with the rough guideline that if the length of the first detrended correspondence analysis (DCA) axis is greater than 2, then you can safely assume that CA should be used. The length of DCA axis 1 was 2.17, which I don't find helpful.
Comment: Both PCA and CA are related and both can be based on SVD algorithm. The fundamental formal difference (not mentioned in @Gavin's otherwise deep answer) is that PCA decomposes relations between columns only (e.g. by decomposing their covariance matrix), treating rows as "cases"; while CA decomposes columns and rows simultaneously, treating them symmetrically, as cross-tabulation "categories". Hence the biplot left by CA and the quasi-biplot (loadings + scores) that could be plotted after PCA give conceptually quite different information.
Answer: PCA works on the values where as CA works on the relative values. Both are fine for relative abundance data of the sort you mention (with one major caveat, see later). With % data you already have a relative measure, but there will still be differences. Ask yourself

do you want to emphasise the pattern in the abundant species/taxa (i.e. the ones with large %cover), or
do you want to focus on the patterns of relative composition?

If the former, use PCA. If the latter use CA. What I mean by the two questions is would you want
A = {50, 20, 10}
B = { 5,  2,  1}

to be considered different or the the same? A and B are two samples and the values are the %cover of three taxa shown. (This example turned out poorly, assume there is bare ground! ;-) PCA would consider these very different because of the Euclidean distance used, but CA would consider these two samples as being very similar because the have the same relative profile.
The big caveat here is the closed compositional nature of the data. If you have a few groups (Sand, Silt, Clay, for example) that sum to 1 (100%) then neither approach is correct and you could move to a more appropriate analysis via Aitchison's Log-ratio PCA which was designed for closed compositional data. (IIRC to do this you need to centre by rows and columns, and log transform the data.) There are other approaches too. If you use R, then one book that would be useful is Analyzing Compositional Data with R.
Comment: As always, a really excellent answer Gavin. Thank you! That clarifies things a lot, and I'll be using PCA then. Given that the intertidal community is 3 dimensional, the percent cover actually went 100% in some cases when the the organisms grew over each other. This is not the closed compositional form that you are talking about though, right?
Comment: No it's not what he's talking about. By closed I believe he means a system in which with three species A, B, C, you have %C = 100% - %B - %A
Comment: and what about DCA?
Comment: DCA is a messed-up version of CA so the same general principles apply to it. DCA is doing some weird torturing of the data and I don't think we need to bother with it as a method in our toolbox today, but others' opinions will vary on that.
Answer: Sorry to revive an oldie, but a student asked me about this. I might be looking at the wrong end of the stick, but the answer by @GavinSimpson seems to me a bit on a tangent, not really aiming at a key distinction between the two methods. A PCA can be run either on a correlation matrix or a covariance matrix. If you run it on the correlation matrix (instead of the covariance matrix), then the absolute values are not being considered. Any multiplication of a row by a constant would not change the results. So the main difference between PCA and a CA must be something else. Underneath the hood, the PCA considers Euclidean distances between points, the CA a qui-squared distances between points. In practice the CA seems to work better with non-continous data (as in nominal, categorical, a considerable amout of 0's, etc). If someone can explain what is the difference in methods regarding the linear algebra underneath the hood in a way that is diggestable to humans, then hats off to them and please post here as I'd love to read it.


announcing rtsp. A way to overcome blocked ports?

Question: If all incoming ports are blocked on a ip camera (due to cellular network stuff), is it possible to have the ip camera initiate the connection to the client-viewer?
I am looking at using a android phone as a ip camera, (there are plenty of examples for the rtsp portion) but will need to figure out how to control the phone from the client-viewer using control connection initiated by the phone itself.
Would SDP announce work?
Answer: In this situation, you need an RTSP server, which can accept announcements from the IP camera, and relay the RTP streams from the IP camera to the RTSP client.
Otherwise, the RTSP client cannot reach the Ip camera directly.
Comment: so, from the ip camera I send an rtsp announce, which gets the client ready for the rtp packets pushed by the ip camera?
Comment: yes, you are right. The ip cameras should keep rtsp connection with the server, and wait for client access.
Comment: is there some IP camera that can do this (be a client only)?


Setting DataSource in VB.NET

Question: Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim dA As OleDb.OleDbDataAdapter
Dim dS As New DataSet
Dim SQL As String

Private Sub AbrirToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AbrirToolStripMenuItem.Click
    Dim strFileName As String
    Dim ClikedOk As Integer

    OpenFD.InitialDirectory = "C:\"
    OpenFD.Title = "Ubica la base de datos"
    OpenFD.Filter = "Agenda|Agenda.mdb"
    OpenFD.ShowDialog()

    If ClikedOk = DialogResult.OK Then
        strFileName = OpenFD.FileName
        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source = Agenda.mdb"

        con.ConnectionString = dbProvider & dbSource
        con.Open()
        SQL = "SELECT * FROM Contactos"
        dA = New OleDb.OleDbDataAdapter(SQL, con)
        dA.Fill(dS, "Agenda")

        con.Close()
        MaxRows = dS.Tables("Agenda").Rows.Count
        inc = -1

    End If
End Sub

What I want to do is this:
The user should press Menu Item and select the Data Base file, then the Data Source would be directed to where the user pointed the Data Base file.
What should I put in the line "dbSource = Data Source = ..."? Access Data Base is .mdb because I got troubles using .accdb
Answer: Change this line:
dbSource = "Data Source = Agenda.mdb"

to this:
dbSource = "Data Source=""" & OpenFD.FileName & """"

and that will point the Data Source to the file the user selected.
Comment: Thanks for your comment, but that didn't work. In fact, if I even try to put OpenFD.Filename; it tells me that ";" doesn't go there.
Comment: @CamiloBloodstar, my apologies, I got my language mixed up. You're using VB and I put that `;` on there because I write primarily in C#. Regardless, you stated you wanted to get the path to the file the user selected - you simply need to append that to the `dbSource` with `OpenFD.FileName` - that doesn't change. Please see my edit, I've fixed the syntax error ***and*** I put double quotes in the string just in case the path has a space in it.
Answer: Well you are in a bit of trouble there. Jet drivers are 32 bit and need to be updated if you want to use jet to access databases in 64 bit windows.
You could also configure your app to run in WoW64 mode to use 32 bit drivers.
if you have done something about making jet work, then you can do:
dbSource = "Data Source=" & OpenFD.FileName;

If you did not want to jump through the hoops of getting Jet to work, then you can use the .Net libraries to connect by changing:
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = Agenda.mdb"

To
dbProvider = "Driver={Microsoft Access Driver (*.mdb)};"
dbSource = "Dbq=" & "Agenda.mdb;"

Best of luck.
Comment: Somewhy when I tried to install AccessDatabaseEngine_x64.exe it told me that I'm under a 32 bit environment (and I don't get it; I have Windows x64, Visual Studio x64 and Office x64).
I tried both of your options and none of them worked fine. After I clicked the Ok button the data base doesn't load.
Thanks for your time anyway!

Setting DataSource in VB.NET


How to vectorize the dot product between segments of a trajectory

Question: Here is a function for the dot product between consecutive segments of a trajectory (xy coordinates). The result is as expected but the "for loop" makes it very slow.
In [94]:
def func1(xy, s):
    size = xy.shape[0]-2*s
    out = np.zeros(size)
    for i in range(size):
        p1, p2 = xy[i], xy[i+s]     #segment 1
        p3, p4 = xy[i+s], xy[i+2*s] #segment 2
        out[i] = np.dot(p1-p2, p4-p3)
    return out

xy = np.array([[1,2],[2,3],[3,4],[5,6],[7,8],[2,4],[5,2],[9,9],[1,1]])
func1(xy, 2)

Out[94]:
array([-16.,  15.,  32.,  31., -14.])

I looked for a way to vectorize the above, hoping to make it much faster. Here is what I came up with:
In [95]:
def func2(xy, s):
    size = xy.shape[0]-2*s
    p1 = xy[0:size]
    p2 = xy[s:size+s]
    p3 = p2
    p4 = xy[2*s:size+2*s]
    return np.diagonal(np.dot((p1-p2), (p4-p3).T))

func2(xy, 2)

Out[95]:
array([-16,  15,  32,  31, -14])

Unfortunately, the dot product produces a square matrix from which I have to take the diagonal:
In [96]:
print np.dot((p1-p2), (p4-p3).T)
np.diagonal(np.dot((p1-p2), (p4-p3).T))

[[-16  10  16 -24  10]
 [-24  15  24 -36  15]
 [-32  20  32 -48  20]
 [ 20 -13 -18  31 -14]
 [ 32 -18 -40  44 -14]]

Out[96]:
array([-16,  15,  32,  31, -14])

My solution is really awful. It improves speed by only a ratio of 2 and more importantly it is now not scalable. My average trajectories have several ten thousands points which means that I'll have to deal with huge matrices.
Do you guys know of a better way?
Thank you
EDIT:
Awesome! einsum is definitively the solution. In my frustration I wrote the dot product myself. I know, not very readable and it defeats the purpose of using optimized libraries but here it is anyway (func4). Speed is on par with einsum.
def func4(xy, s):
    size = xy.shape[0]-2*s
    tmp1 = xy[0:size] - xy[s:size+s]
    tmp2 = xy[2*s:size+2*s] - xy[s:size+s]
    return tmp1[:, 0] * tmp2[:, 0] + tmp1[:, 1] * tmp2[:, 1]

Comment: for larger arrays your `func4` is faster.
Answer: Your idea in func2 leads naturally to using np.einsum.
The good part of func2 is that it only computes p1, p2, p3, p4 once as
larger arrays instead of small pieces as in func1.
The bad part of func2 is that it does a lot of dot products that you don't
care about.
That's where einsum comes in. It is a more flexible version of np.dot.
Whenever you are computing a sum of products, think about using np.einsum. It
is likely to be among the fastest (if not the fastest) way to compute the
quantity using NumPy.
def func3(xy, s):
    size = xy.shape[0]-2*s
    p1 = xy[0:size]
    p2 = xy[s:size+s]
    p3 = p2
    p4 = xy[2*s:size+2*s]
    return np.einsum('ij,ij->i', p1-p2, p4-p3)

The subscript string 'ij,ij->i' has the following meaning:
There are two parts to the subscript string 'ij,ij->i': before the arrow
(->), on the left, ij,ij and after the arrow, i.
On the left, the ij before the comma refers to the subscripts for p1-p2, and
the ij after the comma refer to the subscripts for p4-p3.
The Einstein summation notation sums over repeated subscripts that do not appear after the arrow. In this case, j is repeated and does not appear after the arrow.
So for each i, the sum (p1-p2)[i,j]*(p4-p3)[i,j] is computed, where the sum runs over all j. The result is an array indexed by i.

Sanity check:
In [90]: np.allclose(func1(xy, 2), func3(xy, 2))
Out[90]: True

Here is a benchmark: on an array xy of shape (9000, 2), showing using einsum
is 450x faster that func1 and 7470x faster than func2:
In [13]: xy = np.tile(xy, (1000,1))

In [14]: %timeit func1(xy, 2)
10 loops, best of 3: 42.1 ms per loop

In [15]: %timeit func2(xy, 2)
1 loops, best of 3: 686 ms per loop

In [16]: %timeit func3(xy, 2)
10000 loops, best of 3: 91.8 µs per loop

The OP's func4 performs even better than func3!
In [92]: %timeit func4(xy, 2)
10000 loops, best of 3: 74.1 µs per loop

I think the reason why func4 is beating einsum here is because the cost of setting of the loop in einsum for just 2 iterations is too expensive compared to just manually writing out the sum.
Answer: einsum is a good tool for generalizing the dot product.  Playing around with it, I can reproduce your numbers with:
np.einsum('ij,ij->i',p1-p2,p4-p3)

'ij,kj' produces dot(p1-p2, (p4-p3).T); the 'i...,i...->i' does the diagonal - all in one step.

as a spin off from your cross-product question I tried
tmp11,tmp21),tmp11[:,0]*tmp21[:,0]+tmp11[:,1]*tmp21[:,1])

For 5000 row arrays it is almost 2x the speed of the einsum calc.


Dictionary only captures last key value pair Python

Question: There is a dictionary for names and a function to print the dictionary. The problem is when I run the create_user() function it only captures and prints the last values.
users={}

def create_users():

    while True:
        choice=input('Create a new user?: Y/N ')
        if(choice=='y'):
           first_name=  input('Enter first name: ')
           users['first_name'] = first_name

           last_name= input('Enter last name: ')
           users['last_name']=last_name

           print(len(users))

        if(choice=='n'):
            print('Exit')
            break

def print_dir():
    print('Directory Item ','\n')
    for k,v in users.items():
        print(k,v)

create_users()
print_dir()

I have also tried this below, capturing before while and after while. That did not solve.
def create_user():
  first_name=  input('Enter first name: ')
  users['first_name'] = first_name

  last_name= input('Enter last name: ')
  users['last_name']=last_name

  while True:
        choice=input('Create a new user?: Y/N ')
        if(choice=='y'):
           first_name=  input('Enter first name: ')
           users['first_name'] = first_name

           last_name= input('Enter last name: ')
           users['last_name']=last_name

Any hint/clue  how can I solve this? Thanks
Additional Item I tried per suggestion (nested dictionary)
users={}
users['username'] = {}

while True:
        choice=input('Create a new user?: Y/N ')
        if(choice=='y'):
           first_name=  input('Enter first name: ')
           users['username']['first_name'] = first_name

           last_name= input('Enter last name: ')
           users['username']['last_name']=last_name

I still had the same issue of not getting all the values
Create a new user?: Y/N y
Enter first name: james
Enter last name: jones
Create a new user?: Y/N y
Enter first name: rob
Enter last name: william
Create a new user?: Y/N n
Exit
Directory Item
username {'first_name': 'rob', 'last_name': 'william'}
Comment: [mcve] with example input / expected output please.
Comment: You are basically creating a dictionary with just two key that is 'first_name' and 'last_name' and also each time you input a value in first_name or last_name, these keys are been overwritten by first_name and last_name variables. That's why you the dictionary is containing the last value for each of the keys.
Answer: users['last_name']=last_name

Your problem is that users is being used to store a single user's data. What you want is something more like
users[username]['last_name']=last_name

Making users a dictionary of dictionaries. Where to get username is up to you. If you don't have a good source, making users into a list of dictionaries may be better.
Answer: Dictionary is a key-value pair.Dictionary has unique keys. So when you run the program, and choose second-time 'y' it will update the existing key which is 'first_name' and 'last_name'. You cannot have data in dictionary like {'first_name':'A','last_name':'B','first_name':'C','last_name':'D'} in this case the keys are repeated.  However, you can save first_name as key and last name as values. see the below code.
users={}

def create_users():

    while True:
        choice=input('Create a new user?: Y/N ')
        if(choice=='y'):
           first_name=  input('Enter first name: ')
           # users['first_name'] = first_name

           last_name= input('Enter last name: ')
           # users['last_name']=last_name
           users[first_name] = last_name
           print(len(users))

        if(choice=='n'):
            print('Exit')
            break

def print_dir():
    print('Directory Item ','\n')
    for k,v in users.items():
        print(k,v)

create_users()
print_dir()

Comment: Thanks for the explanation. It does make sense that why firstname,lastname part will not work. I accepted the answer, however if I wanted to  use fname,lname let's say a user wants to know what is the fname, how can I design it?
Comment: I think is you want to use fname, lname you can keep 'users' as list. and keep each user as dictionary so final structure will be something [{fname:fname1,lname:lname1},{fname:fname2,lname:lname2}]. Rest depends upon how you design it. So add each student with list.append() operation.


Are non-string non-aerophone instruments suitable for chordal playing?

Question: A friend has recently told me that while string instruments and aerophones are suitable for playing chords, bells and most other instruments are not. He said the reason is that when you play a certain note on a strings instrument or an aerophones, it produces the main pitch and overtones in other pitches that are harmonically related to the main pitch in a way that is suitable for our usual diatonic scale and chords. But for bells and most other instruments, he says they produce totally different overtones and are thus not suitable for playing chords.
Is this true? How does this work? Also, I play a MIDI controller with a synthesizer and thus I want to know which synthesized sounds are suitable for chordal playing and which are not.
Comment: Cue the argument over whether a piano is a string or a percussion instrument :-)
Answer: Bells, yes.  They tend to have 'inharmonic harmonics' making them problamatic for chordal music.  But 'most other instruments' is going way too far.  Mallet instruments such as xylophone and marimba play chords just fine.  And it's only really the traditional 'bell-shaped' bells that have the issue.  There are plenty of bell-like instruments - glockenspiel, celesta etc. - which have more harmonic spectrums, allowing them to blend in harmony with themselves and other instruments.
Yes, 'all other' covers synthesis too, if we want to be pedantic.
Comment: @Tetsujin I would say more generally that _sufficiently high-pitched_ instruments can play chords even if they have non-harmonic overtone spectra. The higher the pitch, the more does the fundamental dominate and the overtones just add a shimmering effect. So hand bells work quite ok, but larger bells – arguable. IMO, chords on carillon sound pretty horrible.
Comment: Sure you can play chords on bells - https://www.youtube.com/watch?v=tHgpuWPg1OE [it's actually more fun than it initially looks, give it a minute to get going].
Comment: @leftaroundabout but it's not only about the relative prominence of the overtones and the fundamental; there's also the tuning.  Carillon bells are specifically tuned to emphasize the minor third above the "prime" -- the most prominent tone (which is an octave above the "hum tone," the fundamental). This makes chords that include a major third rather dissonant.  A handbell that is nominally an octave lower will have the same fundamental, but the emphasized overtone is a twelfth above.  A handbell choir playing F3-A3-C4 should be more harmonious than a carillon playing F4-A4-C5.
Comment: IIRC timpani are another pitched instrument that has "inharmonic harmonics".
Answer:
for bells and most other instruments, he says they produce totally different overtones and are thus not suitable for playing chords

This is true in general.  However, it is possible to tune the inharmonic overtones of an idiophone by shaping the instrument, which can enable them to play chords.  The marimba and vibraphone are notable examples.  From Wikipedia's vibraphone article:

These rectangular bars have three primary modes of vibration.  The deep arch causes these modes to align and create a consonant arrangement of intervals: a fundamental pitch, a pitch two octaves above that, and a third pitch an octave and a major third above the second. For the F3 bar that usually forms the lowest note on a vibraphone, there would be F3 as the fundamental, F5 as the first overtone, and A6 as the second overtone. ...
After beveling or rounding the edges, fine-tuning adjustments are made. If a bar is flat, its overall pitch structure can be raised by removing material from the ends of the bar. Once this slightly sharp bar is created, the secondary and tertiary tones can be lowered by removing material from specific locations of the bar.

which synthesized sounds are suitable for chordal playing and which are not[?]

The best way to determine this is to use your ear.  But you can use the information above to help you understand what's going on.  Synthesis of bells and other idiophones is achieved by using inharmonic overtones, and just as with natural sounds, the degree to which these overtones approximate the harmonic series determines their usefulness in chords.

Handbells have been mentioned in the comments.  These are also manufactured with attention to the relationship between the fundamental pitch and the overtones. From Wikipedia:

The overtones on an English handbell are a 12th (an octave and a perfect fifth) above the fundamental, while Dutch handbells – such as Petit & Fritsen – focus on the overtone a minor 10th (an octave and a minor third) or a major 10th (an octave and a major third) above the fundamental.

In fact, inharmonicity is also a feature of strings unless the oscillation is driven (with a bow).
This leads to stretched tuning in pianos and various problems with guitar intonation.
Answer: A variety of idiophones will work. Tubular bells, xylophone, marimba, and steel drums come to mind. But on a synthesizer, most anything can work as long as it produces a pitch.
Broadly speaking, for pitches instruments, the overtone series is the overtone series. What changes from instrument to instrument is which partials are more or less present in the sound. Those combinations give different instruments their unique timbres, but don't especially impact whether chords sound good or not.
Comment: "Broadly speaking, for pitche[d?] instruments, the overtone series is the overtone series": idiophones do not have harmonic overtones in general; some care is required to tune the overtones when the goal is to be able to play harmonic intervals.
Comment: @phoog I did not speak of idiophones in general. I named several specific ones that regularly play chords. I'm not sure what your objection is.
Comment: It's to the "broadly speaking" sentence, as indicated in the comment.  The phrase "broadly speaking, for pitched instruments" indicates that the context of the second paragraph isn't limited to the list in given in the first but instead to all pitched instruments (if I'm wrong to assume that "pitches" is a mistake for "pitched," please let me know).  So you seem to be saying that all pitched instruments necessarily have harmonic overtones in varying proportions, which isn't true of pitched idiophones. Their overtones aren't harmonic but may be tuned to resemble harmonics.


using windows shadow copy for folders, files

Question: it seems only R2 versions of W2k3 and W2k8 support folder, file level VSS backup. In regular Windows server editions only volumes could be backed up.
is there any links or doc to confirm this behavior?

2008R2 edition:   launch "windows
  server backup", Choose "custom" backup ... "select items for backup"
  Windows 2008 edition:  displays only volumes ....(C, D...)?
Answer: Vss has been a part of windows since windows 2003 see How Volume Shadow Copy Service Works
VSS is a volume shadow copy.  There is no "file level" volume shadow copy service.  VSS is used by many (including microsoft) backup software solutions in order to do file level backup.  In addition VSS is used to enable the previous versions feature.
Comment: Hi Jim, R2 versions allow you to choose "files, folders" during backup apart from volume. what could this be?
w2k3: ntbackup
w2k8: server backup
both use VSS services but they are more flexible in R2 versions to allow individual files/folders to be selected on a volume? any thoughts? thx in adv.
Comment: So you are really asking about ntbackup - not VSS.  It's a *volume* shadow copy.  I couldn't find much in the way of differences between r2 ntbackup and ntbackup under 2003 (windows server backup is different).  Can you elaborate on exactly what the difference is that you are seeing?
Comment: i am taliking about "windows backup" in 2008 server vs R2 editions. Choose "custom" backup option >> "select items for backup" appears in R2 version. But Srv 2008 displays only volumes (C, D...). Why is this?


How do I tag an EBS volume using AWS CDK?

Question: I am creating an instance with an EBS volume using AWS CDK:
BlockDevice durableStorage = BlockDevice.builder()
                                        .deviceName("/dev/sdf")
                                        .volume(BlockDeviceVolume.ebs(
                                                    DURABLE_STORAGE_GB,
                                                    EbsDeviceOptions.builder()
                                                                    .deleteOnTermination(true)
                                                                    .encrypted(true)
                                                                    .volumeType(EbsDeviceVolumeType.GP2)
                                                                    .build()))
                                                .build();

Instance instance = new Instance(
                this,
                "MyInstance",
                InstanceProps.builder()
                             /* other config here */
                             .blockDevices(List.of(durableStorage))
                             .build());

How do I tag the EBS volume? The Tag static method requires a Construct, which I can't find on the BlockDevice, e.g. I want to do something like this:
Tag.add(durableStorage, "cdk", "true");

Answer: The only working solution I found so far is using EC2 User Data to tag the attached EBS volume:
import * as ec2 from '@aws-cdk/aws-ec2'

// ...

const userData = ec2.UserData.forLinux()
userData.addCommands(
  // Tag volume
  'AWS_INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)',
  `ROOT_DISK_ID=$(aws ec2 describe-volumes --region ${props.region} --filter "Name=attachment.instance-id, Values=\${AWS_INSTANCE_ID}" --query "Volumes[].VolumeId" --out text)`,
  `aws ec2 create-tags --region ${props.region} --resources \${ROOT_DISK_ID} --tags Key=${tagKey},Value=${tagValue}`,
)

const ec2Instance = new ec2.Instance(this, 'ec2-instance', {
  // ...
  userData: userData,
})

Comment: This approach works well.  It's worth noting that the describe-volumes command will give a space-delimited list of volumes, so if you pass that result unquoted (so it is multiple args to 'aws') it will tag all volumes.
Answer: It looks like it is not possible to tag volume directly with CDK constructs. Maybe tag will be propagated to the volume when instance is tagged.
Tagging the volumes directly looks to be however achievable by using CloudFormation resources - https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.CfnInstance.html
Comment: I try to tag the instance and hope the attached volume would be tag. Unfortunately it didn't work.
Comment: You need to set the `propagateTagsToVolumeOnCreation` prop


Can any integer, not a multiple of three, be represented as $n = \sum_{i=0}^{a-1} 3^i \times 2^{b_i}$?

Question: This question has some relevance to the Collatz conjecture. It was originally based on trying to represent a number like this: Finding whether $\dfrac{2^k - (2\cdot3^{n-1} + 2^{t_0}3^{n-2} + 2^{t_0+t_1}3^{n-3} .... + 2^{t_0+t_1+...+t_{n-1}})}{3^n}$ can describe all integers
However, I generalised that and tried to ask a simpler question instead, now this is simply out of curiosity rather than anything useful, as in often the case with the Collatz conjecture.
Is it possible to represent a number that is not divisible by three as:
$$n = \sum_{i=0}^{a-1} 3^i \times 2^{b_i}$$
$$n, a, b_i \in \Bbb{Z^+_0}$$
Where $b_i$ is some arbitrary integer such that $b_{i+1} \leq b_i$
For example, $13=1+3+9$.
EDIT: My original question asked what happened with these conditions:
 $$n, a, b_i \in \Bbb{Z^+}$$
$$b_{i+1} < b_i$$
(Notice the subtle differences in specifications.) Under these above conditions, some numbers cannot be represented, like $13$. What are some characteristics of numbers that cannot be represented like this?
Any help will be much appreciated. Thanks.
Comment: This turned out to be a lot more interesting than it first appeared. Thanks!
Comment: If $a=1$ and $b_i>0$ is not satisfied, I dont think so.
Comment: @NaturalNumberGuy Can you make it rigorous?
Comment: And your "not a multiple of three" is a red herring, since multiples of 3 can clearly be represented in this form if non-multiples can.
Comment: @MJD How so? A number congruent to 0 mod 3 cannot be represented like this as expanding out this sum will show an extra power of 2 without a power of 3, meaning this is 1 or 2 mod 3.
Comment: The answer is yes, but I can't write it up until I get home later. Summary: Every even number is represented with $b_i>0$, so we can represent $2n$ if and only if we can also represent $n$. Now say $n$ is in $[3^k..3^{k+1}-1]$ and is odd. Then represent $n$ as $3^k+2n'$, and get the representation of $2n'$ from the representation of $n'$, which is already representable by the same method. (Induction argument.) Choosing a random example, $12467= 6561+2\cdot 2187+4\cdot243+16\cdot 27+1 28\cdot 1$.
Comment: In your example, the exponents are not strictly increasing by one. You have $3^0=1$ followed by $3^3 = 27$. Due to this application in the collatz conjecture the exponents must only increase by one. This is indicated by the use of $3^i$.
Comment: I also did not ever doubt that $1 = 2^0$ and hence a power of two.
Comment: I see I misunderstood your conditions. Sorry, I will look into it more later on.  How is it possible to represent 13 in your form?
Comment: I have tried to do this already, but it does not seem all too easy to fix up, as all exponents must be increasing, positive, and always must follow the rules I have indictated. The only freedom you have is the choice of $a$ and $b_i$
Comment: That is a good point, I do not believe $13$ can be represented, I shall generalize the question to allow $b_i=b_{i+1}$
Comment: How is this representation of $13$ legal?  As you pointed out, the $1$ term is not allowed because it has $b_0 = 0$, but you required $b_i > 0$.  If all the $b_i$ are required to be positive, obviously no odd number can be represented.
Comment: No because you can add a power of three to make it odd, and I have updated the question to allow for this representation of $13$ due to $b_i \in \Bbb{Z^+_0}$
Comment: Have you thought about how to represent this as a string of digits? e.g., balanced ternary can use 2 for $-1$.
Comment: It might be helpful if you gave some reason to believe that the relation should hold, and/or if you expounded a little on the connection to the Collatz Conjecture. If the connection is something like "my relation implies Collatz with a single trivial step", then one might reasonably suspect that your relation isn't particularly-easy to establish.
Comment: Good idea @RobertSoupe, I had not thought of that.
Answer: I deduced the same thing when study the Collatz conjecture, here is the proof without the restrictions and some stuff related.
Let $G_n = \{m \in \mathbb{N} \,|\,\gcd(m,n) = 1\}$
We can do this for all $G_n$, for example $n = 2$, but we only interested in $n = 3$.
Lemma:
For all $n \in G_3$, exists $a \in \mathbb{Z} : 0 \le a$ such that:

$n = 2^a \hspace{5pt} (\text{mod } 3)$
$n \neq 2^a \hspace{5pt} (\text{mod } 9)$

Proof:
Without loss of generality, we use this table:
<code>      +---+---+---+---+----+----+
      | 1 | 2 | 4 | 8 | 16 | 32 |
+-----+---+---+---+---+----+----+
|Mod 3| 1 | 2 | 1 | 2 | 1  | 2  |
+-----+---+---+---+---+----+----+
|Mod 9| 1 | 2 | 4 | 8 | 7  | 5  |
+-----+---+---+---+---+----+----+
</code>
From the table:

For all $n \in G_3$, exists $a$ such that $n = 2^a\ (\text{mod }3)$.
For all $n \in G_3$, exists $a$ such that $n = 2^a\ (\text{mod }9)$.
By contradiction, suppose that for all $a$ we have $n = 2^a\ (\text{mod }3)$ and $n = 2^a\ (\text{mod }9)$.
By the table, exists $a'$ such that $2^a = 2^{a'}\ (\text{mod }3)$ and $2^{a} \neq 2^{a'}\ (\text{mod }9)$.
Then, $n = 2^{a'}\ (\text{mod }3)$ and $n \neq 2^{a'}\ (\text{mod }9)$.

Finally, by contradiction, exists $a$.
Corollary: For all $n \in G_3$, exists $a \in \mathbb{Z} : 0 \le a$ and $k \in G_3$ such that $n = 2^a + 3k$.
Notation: If a number $n$ can be represented in this form, for some $l \in \mathbb{Z} : 0 \le l$:
$$ n = 2^{\alpha_0} + 2^{\alpha_1}\,3^{1} + 2^{\alpha_2} + ... + 2^{\alpha_{l}}\,3^{l} $$
such that $\alpha_i \in \mathbb{Z} : \alpha_i \ge 0$ for $i = 1, ..., l$.
Then, we said that n can be represented in a $l$-canonical form and if exists $l \in \mathbb{Z} : 0 \le l$ such that n is a $l$-canonical form then we said n can be represented in the canonical form.
Theorem: Every number in $G_3$ can be represented in the canonical form.
Proof: We prove by induction that $n$ can be represented in that form, for all $n \in G_3$.
Base case:
You can find a expression for all $n \in G_3 : n \le 9$.
Inductive case:
Suppose this is holds for all $k \in G_3 : k < n$:
By the corollary, exists $a$ and $k \in G_3$ such that $n = 2^a + 3 k$.
Because $k < n$, $k = 2^{\alpha_0} + 2^{\alpha_1}\,3 + ... + 2^{\alpha_i}\,3^i$.
Finally, because $n = 2^{a} + 3\,(2^{\alpha_0} + 2^{\alpha_1}\,3 + ... + 2^{\alpha_i}\,3^i)$, every number in $G_3$ can be represented in the canonical form.
Notation: If $n \in G_3$ can be represented in a $0$-canonical form, we said $n$ can be represented in the canonical principal form.
Notation: If $m$ can be represented in a $l$-canonical form and $n = m + 3^{l + 1}\,t$ then we said $t$ is in the tail of $n$, with $n,m,t \in G_3$.
The Collatz conjecture implies that every number in $G_3$ is in the tail of some canonical principal form.
Conjecture: If $n \in G_3$ can be represented in a $a$-canonical form and $b$-canonical form, for $a \le b$, then $n$ can be represented in a $l$-canonical form, for all $l \in \mathbb{Z} : a \le l \le b$.
Comment: Very nice proof, however one slight snag. You say that $2^a +3(2^{\alpha_0}+...+2^{\alpha_i}3^i) = n$, however you have not assured that $a\geq \alpha_i$. Despite this, this proof could be extended to cover the aforementioned case?
Comment: Exactly i not prove that because i did not know how, my answer was only a help. But now i know how (I might edit today or tomorrow). This is the idea to prove that $\alpha_0 \ge a$. Suppose, without loss of generality, that $\alpha_0 = a + 1$ and a is the maximum (i.e. not exists $k \in G_3$, for $a + 2$, such that $n = 2^{a+2} + 3k$); Then, $n = 2^a + 2^{a+1}\,3 + 3^2\,k'$ for some $k' \in G_3$. This implies that exists $k \in G_3$ such that $n = 2^a(1+3*2) + 3^2\,k = 2^a(2^2 + 3) + 3^2\,k' = 2^{a+2} + 3k$. Then, by contradiction, $\alpha_0 \le a$. Finally, by induction, $a \ge \alpha_i$.
Answer: This is only a comment, but too long for the comment box
Since your $b$'s are organized as equal or decreasing one can define "layers" of equal $b$ and sum the terms with equal $2^b$ and decreasing powers of $3$ up. That would give something like
$$ n = 2^{b_1}\left( {3^{A_1} -1 \over 3-1 }\right) + 2^{b_2}\left( {3^{A_2} -1 \over 3-1 } \right) + ... + 2^{b_m}\left( {3^{A_m} -1 \over 3-1 } \right)$$
and can even be expressed more compact like this form
$$ n = 2^{b_1}\left( {3^{A_1} -1 \over 3-1 }+ 2^{b_2}\left( {3^{A_2} -1 \over 3-1 }  + ... + 2^{b_m}\left( {3^{A_m} -1 \over 3-1 } \right)\right)\right) $$
(the indexes $A_k$ and $b_k$ are used only as models, not actual indices here).
From this the question can then also be written in base-3 representation
$$ n=2^{b_1} \cdot 111111...1_3 + 2^{b_2} \cdot 111...1_3 + ...$$
or so. Perhaps looking at such representations would easier give an idea whether any number $n$ can be expressed in such a form.


How do I fix subscript out of bound error for my interaction matrices?

Question: I am trying to create interaction matrices from multiple files. With some of my files, I am receiving the following error:
Error in interaction_matrix[as.numeric(pig_id), as.numeric(closest_pig)] :
  subscript out of bounds

This is the code I am using.
create_interaction_matrices <- function(nm, behavior_code = "AFF", select_every = 10, nsel = 25) {
  NNdatafull <- read_excel(nm, sheet = 1)
  colnames(NNdatafull) <- c("Date", "Time", "Pen", "Pig_ID", "Distance", "Closest_Pig", "Behavior", "Orientation", "Location")
  datelevels <- unique(NNdatafull$Date)

  interaction_matrices <- list()

  for (date in datelevels) {
    mdat <- filter(NNdatafull, Date == date)
    mdat <- filter(mdat, Behavior == behavior_code)

    # Get unique pig IDs from both Pig_ID and Closest_Pig columns
    unique_pigs <- union(unique(mdat$Pig_ID), unique(mdat$Closest_Pig))

    # Initialize interaction matrix with the size based on the number of unique pigs
    interaction_matrix <- matrix(0, nrow = length(unique_pigs), ncol = length(unique_pigs))

    for (i in 1:nsel) {
      time <- min(mdat$Time) + (i - 1) * (60 * select_every)
      time_data <- filter(mdat, Time == time)
      interacting_pigs <- unique(time_data$Pig_ID)
      closest_pigs <- unique(time_data$Closest_Pig)

      # Update interaction matrix based on pig ID to closest pig interactions
      for (pig_id in interacting_pigs) {
        closest_pig <- time_data$Closest_Pig[time_data$Pig_ID == pig_id]
        # Increment the value in the interaction matrix
        interaction_matrix[as.numeric(pig_id), as.numeric(closest_pig)] <- interaction_matrix[as.numeric(pig_id), as.numeric(closest_pig)] + 1
      }
    }
    interaction_matrices[[as.character(date)]] <- interaction_matrix
  }
  return(interaction_matrices)
}

This is an example of data I am using:
data example
I think it is because the code has the matrix as length(unique_pigs) but some of the ID numbers are greater than the number of pigs. For example, a pen has 13 pigs, but one pig has 16 as an ID number.
How would I edit the code to include only the ID numbers present in the datasheet?
Thanks!
Comment: So we are supposed to send that image file to an AI-bot to turn it into something machine readable? Not me. Learn to post in text.
Answer: You were correct in assuming that the issue arises because the pig IDs and the closest pig IDs do not necessarily correspond to the row and column numbers of the matrix. You would need to create a mapping between pig IDs and the matrix indices.
Here's the how I did it:
create_interaction_matrices <- function(nm, behavior_code = "AFF", select_every = 10,
  nsel = 25) {
  # you can keep doing as you had here, I just did this for myself
  file_path <- file.path(getwd(), paste0(nm, ".xlsx"))
  NNdatafull <- read_excel(file_path, sheet = 1)
  colnames(NNdatafull) <- c("Date", "Time", "Pen", "Pig_ID", "Distance",
    "Closest_Pig", "Behavior", "Orientation", "Location")
  datelevels <- unique(NNdatafull$Date)

  interaction_matrices <- list()

  for (date in datelevels) {
    mdat <- filter(NNdatafull, Date == date)
    mdat <- filter(mdat, Behavior == behavior_code)

    # Get unique pig IDs from both Pig_ID and Closest_Pig columns
    unique_pigs <- union(unique(mdat$Pig_ID), unique(mdat$Closest_Pig))

    # create mapping of pig IDs to matrix indices
    pig_id_to_matrix_idx <- match(unique_pigs, sort(unique_pigs))

    # Initialize interaction matrix with the size based on the number of unique pigs
    interaction_matrix <- matrix(0, nrow = length(unique_pigs),
      ncol = length(unique_pigs))

    for (i in 1:nsel) {
      time <- min(mdat$Time) + (i - 1) * (60 * select_every)
      time_data <- filter(mdat, Time == time)

      # Update interaction matrix based on pig ID to closest pig interactions
      for (row in 1:nrow(time_data)) {
        pig_id <- time_data$Pig_ID[row]
        closest_pig <- time_data$Closest_Pig[row]

        # convert pig IDs to matrix indices
        pig_idx <- which(unique_pigs == pig_id)
        closest_pig_idx <- which(unique_pigs == closest_pig)

        # check if the indices are valid; then update the interaction matrix
        if (length(pig_idx) == 1 && length(closest_pig_idx) ==
          1) {
          interaction_matrix[pig_idx, closest_pig_idx] <- interaction_matrix[pig_idx,
          closest_pig_idx] + 1
        }
      }
    }
    interaction_matrices[[as.character(date)]] <- interaction_matrix
  }
  return(interaction_matrices)
}

this doesn't return any errors on my end, and after running it through a tester function to graph it, it seems to work:
test_and_plot_interaction_matrices <- function(filename) {
  library(ggplot2)
  library(reshape2)

  interaction_matrices <- create_interaction_matrices(filename)

  some_date <- names(interaction_matrices)[1]
  matrix_to_plot <- interaction_matrices[[some_date]]

  matrix_long <- melt(matrix_to_plot)
  names(matrix_long) <- c("Pig_ID", "Closest_Pig", "Frequency")

  ggplot(matrix_long, aes(x = Pig_ID, y = Closest_Pig, fill = Frequency)) +
    geom_tile() + scale_fill_gradient(low = "white", high = "steelblue") +
    theme_minimal() + labs(title = paste("Interaction Matrix for Date:", some_date),
    x = "Pig ID", y = "Closest Pig ID", fill = "Interaction\nFrequency") +
    theme(axis.text.x = element_text(angle = 90, hjust = 1))

}

image of heatmap on stack.imgur.com
I don't know if I graphed the data correctly, but it shouldn't matter since your function works now [I hope :)]

edit:
I thought of a way to do it with vectorized operations:
library(dplyr)

create_interaction_matrices <- function(nm, behavior_code = "AFF", select_every = 10,
  nsel = 25) {
  file_path <- file.path(getwd(), paste0(nm, ".xlsx"))
  NNdatafull <- read_excel(file_path, sheet = 1)

  # preprocess
  NNdatafull <- NNdatafull |>
    filter(Behavior == behavior_code) |>
    mutate(Date = as.character(Date))

  # store interaction matrices for each date
  interaction_matrices <- list()

  # process each date
  for (date in unique(NNdatafull$Date)) {
    mdat <- filter(NNdatafull, Date == date)

    # Get unique pig IDs and create a mapping to matrix indices
    unique_pigs <- sort(unique(c(mdat$Pig_ID, mdat$Closest_Pig)))
    pig_id_to_matrix_idx <- match(unique_pigs, unique_pigs)

    # Initialize the interaction matrix with the size based on the number of unique pigs
    interaction_matrix <- matrix(0, nrow = length(unique_pigs),
      ncol = length(unique_pigs), dimnames = list(unique_pigs,
        unique_pigs))

    # iterate through selected dates
    for (i in seq_len(nsel)) {
      time <- min(mdat$Time) + (i - 1) * (60 * select_every)
      time_data <- filter(mdat, Time == time)

      # update interaction matrix for each row in time_data
      apply(time_data, 1, function(row) {
        pig_idx <- match(row["Pig_ID"], unique_pigs)
        closest_pig_idx <- match(row["Closest_Pig"], unique_pigs)

        if (!is.na(pig_idx) && !is.na(closest_pig_idx)) {
          interaction_matrix[pig_idx, closest_pig_idx] <- interaction_matrix[pig_idx,
          closest_pig_idx] + 1
        }
      })
    }

    interaction_matrices[[date]] <- interaction_matrix
  }

  return(interaction_matrices)
}

I'm not sure if it's the best way to do it, but it doesn't give any errors either,on my end at least, we'll have to see how it goes for you :)


How to prove it is a basis

Question: If $p(x)$ is a polyonmial of degree $n$ (with coefficients in ${\mathbb{Q}}$). If $p(x)$ is irreducible in ${\mathbb{Q}}[x]$ and $a$ is a complex root of $p(x)$. How to show that $1, a, a^2, ..., a^{n-1}$ are linearly independent in ${\mathbb{Q}}$?
Comment: If they weren't linearly independent, what would this say about the irreducibility of $p(x)$?
Comment: @BrandonThomasVanOver I don't get it.
Answer: Hint:
Suppose there exist $c_i \in \mathbb{Q}$ (not all of them are $0$) such that $c_0+c_1a+c_2a^2+\dotsb+c_{n-1}a^{n-1}=0$, then what does it say about $a$ being root of a....
Comment: $a$ is a complex root of $p(x)$, I do not see the relation between $ c_0+c_1a+c_2a^2+\dotsb+c_{n-1}a^{n-1}=0$ and $p(x)$. More hint?

How to prove it is a basis


Deploying parse Server example to Digital Ocean

Question: I successfully deployed  parse server example to heroku and access it with parse dashboard.
Now I want to deploy it to Digital Ocean but I 'm not getting any clue that how to achieve this. I already spent hours on this but didn't find anything enough clear to follow.
Advise to towards right direction
(I know this doesn't meet the requirements of stack overflow's question criteria, but what to do when you don't 've anything to start. )
Answer: Digital Ocean has a pretty straight tutorial here
I have followed it and can certified it does work.
Comment: I have seen it
but couldn't follow (due first interaction with digital ocean
How ever thanks
Comment: I recommend you do this tutorial first to configure your server: https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04


Parallel chord substitutions

Question: Are parallel chord substitutions common in (classical) music (if so, please share some examples)? And do they have the same function as the original chords?
By saying parallel chords I am referring to the term Hugo Riemann used in his theory (Parallelklang in German). For example, in the key of C major (minor), the parallel chords for each chord degree are:
  I: A minor (Eb major)
 ii: F major (diminished, so none?)
iii: G major (C minor)
 IV: D minor (Ab major)
  V: E minor (Bb major)
 vi: C major (F minor)
VII: diminished, so none? (G minor)

Since any parallel chord for each chord degree is in the key of C major or C minor, I won't talk about them much (*).
But I was wondering, what if we use the parallel chord of, say, the Neapolitan, e.g. Bb minor, in the key of C? For example, instead of having the usual chord progression: I-iiN-V, we'd have: I-(iiN)p-V, where (iiN)p is the parallel chord of the Neapolitan, Bb minor. Please note that I am referring to V as function, so it could be V, V7, V+, VII7 (diminished or not), etc.
And also, what about secondary dominants? For example, in C major:
 V/ii: F# minor (instead of A major)
V/iii: G# minor (B major) (the same applies to the V/III)
 V/IV: A minor  (C major)
  V/V: B minor  (D major)
 V/vi: C# minor (E major)

*I'd like to mention that, while writing the first table, I realized that the parallel chord of II is IV, and vice versa. Since we know that these two chords share the same function, does this answer my very question? Does this apply to the parallel chords of the "chromatic" chords as well (the Neapolitan and the various double dominants)?
However, interestingly, this does not apply to minor scales. For example, in C minor, the parallel chord of i is not Ab major (VI), etc.
Comment: There's a slight confusion in terms. In German, I believe, the term parallel is used to mean relative - and that's where this question is going. The majority, I believe, is used to the term parallel to mean 'having the same root note, or home'. So, it may benefit the question to use the term 'relative' rather than parallel.
Answer: Yes, you definitely started realizing your own answer at the end!
In your key of C major, the ii is the Parallelklang (what we call "relative key" in English) of IV, and vice-versa. And since those two chords both function as predominants, we do see a kinship between the two harmonies.
It's a similar story with I and vi: vi, the Parallelklang of I, often substitutes for I, especially at deceptive cadences where V moves to vi instead of to I. In more advanced tonal music, you'll occasionally see iii substituting for V, and those keys have the same relationship.
But while this is true, I think it's more important to realize that these two Parallelklang-related triads share 2 of 3 tones: C major and A minor both have C E, F major and D minor both have F A, and G major and E minor both have G B. In my mind, it's this relationship that allows these substitutions to occur. The fact that they are relative keys is, to me, less important and more coincidental than the statement "a chord that is 2/3 the same as another chord will in some sense sound similar." (Note also that the remaining pitch is only one whole step away from the pitch in the other triad.)
Looking at your second chart, I think you'd be really interested in reading about Neo-Riemannian music theory. As its name implies, it renews Riemannian ideas to help explain chord progressions that would be considered non-standard in tonal music. Just be aware that it was created by American scholars, so their terminology is based on the English language; this means that C major and A minor are relative keys while C major and C minor are parallel keys. Confusing, huh? :-)
Comment: @ richard: this is a wonderful example that to find a good answer is to ask a good question. if only all user would do this like george ;)
Comment: Truth is that I am very interested in Neo-Riemannian theory, although I feel I am not ready yet to start studying it. Currently, I am reading Schoenberg's "Structural Functions of Harmony", and maybe after that I will start going through Neo-Riemannian textbooks (I really like John Adams's music, so that would motivate me more!). Regarding parallel and relative, in Greece we use the word σχετικός (=relative) to describe relative keys. But I checked the Wikipedia article that says parallel, so I used that instead (https://en.wikipedia.org/wiki/Parallel_and_counter_parallel).
Comment: @George If you know how to build/identify triads and seventh chords, then that's actually all you need to know to learn Neo-Riemannian Theory. (Not that I'm trying to force you to learn it, I'm just letting you know!)
Comment: @AlbrechtHügli - welcome to the site. It appears that you've been round the block a few times, and will be an interesting and useful contributor to this site. Be aware that a lot of the questions posed here are from beginners, thus will be basic and probably not couched in some of the terms that some of us use day to day. We just need to help them on their journey, and eventually, they'll help the next generation in turn - I hope! In the meantime, immerse yourself in the diversity of questions (and answers like Richard's!). Sometimes it's like Pandora's box!

Parallel chord substitutions


Relacionamento BelongsToMany - Sequelize

Question: Olá, estou com um problema na listagem de registros com associação belongsToMany utilizando sequelize + nodejs.
Tenho as tabelas developers e technologies.
A relação entre elas é many to many. Para criar a relação, utilizei uma tabela pivô chamada developers_technologies.
Abaixo tem um gist com o código de: Models (developers, techonologies), migration (developers_technologies) e controller de Developers.
https://gist.github.com/fredarend/85ff60fca70643d80301b499e871c4a6
O código do Index do controller Developer é esse:
async index(req, res) {
const developers = await Developer.findAll({
  attributes: ['id', 'name', 'email'],
  include: [
    {
      model: Technologies,
      as: 'technologies',
      attributes: ['id', 'name'],
      through: { attributes: [] },
    },
  ],
});

return res.json(developers);}

O retorno que estou recebendo é:
[
    {
        "id": 25,
        "name": "Jon Doe",
        "email": "firstname.lastname@example.com",
        "age": 27,
        "url_linkedin": "http://asdasdasd",
        "technologies": []
    }
]

Gostaria de saber por qual motivo não estou recebendo as technologies atreladas ao developer, sendo que existe a tabela developers_technologies no banco de dados está populada e as relações nos models estão ok.
Atenciosamente,
Answer: olha falto a associacao (association)
faz assim :
async index(req, res) {
  const developers = await Developer.findAll({
    attributes: ['id', 'name', 'email'],
    include: [
      {
        model: Technologies,
        as: 'technologies',
        association: 'nome da associacao', attributes: ['id', 'name'],
        through: { attributes: [] },
      },
    ],
  });

  return res.json(developers);
}


Kaprekar Number

Question: can somebody please explain why 4879 isn't a Kaprekar number? It's square is 23804641, which can be divided into 238 and 04641. When we add these two, the sum is 4879.
Thanks.
Comment: I fixed the english Wikipedia article.
Comment: The English Wikipedia page doesn't list $4879$, but the German, French, Italian and Japanese pages do. It is also listed in OEIS: https://oeis.org/A006886
Comment: [It is](https://oeis.org/A006886). Apparently, there is a mistake on Wikipedia article.
Comment: I thought 6174 is a Kaprekar number. It is one where the process of ordering digits in ascending and subtracting from number obtained by ordering digits in descending and repeating the process for any (not all digits same) for digit number leads to it.
Comment: Maybe the solution was omitted because $04641$ starts with a leading zero.
Answer: the digits must be grouped from left to right according to the number of digits the number has. Here:
thhe given no.is 4879.
therefore the sum has to be taken like:
<code>          2380+4641
</code>
which is not equal to 4879.
Comment: No, the digits do not have to be divided evenly and in some cases you can't divide them evenly at all b/c there are an odd number.  Example $297^2=88209$ where you have five digits, you set off the first two from the last three.  For $4879^2$ you set off the first three digits from the last five, not four and four.

Kaprekar Number


UIPageControl with UITableView

Question: I want a page control(UIPageControl) on a view (UIViewController) with table view(UITable) on each page.
Is it possible? If YES then HOW?
Answer: Create UITableView in CellForRowAtIndexPath and then create the UIScrollView which you can add to UITableView and attach your UIPageControl on UIScrollView.
Comment: I didn't understand, can you explain it better?
Comment: alrite. so can you explain me more how do you want it ? U need a tableview in uipagecontrol. I guess you dont need it if you have UIScrollView to work around that part.
Comment: Sure, maybe you can think of other soluction.I have 3 tables, like Favorites, History and Notifications. I have a UIView with others things (like buttons and searchBar) and in the bottom half of the UIView I have a UITableView. The UITableView shows the table of Favorites. I want with swipes to change between tables. I thought about UIPageControl and UIScrollView but maybe there is other alternatives...
Comment: Alrite. So have you created UITableView Favorites rite. So now add UIScrollView of the width and height of the tableView and attach UIPageControl to it. In code: scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; [[cell contentView] addSubView: scrollview]; pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, frame.size.width, frame.size.height)];
[pageControl setNumberOfPages:4];
[[cell contentView] addSubview:pageControl];Ignore my coding since not tried on xcode.

UIPageControl with UITableView


Slight point strokes in ggplot points

Question: Consider the following:
library(ggplot2)
df = data.frame(x = rep(0,9), y = rep(0,9), alp = c(1:8/20,1))
ggplot(df) +
  geom_point(aes(x, y, alpha=alp), size = 20, col = 'red') +
  theme_minimal() + facet_wrap(~ alp) + guides(alpha = F)

As you can see there are feint outlines.  It makes overlaying many low-transparency points look a bit like frogspawn.  Is this just a Mac thing?  Any idea how to remove it?
Comment: Are you talking about the very faint border of the point itself or are you talking about the underline of the background underneath the semi-transparent point?
Answer: The default point shape for ggplot2 is pch = 19. It's not one of those points where the colour of its border and its inside can be controlled separately; for instance, in the following, fill = 'black' has no effect.
library(ggplot2)

df = data.frame(x =runif(1000), y = runif(1000))

p = ggplot(df) +
geom_point(aes(x, y), alpha = .1, size = 5, fill = 'black', colour = 'red') +
  theme_bw()
p

Yet the point does have a boundary line. The line's width can be changed with stroke; as follows:
p = ggplot(df) +
geom_point(aes(x, y), stroke = 2, alpha = .1, size = 5, fill = 'black', colour = 'red') +
  theme_bw()
p

Unfortunately, setting stroke to zero will not remove the boundary line; it seems there is a lower limit.
To remove the boundary line, use one of the shapes that has a border that can be manipulated; for instance, shape = 21. Set its "fill" to red and its "colour" to transparent.
p = ggplot(df) +
geom_point(aes(x, y), shape = 21, alpha = .1, size = 5, fill = 'red', colour = 'transparent') +
  theme_bw()
p

Comment: Whether the boundary vanishes when `stroke` is set to zero also depends on how you print the plot object: for example, when using `grDevices::svg()` to output an `svg`, the boundary disappears with `stroke = 0`.  (I checked that with chromium; with SVG's appearance might also depend on your viewer.)
Comment: Also, if yo use `grDevices::png()` with `type = "cairo"`, the boundary disappears.
Answer: see::geom_point2 draws points without this border.
library(ggplot2)
library(see)

df = data.frame(x = rep(0,9), y = rep(0,9), alp = c(1:8/20,1))

ggplot(df) +
  geom_point2(aes(x, y, alpha=alp), size = 20, col = 'red') +
  theme_minimal() + facet_wrap(~ alp) + guides(alpha = F)

Created on 2020-05-14 by the reprex package (v0.3.0)

Slight point strokes in ggplot points


Why the error using backward Euler is less than using Crank--Nicolson?

Question: I'm reading the following paper
https://www.wias-berlin.de/people/john/ELECTRONIC_PAPERS/JMR06.CMAME.pdf
where calculates the solution of the time-dependent Navier--Stokes equations using different $\theta$-Schemes for the time, and finite element method in each time step (Taylor--Hood). My question is related with BWE (backward Euler) and CN (Crank--Nicolson). If we see the picture that I attach (Figure 2 of Section 4.1 of the paper) the error in the pressure using BWE is less than the error using CN, Is this possible? I understand that the CN method is better, so I would expect the pressure error to be lower using CN. I think it is ok, but I would like to know some explanation.
My second question is: BWE is order 1, and CN is order 2, but I see that in the pressure they have the same order. Why I can't see the order or the errors in these figures?
We can see the same behaviour in all the numerical tests, but I only attached one of them.
Comment: The fact that the relative performance is reversed between velocity and pressure suggests that there is some nontrivial dynamical effect in the discretized systems which is not negligible toward the left side of these graphs. In the case of BWE presumably it is the familiar improved stability of implicit methods in general. (That said I can't tell from context what "time step" actually means, which is problematic for a careful assessment...)
Comment: Thanks Ian, the time steps are $0.1\times 2^{-k}$, with $k=0,1,... 8$
Comment: They are using FEM for the spatial discretisation, but what finite elements exactly? P1-P1, P1-P1 bubble, P2-P1, other? If the error introduced by the space discretisation is not properly controlled, the estimation of the error introduced time discretisation becomes inconsistent.
Comment: Hi @PierreCarre, I implemented the same examples using Taylor--Hood with Pk-Pk-1. I tested with k=2,3,4, using Picard and Newton, obtaining the same results than in the paper. I tested with very refined meshes of triangles, and the paper uses squares.
Answer: Typical error estimates for these algorithms are based on worst-case scenarios. However, on a specific problem, it's usually hard to predict which algorithm will perform the best.
As for CN behaving order 1 instead of order 2 -- my guess is that, if we check the list of assumptions in the theorem which guarantees order 2 for CN, one of them likely doesn't hold here.


How to force a drop of MSSQL Server database

Question: I am trying to delete an MSSQL Server database, however I am having no luck. I have tried multiple things such as
user
ALTER DATABASE my_database
SET RESTRICTED_USER
WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE my_database;
GO

I have also tried to right click on it a delete it.
This does not work, it tells me "Cannot drop database "ima_debts" because it is currently in use". The thing is there is definately no other user connected to it. In fact I disabled TCP/IP for the database and restarted it.
Not even "Microsoft SQL Server Management Studio (Administrator)" is connected to it. I have made sure to login to "master".
Why is it telling me it is currently in use. Is it possible for me to delete perhaps a directory or something from the file system to get rid of this database?
Any help would be appreciated.
Thanks.
Answer: Sometimes IntelliSense and/or Management Studio will hang connections open just because you clicked on the database in Object Explorer. Also, if you have any SQL Agent jobs that access the database, these will not be stopped by disabling TCP/IP.
Regardless, the following script will "take over" the database by putting it into single-user mode in the session, and then take it offline so no one can connect to it to run queries.
You can then drop the database using Management Studio or T-SQL.
USE [MyDB];
GO

ALTER DATABASE [MyDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO

ALTER DATABASE [MyDB] SET OFFLINE;
GO

Comment: Don't you need to use db 'master' here?
Answer: From http://www.morgantechspace.com/2013/11/Cannot-drop-database-because-it-is-currently-in-use-in-MS-SQL-Server.html
USE [master]
GO
ALTER DATABASE [TestDB] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
/****** Object:  Database [TestDB]    Script Date: 11/29/2013 13:40:36 ******/
DROP DATABASE [TestDB]
GO

In C#
public static void DeleteDataBase()
{
    using (SqlConnection sqlconnection = new
        SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=master;Integrated Security=SSPI;"))
    {
        sqlconnection.Open();
        // if you used master db as Initial Catalog, there is no need to change database
        sqlconnection.ChangeDatabase("master");

        string rollbackCommand = @"ALTER DATABASE [TestDB] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE";

        SqlCommand deletecommand = new SqlCommand(rollbackCommand, sqlconnection);

        deletecommand.ExecuteNonQuery();

        string deleteCommand = @"DROP DATABASE [TestDB]";

        deletecommand = new SqlCommand(deleteCommand, sqlconnection);

        deletecommand.ExecuteNonQuery();
    }
}

Answer: When you delete it in SSMS there's a checkbox that reads "Close existing connections". This checkbox is not checked by default. Check the box and any connections to the DB will be terminated allowing you to delete it.


Question regarding prefix codes

Question: Question: Show that for any alphabet $E$ of size $[E]=m$ $(m>0)$, there exists a prefix code $f: E \to \{0,1\}*$ with codeword lengths $l = 1,2,\dots,m$.
Just started this topic on Coding and have just learnt about Kraft's Inequality not sure how I would go about showing there exists a prefix code for this alphabet is this something I need to find as a follow up from Kraft's Inequality. Any help or guidance would be greatly appreciated!
Answer: Kraft's inequality can be used here. It says that if your input alphabet $E$ has size $n$, your codeword alphabet has size $r$, there is a prefix code with codeword lengths $\ell_1,\ldots,\ell_n$ if and only if
$$\sum_{k=1}^n\left(\frac1r\right)^{\ell_k}\le 1\;.\tag{1}$$

What are $m,r$, and $\ell_1,\ldots,\ell_n$ in your problem?

Once you've answered that, substitute the appropriate values into the summation in $(1)$ and evaluate it to see whether it is indeed less than or equal to $1$.
Answer: You can apply Kraft's inequality (see Brian M Scott answer) to prove that such a code must exist; or, in this particular case, you can also explicitly contruct it:
$$E_1 \to 0$$
  $$E_2 \to 10$$
  $$E_3 \to 110$$
  $$E_4 \to 1110$$
  $$ \cdots$$

Question regarding prefix codes


Sum of all Residues at the poles of the function $f(z)=\frac{\cot(\pi z)}{(z+a)^2}$, where $a \in \mathbb{Z}-{0}$

Question:
Sum of all Residues at the poles of the function $f(z)=\frac{\cot(\pi
 z)}{(z+a)^2}$, where $a \in \mathbb{Z}-\{0\}$
(a)$\frac{1}{\pi} \sum_{n=-\infty}^{\infty}\frac{1}{(n+a)^2}-\pi
cosec^2 \pi a$
(b)$-\frac{1}{\pi} \sum_{n=-\infty}^{\infty}\frac{1}{(n+a)^2}-\pi
cosec^2 \pi a$
(c)$\frac{1}{\pi} \sum_{n=-\infty}^{\infty}\frac{1}{(n+a)^2}+\pi cosec^2 \pi a$
(d) $-\frac{1}{\pi} \sum_{n=-\infty}^{\infty}\frac{1}{(n+a)^2}+\pi
cosec^2 \pi a$

We want $$\sum_{a \in \mathbb{Z}-\{0\}} Res_{z=-a}\frac{\cot(\pi
 z)}{(z+a)^2}$$
$$Res_{z=a}\frac{\cot(\pi
 z)}{(z-a)^2}$$
$$\lim_{z\rightarrow a} (z-a)\frac{\cot(\pi
 z)}{(z-a)^2}$$
$$\lim_{z\rightarrow a} \frac{\cot(\pi
 z)}{(z-a)}=cot(\pi a)$$
Am I correct?. I don't get any series given the option. Please help me.
Answer: Form $\cot\pi z=\dfrac{\cos\pi z}{\sin\pi z}$ we know the points $\pi z=kz$ or $z=k\in\mathbb{Z}$ are poles. If $-a\neq k$ then we have a simple pole in $z=k$ so
\begin{align}
\operatorname{Res}_{z=k}\frac{\cot\pi z}{(z+a)^2}
&= \lim_{z\to k} (z-k)\frac{\cot\pi z}{(z+a)^2} \\
&= \lim_{w\to0} w\frac{\cos\pi(w+k)}{(w+k+a)^2\sin\pi(w+k)} \\
&= \lim_{w\to0} w\frac{\cos(k\pi+w\pi)}{(w+k+a)^2\sin(k\pi+w\pi)} \\
&= \dfrac{1}{\pi(k+a)^2}
\end{align}
 If $-a=k$ then we have a pole of order $3$ in $z=k$ so
\begin{align}
\operatorname{Res}_{z=-a}\frac{\cot\pi z}{(z+a)^2}
&= \lim_{z\to-a}\frac{1}{2!}\dfrac{d^2}{dz^2}\left((z+a)^3\frac{\cot\pi z}{(z+a)^2}\right) \\
&= \frac{1}{2!}\times-2\pi(1+\cot^2\pi(-a)) \\
&= -\pi\csc^2\pi a
\end{align}
then the sum of residues is
$$\color{blue}{\sum_{k=-\infty,k\neq -a}^\infty\dfrac{1}{\pi(k+a)^2}-\pi\csc^2\pi a}$$
Comment: I understood the theory behind second part, but the second derivative gone too complicated. How did you calculate the $Res_{z=-a}$
Comment: $\frac{d^2}{dz^2}(z+a)(cot\pi z)=2(z+a)\pi^2 cosec^2 \pi z cot\pi z$
Comment: I am getting like this. @MyGlasses
Comment: please help me @MyGlasses
Comment: Sorry. I left here. Check you derivative again
Comment: $$\frac{d}{dz}(z+a)cot \pi z =-\pi(z+a) cosec^2\pi z + cot\pi z. right?$$
Comment: @My Glasses: I got it. Thank you. :)
Comment: Second derivative is $2 \pi ^2 (a+x) \cot (\pi  x) \csc ^2(\pi  x)-2 \pi  \csc ^2(\pi  x)$, anyway you're welcome and good luck!
Comment: I got it already, by careful differentiation.
Comment: Anyway thank you again.


How to upload image without using or by hiding ?

Question: I am making a php script in which, I fetch the listing of directories and files from the targeted local computer drives.
Now, what I am trying to achieve is, when I click on particular image name from the listing, it suppose to upload to my server.
I don't want to select the image using "browse / input type='file'" button.
I tried several ways but not succeeded yet.
Kindly help.
Thanks in advance.
H Gandhi
Comment: it means you want to upload image to server by just selecting image from local machine.
Comment: You can't do that due to security reasons. You can upload only the files, that was selected by user.
Comment: You has to use file input. If you do not want to show the input you can hide it.
Comment: @TarangP, shukshin.ivan, Nirali Joshi. Thank you all for quick reply. suggest how to hide the  and at the same time set value to it without using form.
Answer:

$('button').click(function(){
    $('input').click();
});
input {
    display: block;
    visibility: hidden;
    width: 0;
    height: 0;
}

<input type="file" name="somename" size="chars">
<button>Choose File Button</button>


Filter a multidimensional array by another array in javascript

Question: I'm trying to filter an array, from another array :
I have the first array :
var json = [["ID1", "Person1", "10"], ["ID2", "Person2", "20.0"], ["ID3", "Person3", "50.0"], ["ID4", "Person4", "40.0"]];

My filter array : (which is not ordered);
var filter = ["ID4", "ID1"];

And the result I would like to have :
var json = [["ID1", "Person1", "10"], ["ID4", "Person4", "40.0"]];

or
var json = [["ID4", "Person4", "40"], ["ID1", "Person1", "10.0"]];

Comment: Or just plain old iteration -> http://jsfiddle.net/NWQJV/
Answer: You could do
json = json.filter(function(v) { return filter.indexOf(v[0])!==-1 })

But I'd recommend you to avoid naming json a variable not holding some JSON.
If you want to be compatible with IE8, I'd recommend to use a shim for filter and indexOf or simply to iterate using 2 for loops.
Comment: That's the best solution for newer browsers, but not supported in IE8 or below. +1, but it looks strange with variable names like `json` and `filter` !
Comment: It works great thanks!
I'll check the alternative for IE8, since I'll need it.
Thanks!


Finetunng a deep network

Question: Can we perform fine tuning using the pre-trained weight file when the network is slightly changed?
I have changed the CNN from classification to regression network and rest of the network is the same. I have a pretrained model file for classification.
Answer: Yes, this can be done. Usually, when you switch from classification to regression, you may "decapitate" the CNN by chopping off the top few layers including the softmax, and putting on new fully connected layers you need for regression.
Therefore you would be using the pretrained weights for the rest of the layers, but you would need to start training the new layers you tacked on from scratch.
The procedure for doing this in practice depends very much on which framework you are using. In keras, I believe you can just load the entire model with all the weights, and then just pop off the top few layers and add on your new layers. In tensorflow, you want to build your new regression first, then load only the weights from the old layers, making use of variable scopes to specify which weights should be restored.
https://www.tensorflow.org/programmers_guide/variables#choosing_which_variables_to_save_and_restore
Comment: I am using caffe as the framework.
I want to use SqueezeNet (https://arxiv.org/pdf/1602.07360.pdf) as the network. But SqueezeNet has no fully connected layer.

Finetunng a deep network


How do you add javascript to forms in the Zend Framework?

Question: How do you add javascript to forms created with the Zend framework?  For instance, if I wanted to validate the form, or if I wanted to utilize events like blur, focus and onclick?
Answer: What I tend to do is :

From the PHP code, generate the form, using Zend_Form and the related classes

This includes setting up the form itself,
But also server-side validations and all that

Once the form works fine, I add Javascript in an un-obstrusive way.

Which means my JS code is totally distinct from the PHP/Zend_Form code.
And also mean my form doesn't rely on Javascript being activated in the user's browser, as the form already works before.

If you are using jQuery, you can detect when the page is loaded $.ready, and hook a function on that event -- function that will add some dynamic bahaviors to your form.
Of course, you can do the same sort of thing with other JS libraries -- I only took jQuery as an example because of its popularity, and the fact there are some jQuery-related components in ZendX.


Como funciona os tipos de dados apos constante no C?

Question: Estou lendo alguns códigos e me deparei com o seguinte código
#define foo unsigned int
#define i typedef

eu sei como funciona esse unsigned e signed, typedef etc, mas eu não estou conseguindo entender como isso funciona na constante.
Answer: Este exemplo mostrado na pergunta não serve para nada de útil, a não ser demonstrar a diretiva #define.
O resultado disso é o seguinte:
#define foo unsigned int
#define i typedef

// bla bla bla

foo x;     // literalmente: "unsigned int x;"
i int y;   // literalmente: "typedef int y;"
y z;       // literalmente: "int z;"

Como disse acima, este exemplo não serve para nada de útil, apenas para demonstrar a diretiva #define. Obviamente, a diretiva #define é muito útil e aparece bastante nas próprias bibliotecas da linguagem C.
Comment: nossa cara é isso mesmo =D obg. mas seu exemplo serve para coisas útil sim, um ex e que a libc usa ele dessa forma que vc mostrou :)


Plot a generalised mixed effects model with binomial errors

Question: Plotting a glm binomial model is reasonably simple with the predict function. I'm having trouble creating a similar plot for a glmer model; predict doesn't work:
id    <- factor(rep(1:20, 3))
age   <- rep(sample(20:50, 20, replace=T), 3)
age   <- age + c(rep(0, 20), rep(3, 20), rep(6, 20))
score <- rbinom(60, 15, 1-age/max(age))
dfx   <- data.frame(id, age, score)

library(lme4)
glmerb  <- glmer(cbind(score, 15-score) ~ age + (1|id), dfx, family=binomial)
ndf     <- expand.grid(age=10:60) #for extensibility, usually also have factors
ndf$fit <- predict(glmerb, ndf, type="response")
*Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "mer"*

How can I produce the desired plot?
While I'm at it, what other plots would be useful for this kind of model for either diagnostic, presentation or glam purposes?
Comment: Of possible interest: [Using lmer for prediction](http://stats.stackexchange.com/q/4187/930). And it's probably worth to check what Doug Bates has to say in his draft on [lme4: Mixed-effects Modeling with R](http://lme4.r-forge.r-project.org/) (I didn't check it, though), or maybe [LMERConvenienceFunctions](http://cran.r-project.org/web/packages/LMERConvenienceFunctions/) on CRAN.
Comment: Unfortunately not much in his lme4 drafts. Although I will check his book. This came close http://glmm.wikidot.com/faq but there's no glmer plot only lmer.
Answer: Take a look at the ez package, particularly at ezPredict.
ps. if you would like to use 'to_predict' parameter, you'll need the dev version, see the instructions here: https://github.com/mike-lawrence/ez
Answer: The issue is that the mer class in R, and the lmer etc commands in lme4 all produce mer objects, and these are not compatible with some "normal" R commands.
You can get the fits out by using fitted but amending your code to
ndf$fit <- fitted(glmerb, ndf, type="response")

gave me the error
Error in `$<-.data.frame`(`*tmp*`, "fit", value = c(0.213527879025905,  :
replacement has 60 rows, data has 51

But this worked:
> fit <- fitted(glmerb, ndf, type="response")
> str(fit)
num [1:60] 0.214 0.282 0.335 0.154 0.335 ...

Is that what you were after?
Comment: Close. What I wanted was perhaps simpler, and that is the straight fixed effects curve. Not so much individual predictions. However this is useful and satisfies Q2. Perhaps a manual command to "curve" will do the trick?
Answer: To plot curves of fixed effects, I typically use code like this:
model.coefs <- fixef(model)
curve( invlogit( cbind(1, x) %*% model.coefs ), add=TRUE )

Note that invlogit is in the arm package.


matrix elementary column operations

Question: Until now I was using the elementary row operations to do the gaussian elimination or to calculate the inverse of a matrix.
As I started learning Laplace's transformation to calculate the determinant of a $n \times n$ matrix, I noticed that the book uses elementary column operations. I tried to use the column operations to do the gaussian elimination or to solve a $Ax = b$ matrix but it didn't work (comes out as a wrong answer).
I'm getting confused!
Example:
Let $A=\left[\begin{array}{rrrr}
x_1 & x_2 & x_3 \\
2 & 1 & 3  \\
4 & 4 & 2  \\
1 & 1 & 4 \\
 \end{array}\right]b= \left[\begin{array}{r}10\\8\\16\end{array}\right]$
in this case if I interchange two rows/add a row to another/or multiply a row with a nonzero element the answer is always $$x = \begin{bmatrix}{-2,2,4}\end{bmatrix}$$
but if I interchange for example $x_1$ and $x_2$ /add a column to another.... comes out a different answer.
So why do column operations work for some operations and others not? How do you know when to use the column operations?
It would be great if anybody can help!
Reminder:
Elementary Row / Column Operations :
1. Interchanging two rows/or columns,
2. Adding a multiple of one row/or column to another,
3. Multiplying any row/or column by a nonzero element.
Answer: In your example, if you interchange two first colums you'll get
$$
\left[\matrix{
x_1 & x_2 & x_3 \\
2 & 1 & 3  \\
4 & 4 & 2  \\
1 & 1 & 4}\right]\quad\sim\quad
\left[\matrix{
x_2 & x_1 & x_3 \\
1 & 2 & 3  \\
4 & 4 & 2  \\
1 & 1 & 4}\right].
$$
What happened in the top most row is that the roles of $x_1$ and $x_2$ have been interchanged. When you get a solution to the modified system as $(2,-2,4)$, you must not forget to switch the variables back to the original ones.
Similar thing happens if you take, say, the first column minus the second column
$$
\left[\matrix{
x_1 & x_2 & x_3 \\
2 & 1 & 3  \\
4 & 4 & 2  \\
1 & 1 & 4}\right]\quad\sim\quad
\left[\matrix{
\tilde x_1 & \tilde x_2 & \tilde x_3 \\
1 & 1 & 3  \\
0 & 4 & 2  \\
0 & 1 & 4}\right].
$$
To see the relation between the old and the new variables, we write
$$
A_1x_1+A_2x_2+A_3x_3=(A_1-A_2)\tilde x_1+A_2\tilde x_2+A_3\tilde x_3=
A_1\tilde x_1+A_2(\tilde x_2-\tilde x_1)+A_3\tilde x_3.
$$
hence, $x_1=\tilde x_1$, $x_2=\tilde x_2-\tilde x_1$ and $x_3=\tilde x_3$, i.e. your variables has changed again. It means that

working with columns you are working with variables while working with rows you are working with equations.

In general, elementary row operations are equivalent to left multiplication by an elementary matrix transformation
$$
Ax=b\quad\implies\quad \underbrace{LA}_{\tilde A}x=\underbrace{Lb}_{\tilde b},
$$
while column operation corresponds to right multiplication by an elementary matrix. Because this multiplication has to be done for $A$ and matrix multiplication is not commuting, we have to compensate this action by the inverse matrix
$$
Ax=b\quad\implies\quad \underbrace{AR}_{\tilde A}\underbrace{R^{-1}x}_{\tilde x}=b.
$$
Comment: I guess in the first line, you mean 'interchange the first two `columns`'.
Comment: Yes, that's right. many thanks!
Answer: Manipulating rows is like manipulating the equations of the system but without changing the order of the unknowns, so interchanging the rows has no effect on the resolution of the system. Interchanging columns however is equivalent to changing the order of the variables, so the only way to obtain the same result as before, you'll have to adjust $b$ accordingly. In the same manner one can see that adding columns is illogical, since, for example, adding $x_1$ to $x_2$ is equivalent to saying that $2\alpha + 1\beta=3\beta$ (assuming $\alpha, \beta$ and $\gamma$ are the variables in that order), which is not true here.

matrix elementary column operations


Consulta em 2 Tabelas Muito Lenta no mysql

Question: <code>SELECT scanlators.* FROM lista_de_capitulos
INNER JOIN scanlators ON JSON_CONTAINS(lista_de_capitulos.scanlator, scanlators.id)
GROUP BY scanlators.url ORDER BY COUNT(lista_de_capitulos.id_manga) DESC LIMIT 100
</code>
Como posso melhorar velocidade de execução dessa consulta? está levando cerca de 15 segundos pra completar a execução atualmente.
Comment: Avalie o plano de execução e se, por acaso, a implementação de algum índice possa melhorar a performance de sua consulta.
Comment: esse json_contains parece custoso, não entendi pq está fazendo assim
Comment: @LucasMiranda e porque a coluna e um objeto.
Comment: mas precisa ser mesmo? ele vai olhar o objeto inteiro só pra ver se possui o id, isso não é nada performático, se você pudesse normalizar esse cara em outra tabela, você poderia indexar
Answer: Ocorre que esta agrupando a tabela inteira, para depois selecionar as 100 primeiras linhas.
Teria que dizer que quer trabalhar na primeira centena de linhas.
Estou com mesmo problema e desconheço a solução
Comment: E como seria possível selecionar as 100 linhas com maior contagem sem contar todas elas antes?
Comment: acredito não ser possível associar somente 100 linhas, porque ele precisa avaliar mais de 50 mil linhas de capitulos e 500 de scan lator


Does integral of f(x) = the integral of conjugate of f(x)?

Question: $$\int f(x) = \int \overline f(x)$$
Is the above statement true?
Comment: No, $\int \overline f\,dx=\overline{\int f\,dx}$.
Answer: Since integration on complex functions is defined by
$$\int f(z) \mathrm dz := \int \Re(f(z)) \mathrm dz + i \int\Im (f(z))\mathrm dz$$
You can quickly see
$$\int \bar f(z) \mathrm dz = \int\Re(\bar f(z)) \mathrm dz + i \int \Im(\bar f(z)) \mathrm dz = \int\Re(f(z)) \mathrm dz + i \int -\Im (f(z)) \mathrm dz \\
= \int \Re(f(z)) \mathrm dz -i\int\Im(f(z)) \mathrm dz = \overline{\int f(z) \mathrm dz}$$


How to override "Create @bundlename" text?

Question: I'm using String Overrides module to override some strings on my site. I could override some strings like
"Performance" to "Clear"   (admin/config/development/performance)
"Content" to "List of Nodes" (admin/content)
But it doesn't override "Create @bundlename" strings like
"Create Article" to "New Article".

When I navigate to /node/add/article it's still showing Create Article as title of the 'Node add form'.
I've cleared cache, but still not working.
Any suggestions?
Answer: You can try using Node Add title module ...

Above module allows you to configure the title of the Node Add pages
  and the Node Editing pages.
The default title of the page where users create new nodes is
  constructed by Drupal in a dynamic way that not always looks good. By
  default Drupal concatenates the word Create and the name of the
  content type. The result could be something like "Create Basic page"
  or "Create article". This is not always acceptable.
With this little module you can replace Drupal's default concatenated
  Node Add page title and the Node Editing page title with a title of
  your choice for every content type.
After installing this module you will see the following two new fields
  at the bottom of the content type configuration form:
Node Add page title Node Edit page title Use these two new fields for
  overriding the titles of the new node creation pages and the node
  editing pages.

PS : Module is in Dev Version.... Test before you use..
If you are interested to change same using code you can use drupal_set_title
function mymodule_form_alter(&$form, $form_state, $form_id) {
    if($form_id == 'mynodetype_node_form'){
         drupal_set_title('My new title');
    }
}

Comment: Thanks Anil, The module works great! I can use coding but client wants user interface for that so I had to install this module . both ways helpful. Thanks for the alternate module :)
Answer: For those who may be looking to override the string for node creation using string override, you need to use the following under Original : "Create @name"
Answer: This is the reason why it doesn't work the way you want it to.  StringOverrides uses the localization functions, and the localization functions do not translate @params.
Answer: In original Column write "Create @bundlename". In Replacement Column write "New @bundlename".
Note : This will override for all the Bundles.
Comment: I think I already tried that but didn't work. Did you check it?


Algoritmo para definir prioridade de ordem de serviço

Question: Boa noite. Sou iniciante na programação e estudante de T.I. acabei de criar minha conta aqui no Stackoveflow.
Tenho noções básicas (BEM básicas) de Python e JavaScript (e HTML e CSS também).
Estou tentando desenvolver um programa simples para um projeto do meu curso, onde temos uma "empresa" de T.I. que precisa de um método de controle de fluxo de atendimento, para definir a prioridade das ordens de serviço.
A ideia é que o programa faça algumas perguntas referente ao atendimento. Cada pergunta recebe um valor caso a resposta seja positiva ou negativa. Ao final do programa, os valores são somados, e se atingem um determinado score, são categorizados como Prioridade 1, Prioridade 2 ou Prioridade 3.
Fiz um modelo usando JavaScript e HTML, porém, não ta funcionando de jeito nenhum.
Peguem leve, sei que muito provavelmente devem ter alguns erros bem esdrúxulos.
Se alguém puder dar uma força, agradeço

var per1 = document.getElementByName('p1');
var per2 = document.getElementByName('p2');
var per3 = document.getElementByName('p3');
var rs = document.getElementById('res');

function calc() {
  if (per1 == 's') {
    var per1 = 1;
  } else if (per1 == n) {
    var per1 = 2;
  } else {
    alert('Por favor, digite um valor válido!');
  }

  if (per2 == s) {
    var per2 = 1;
  } else if (per2 == n) {
    var per2 = 2;
  } else {
    alert('Por favor, digite um valor válido!');
  }

  if (per3 == s) {
    var per3 = 3;
  } else if (per3 == n) {
    var per3 = 1;
  } else {
    alert('Por favor, digite um valor válido!');
  }
  var rs = (per1 + per2 + per2);
  innerHTML.res += ` ${rs}`;
}
<!doctype html>

  <meta charset=utf-8>
  <title>Priority IT</title>
  <link rel="stylesheet" href="style.css">

Priority IT - dualreceiver

Perguntas:

Responda com "s" para SIM ou "n" NÃO:

1. O diagnóstico foi conciso? <input type="text" name="p1">

2. Você considera que o problema será resolvido de forma relativamente rápida? <input type="text" name="p2">

3. Cliente é PCD, idoso ou necessita atenção especial? <input type="text" name="p3">

    <input type="button" value="Calculate" onclick='calc()'>

 Calculando...

Answer:
Você tem que pegar os valores dos inputs dentro da função, e não fora. Isso porque ao chamar a função, as variáveis às quais são atribuídas os valores dos inputs estarão sempre vazias no carregamento da página, e a função não irá pegar os valores corretos no momento em que a função for chamada.
document.getElementByName não existe. Existe document.getElementsByName (Elements e não Element). E como document.getElementsByName retorna uma lista de nós, é preciso usar o índice [0], e para pegar o valor do input, acrescentar .value no final: document.getElementsByName('p1')[0].value.
Faltaram as aspas nas strings s e n dentro dos if's, senão essas letras serão tratadas como variáveis que não existem.
Já que as variáveis per1, per2 e per3 já foram declaradas com var, não é necessário utilizar var novamente para alterar seus valores.
innerHTML.res está incorreto. Primeiro vem o elemento e depois o .innerHTML, assim: res.innerHTML.
Coloque um return após cada alert para impedir que a função chegue até o final em caso de entrar num bloco else.
Na soma dos valores você repetiu o per2 no final: per1 + per2 + per2, quando o correto seria: per1 + per2 + per3
No innerHTML não precisaria utilizar o operador +=. Se você quer substituir o conteúdo da div#res, o operador seria apenas o =.
Colocar uma div dentro de um
 é incorreto no HTML. Você pode utilizar apenas o
, desta forma:
 Calculando...
.

Seu código ficaria assim:

var rs = document.getElementById('res');

function calc() {
   var per1 = document.getElementsByName('p1')[0].value;
   var per2 = document.getElementsByName('p2')[0].value;
   var per3 = document.getElementsByName('p3')[0].value;
  if (per1 == 's') {
    per1 = 1;
  } else if (per1 == 'n') {
    per1 = 2;
  } else {
    alert('Por favor, digite um valor válido!');
    return;
  }

  if (per2 == 's') {
    per2 = 1;
  } else if (per2 == 'n') {
    per2 = 2;
  } else {
    alert('Por favor, digite um valor válido!');
    return;
  }

  if (per3 == 's') {
    per3 = 3;
  } else if (per3 == 'n') {
    per3 = 1;
  } else {
    alert('Por favor, digite um valor válido!');
    return;
  }
  var rs = (per1 + per2 + per3);
  res.innerHTML = ` ${rs}`;
}

Priority IT - dualreceiver

Perguntas:

Responda com "s" para SIM ou "n" NÃO:

1. O diagnóstico foi conciso? <input type="text" name="p1">

2. Você considera que o problema será resolvido de forma relativamente rápida? <input type="text" name="p2">

3. Cliente é PCD, idoso ou necessita atenção especial? <input type="text" name="p3">

   <input type="button" value="Calculate" onclick='calc()'>

 Calculando...

Comment: Muito obrigado!
Comment: Sei que foram alguns erros meios bobos, vou estudar mais. Obrigado mesmo!


get the file URI of an ImageItem

Question: How can I get the file URI of a Drupal\image\Plugin\Field\FieldType\ImageItem instance?
I get the field value with $node_entity->get('field_image')[0] but I don't know how to get the file URI.
Answer: You can use
$image_item->entity->getFileUri()

get the file URI of an ImageItem


Average area of the shadow of a convex shape

Question: What is the average area of the shadow of a convex shape taken over all possible orientations?
If we take a sphere, its surface area is exactly 4 times the area of its shadow. How can it be generalised for any convex shape? I know there are a lot of books like "Introduction to Geometric Probability", but I would appreciate an almost-intuitive explanation.
Comment: A very nice explanation can be found here: https://youtu.be/ltLUadnCyi0
Comment: The ratio is always $1/4$ for any convex shape in 3D. You can see [Christian Blatter's explanation](https://math.stackexchange.com/a/93743/856) for the 2D case in which the ratio is $1/\pi$.
Comment: @liaombro: Yes, I was going by the title of the question ("average area ... taken over all possible orientations"), which unfortunately does not appear in the question body. *Edit:* I've now moved it to the body of the question.
Comment: @kvantour I just came in to suggest that exact video. +1 upvote there! 3Blue1Brown cover it well from both the specific grind and the slick general approaches.
Comment: @Rahul the shadow of a cube when you project it parallel to its edge, is 1/6 of its surface area. Do you mean that the _average_ ratio is 1/4?
Comment: Let me correct a wrong assumption in the question body. The surface area of a sphere isn't 4 times the area of its shadow. That's because the shadow is always bigger than the object. So here, the shadow is bigger than the greatest circle of the sphere. In fact, the ratio of the area of the shadow to the surface area of the sphere is: $$\frac{A}{a}=\frac{1}{4}(\frac{q}{p})^2$$ Where $A$ is the area of the shadow, $a$ is the surface area of the sphere, $q$ is the distance between the light source to the shadow, and $p$ is the distance between the light source and the sphere.
Answer: The average size of the shadow cast by any 3D convex shape is $\frac{1}{4}$ times its surface area.

Because the shape is convex, you can compute the size of its shadow along a particular direction $\vec{u}$ by summing the shadows cast by its surface pieces:

Divide the surface of the shape into little area patches.
Compute the size of each patch's shadow by measuring the size of the dot product with $\vec{u}$.
Add up the sizes of each of the shadow-patches.
Note that this is actually twice the size of the shape's shadow, because we've double-counted: every patch has a counterpart on the "other side" of the shape, and both patches cast the same, overlapping shadow.  So we must divide our total by two.

Because the shadow of a convex shape can be computed by summing the shadows of its individual surface patches, the average shadow can be computed in the same way:

Divide the surface of the shape into little area patches.
Compute the average shadow cast by the patch over all directions.
Add up the average shadows of each of the patches.
Divide the total by two.

There is a constant of proportionality $\lambda$ which connects the surface area of any convex object to the average size of its shadow.

The size of the shadow cast by a patch, averaged over all directions, should be proportional to the area of the patch. (After all, both the patch and the shadow have units of area, and magnifying the patch by some amount should magnify the shadow by the same amount.)
It follows that there is a constant of proportionality $\lambda$ such that if the area of the patch is $A$, the average area of its shadow is $\lambda \cdot A$.
When we compute the average shadow of the shape by adding up the individual contributions from each patch, this constant $\lambda$ will factor out of the sum.  The remaining sum is just the surface area of the shape.
Hence the average shadow cast by a convex shape should be $\lambda$ times its surface area (divided by two).

Because the constant of proportionality $\lambda$ is the same for all convex shapes, we can use a known shape to solve for its value.  A sphere of radius 1 has surface area $4\pi$, and casts a shadow of area $\pi$ in each direction. Hence its average shadow area over all directions is $\pi$, and the constant of proportionality must be $$\lambda \equiv \frac{\pi}{4\pi} =  \frac{1}{4}.$$

  So the average size of the shadow cast by any 3D convex shape is $\frac{1}{4}$ times its surface area.

Bonus: By extension to $n$ dimensions, there will be a constant of proportionality $\lambda_n$ relating the outer surface of a convex volume to the average size of its shadow.  We can use $n$-dimensional spheres, whose geometries are known, to compute those constants.
The constant $\lambda_n$ is the volume of an $n-1$ ball (a disc, in our example), divided by the surface area of an $n-1$ sphere (a sphere, in our example). So, by standard formulas,
$$V_n = \frac{\pi^{n/2}}{\Gamma(\frac{n}{2}+1)}$$
$$A_n = \frac{2\cdot \pi^{\frac{n+1}{2}}}{\Gamma(\frac{n+1}{2})}$$
$$\lambda_{n+1} = \frac{V_{n}}{A_n} = \frac{\pi^{n/2}}{\Gamma(\frac{n}{2}+1)}\cdot \frac{\Gamma(\frac{n}{2}+2)}{2\cdot \pi^{\frac{n+1}{2}}} = \frac{1}{2\sqrt{\pi}}\frac{\Gamma(\frac{n}{2}+2)}{\Gamma(\frac{n}{2}+1)} $$
Note that the $\Gamma()$ function simplifies if we consider odd and even cases separately. After manipulation, we find:

$$\lambda_{n+1} = \begin{cases}\frac{1}{2^{n+1}} {n \choose n/2} & n\text{ even}\\
\frac{1}{\pi}\frac{2^n}{n+1} {n \choose {\lfloor n/2\rfloor} }^{-1} & n\text{ odd}
\end{cases}$$
 And so $$\lambda_n = \frac{1}{2},\; \frac{1}{\pi},\; \frac{1}{4},\; \frac{2}{3\pi},\; \frac{3}{16},\; \frac{8}{15\pi},\; \ldots .$$
Comment: Aren't you skipping a step here? You say **every patch has a counterpart on the "other side" of the shape, and both patches cast the same, overlapping shadow**. But what if the solid lacks the symmetry for that to hold in all orientations, i.e., in a given orientation, the surface area of the portion being hit by light is not equal to the surface area of the dark portion?
Comment: Is, for example, $\frac{3}{16}$ the ratio of the area of the shadow to the area of the object in $5$ dimensions?
Comment: If you place a cube near a wall from one of the plains will the area of the shadow be $\frac{1}{6}$ of the surface area of the cube or $\frac{1}{4}$ of it? What if we place the cube from one vertex near the wall?
Comment: @SnackExchange Yes, 3/16 is the ratio between the *average* shadow and surface area in five dimensions.  *Average* shadow means the area of the shadow when averaged over all possible viewing angles.

When viewed head-on, a cube's shadow is 1/6 of its total surface area. But when averaged over all viewing angles, the cube's shadow is on average 1/4 of its total surface area. This formula is for averaged viewing angle, not specific viewing angles.